Error handlign + small refactoring

master
Cristiano Pires 2 years ago
parent c53dd9b26f
commit b02a6a46cb

@ -2,7 +2,6 @@ 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, {
@ -107,7 +106,7 @@ function _delete(message, m)
{
try {
message.delete();
m.delete();
m.message.delete();
} catch (error) {
console.log('SetYTID: Error deleting the message:',error)
}
@ -115,21 +114,21 @@ function _delete(message, m)
async function getChannelId(url)
{
if(url.split('@').length<1) return false
return await fetch(url)
.then(handleResponse)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.text();
if(response.ok) 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];
return data ? data.split('https://www.youtube.com/feeds/videos.xml?channel_id=')[1].split('"')[0] : false;
}
}

@ -123,7 +123,8 @@ class _Client extends Client
if(message.content.startsWith('!'))
{
let commandName=message.content.split('!')[1].split(' ')[0];
let args = message.content.split(`!${commandName}`)[1].split(' ');
let args = message.content.split(' ');
args.splice(0,1);
args = args.filter(n=>n);
if(this.commands.get(commandName))
{
@ -201,13 +202,16 @@ class _Client extends Client
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:{
author:
{
name:data.feed.entry[0].author.name._text,
url:data.feed.entry[0].author.uri._text
}
}
}
const channel = this.channels.cache.get(feed.ChannelId)
if(!res) return false;
const channel = this.channels.cache.get(feed.ChannelId);
if(!channel) return false;
const lastSentMessage = await channel.messages.fetch().then(res=>
{
for(var item of res)
@ -299,12 +303,11 @@ class ErrorMessage
*
* @param {ErrorType} errorType
* @param {Message} message
* @param {[... String]} extraMessages
* @param {[String]} extraMessages
*/
async send(errorType,message, extraMessages)
{
console.log('Sending Error Message')
if (!message.channel.id) return;
const embed = new EmbedBuilder()
.setColor(0x4d0000)

Loading…
Cancel
Save