Get channel Id automatically, make the command requirement an url insted of an id.

master
Cristiano Pires 2 years ago
parent d26096eea2
commit c53dd9b26f

@ -2,12 +2,13 @@ const {Command, Random, ErrorMessage, ErrorType} = require('../../lib.js')
const {EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle} = require('discord.js');
const xmlparser = require('xml-js')
const feedM = require('../../models/feeds');
const { get } = require('mongoose');
class setYTFeed extends Command{
constructor(client){
super(client, {
name: 'setFeed',
aliases:['setYTFeed','setytf', 'setYTF'],
description: 'Sets the message channel where a defined youtube channel feed will be displayed. (Order: YTchannelId, discord channel id, Costum message to display when a new video is released.) You can only assign one feed per channel.',
description: 'Sets the message channel where a defined youtube channel feed will be displayed. (Order: Youtube channel url, discord channel id, Costum message to display when a new video is released) You can only assign one feed per channel.',
needsAdmin:true,
});
this.client = client;
@ -66,11 +67,12 @@ class setYTFeed extends Command{
async confirmArgs(message,args)
{
if(args.length<2) return {error:'Unsuficient amount of arguments.'}
var YTChannelId = args[0];
var channelURL = args[0];
var YTChannelId = await getChannelId(channelURL);
if(!YTChannelId) return {error:'Provided Youtube channel does not exist.'};
var channelId = args[1];
const exists = await this.client.channels.fetch(channelId).then(channel=>{return channel }).catch(()=>{return });
if(!exists) return {error:'Provided channel id does not exist.'}
if(!exists) return {error:'Provided channel id does not exist.'}
var name = await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=${YTChannelId}`)
.then(handleResponse)
.then(handleData)
@ -93,7 +95,7 @@ class setYTFeed extends Command{
data = JSON.parse(data);
return data.feed.author.name._text
}
if(!name) return {error:'Provided Youtube channel id does not exist.'}
if(!name) return {error:'Provided Youtube channel does not exist.'}
const channelInUse = await feedM.findOne({ChannelId:channelId}).then(channel=>{return channel}).catch(()=>{return []});
if(channelInUse) return {error:'Discord channel already in use. Please choose another.'}
return {YTChannelId, channelId, name, channelName:exists.name}
@ -109,4 +111,25 @@ function _delete(message, m)
} catch (error) {
console.log('SetYTID: Error deleting the message:',error)
}
}
}
async function getChannelId(url)
{
return await fetch(url)
.then(handleResponse)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.text();
}
function handleError(error)
{
console.log(error)
return error;
}
function handleData(data)
{
return data.split('https://www.youtube.com/feeds/videos.xml?channel_id=')[1].split('"')[0];
}
}

@ -1,53 +1,28 @@
var channelId = 'UCRsrOaKEdy0ymNqR7Urqa2Q'
const xmlparser = require('xml-js');
(async function ()
async function getChannelId(url)
{
return await fetch(url)
.then(handleResponse)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.text();
}
function handleError(error)
{
console.log(error)
return error;
}
function handleData(data)
{
return data.split('https://www.youtube.com/feeds/videos.xml?channel_id=')[1].split('"')[0];
}
}
(async ()=>
{
var coiso = await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}`)
.then(handleResponse)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.text()
}
function handleError(error)
{
return error;
}
function handleData(data)
{
data = xmlparser.xml2json(data,
{
compact: true,
space: 4
});
data = JSON.parse(data)
// console.log({
// channelName:data.feed.author.name._text,
// lasVid:
// {
// link:data.feed.entry[0].link._attributes.href,
// published:data.feed.entry[0].published._text
// }
// })
console.log(data.feed.entry[0])
return {
channelName:data.feed.author.name._text,
lasVid:
{
link:data.feed.entry[0].link._attributes.href,
published:data.feed.entry[0].published._text,
link:data.feed.entry[0].link._attributes.href,
image:data.feed.entry[0].link._attributes.href.split('=')[1],
title:data.feed.entry[0].title._text,
author:{
name:data.feed.entry[0].author.name._text,
url:data.feed.entry[0].author.url._text
}
var coiso = await getChannelId('https://www.youtube.com/@craftedworkshop');
console.log(coiso)
})()
}
}
}
//console.log(coiso, coiso.channelName)
})()
Loading…
Cancel
Save