From 988707ed9c39e5293345b7a9cd7ab887e2319aae Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 30 Aug 2023 19:10:57 -0700 Subject: [PATCH] Added Debug to assist with yt latency troubleshooting --- .gitignore | 2 +- bot.js | 76 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 265e634..a280ef0 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .env -config.json +.data/* diff --git a/bot.js b/bot.js index 5ab2779..5e33117 100755 --- a/bot.js +++ b/bot.js @@ -12,10 +12,13 @@ const client = new Client( { intents: [GatewayIntentBits.Guilds, GatewayIntentBi // Define Sigtrap Callback for clean shutdowns function signalExit(signal) { console.log(`\tCaught ${signal}`); - // Kills voice connections, and logs the client out - client.destroy(); - // Exit with return code 0 - process.exit(0); + // Signal via discord that we are closing + client.channels.fetch("860393205401255936") + .then(papi => papi.send('bye bye papi') ) + // then disconnect + .then( client.destroy() + // and when that is done finally exit + .then( process.exit(0) ) ); } // Subscribe to a few signal events process.on('SIGINT', signalExit); // For Ctrl-C when running in a TTY @@ -24,8 +27,13 @@ process.on('SIGTERM', signalExit); // for systemd's stoppping of services // Define Discord Callbacks client.on( Events.ClientReady, c => { console.log(`Logged in Successfully as ${c.user.tag}`); + // Signal via discord that we are alive + client.channels.fetch("860393205401255936") + .then( papi => papi.send('hello papi') ); }); client.on( Events.MessageCreate, async message => { + // Variables that may be useful + var d = new Date(); // Today's date and timestamp // *** FIlter Section *** // In the event of a DM... if (message.guild == null) { @@ -60,8 +68,6 @@ client.on( Events.MessageCreate, async message => { } // *** Process Section *** - // Variables that may be useful - const d = new Date(); // Today's date and timestamp // Check for the shutup message if (message.content === "~") { console.log(`!stop issued by ${message.author.tag}`); @@ -123,10 +129,10 @@ client.on( Events.MessageCreate, async message => { console.log(`ytmatch yields ${ytmatch[0]}`); // Message matches the youtube video regex, see if it comes with a timestamp const timematch = message.content.match(/(?<=t=)(\d{1,4})s?(?=$)/); - if (timematch) { - console.log(`timematch yields $timematch[1]`); - timestamp = timematch[1]; - } + if (timematch) { + console.log(`timematch yields $timematch[1]`); + timestamp = timematch[1]; + } // Reconstruct a URL for the supplied video yturl = `https://youtu.be/${ytmatch[0]}`; } else { @@ -142,34 +148,37 @@ client.on( Events.MessageCreate, async message => { } console.log(`yturl: ${yturl}`); // Create a stream reference to the provided URL - const stream = await play.stream(yturl, { seek: timestamp }); + const stream = await play.stream(yturl, { + seek: timestamp + }); // Now that we have a stream reference we can join the channel and play const connection = Voice.joinVoiceChannel({ - channelId: message.member.voice.channel.id, - guildId: message.guild.id, - adapterCreator: message.guild.voiceAdapterCreator, - }); + channelId: message.member.voice.channel.id, + guildId: message.guild.id, + adapterCreator: message.guild.voiceAdapterCreator, + }); // Open the stream as a voice resource - const resource = Voice.createAudioResource(stream.stream, { inputType: stream.type }); - // Create a player to play the resource - const player = Voice.createAudioPlayer(); + const resource = Voice.createAudioResource(stream.stream, { inputType: stream.type }); + // Create a player to play the resource + const player = Voice.createAudioPlayer(); // Play the resource through the player (i hate discord v16) - player.play(resource); + player.play(resource); // Connect the player to the voice connection (internal screaming intensifies) - connection.subscribe(player); - // Setup Callbacks for when the player completes + connection.subscribe(player); + // Setup Callbacks for when the player completes return; } if (message.content === "!help" ) { console.log(`!help issued by ${message.author.tag} ${message.author.id}`); - var response = 'ZeSkypeBot v7.2.0 \'Table Salt\'\n'; - response += 'Original by moosecrap#5953 for Skype, Remade by Weegee#6402 for Discord\n'; + var response = 'ZeSkypeBot v7.2.1 \'Table Salt\'\n'; + response += 'Original by @moosecrap for Skype, Remade by @fredstonemason for Discord\n'; response += 'Changelog:\n'; + response += 'v7.2.1 Added !echo to try and deduce why theres lag in commands\n'; response += 'v7.2.0 Attempts to fix seemingly random searches, now should support age-restricted videos\n'; response += 'v7.1.0 Added support for searching in !play\n' - response += 'v7.0.0 Update to Discord.js 14\n'; - response += 'v6.1.0 Update to Discord.js 13, replaced ytdl-core-discord with play-dl\n'; - response += 'v6.0.6 Changes to !play regex in pursuit of playing at timestamps\n'; + response += 'v7.0.0 Update to Discord.js 14\n'; + response += 'v6.1.0 Update to Discord.js 13, replaced ytdl-core-discord with play-dl\n'; + response += 'v6.0.6 Changes to !play regex in pursuit of playing at timestamps\n'; response += 'v6.0.5 Added feedback for cases where ytdl crashes\n'; response += 'v6.0.3 Changed !play regex per user feedback and counterexamples\n'; response += 'v6.0.2 Removed response to voiceless sound commands, for being too spammy\n'; @@ -184,7 +193,7 @@ client.on( Events.MessageCreate, async message => { message.channel.send(response); return; } - + // Generic message responses if (message.content.match(/https:\/\/ootbingo\.github\.io/)) { var response = 'ooh its bingo time, i call '; @@ -194,6 +203,19 @@ client.on( Events.MessageCreate, async message => { message.channel.send(response); return; } + + if (message.content === "!echo" ) { + var response = 'Debug Timers:\n```'; + response += `Discord Timestamp: ${message.createdAt.toISOString()}\n`; + response += `Local event start: ${d.toISOString()}\n`; + d = new Date(); + response += `Local event now: ${d.toISOString()}\n`; + response += `Online Since: ${client.readyAt.toISOString()}\n`; + response += '```'; + message.channel.send(response); + return; + } + }); // Now our client is prepared, we can log in. client.login(token);