207 lines
8.2 KiB
JavaScript
Executable File
207 lines
8.2 KiB
JavaScript
Executable File
// ZeSkypeBot 7.0.0_a
|
|
|
|
// *** Libraries to import ***
|
|
const { Client, Events, GatewayIntentBits } = require('discord.js');
|
|
const Voice = require('@discordjs/voice');
|
|
const { createReadStream, existsSync } = require('fs');
|
|
const play = require('play-dl');
|
|
// Local config files to import
|
|
const { token } = require('./config.json');
|
|
|
|
// Declare a new Discord Client Instance
|
|
const client = new Client( { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates] } );
|
|
|
|
// 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);
|
|
}
|
|
// Subscribe to a few signal events
|
|
process.on('SIGINT', signalExit); // For Ctrl-C when running in a TTY
|
|
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}`);
|
|
});
|
|
client.on( Events.MessageCreate, async message => {
|
|
// *** FIlter Section ***
|
|
// In the event of a DM...
|
|
if (message.guild == null) {
|
|
switch (message.author.id) {
|
|
case "262406433201586177": // Weegee
|
|
case "137358406821347328": // moosecrap
|
|
case "144317130026778624": // Piroglith
|
|
case "137293315883139072": // tks_ftw
|
|
case "172538370440953867": // Brightland
|
|
case "183009699300507648": // spamminn
|
|
case "209154502412992514": // NariNaju
|
|
case "198278682677084160": // DrunkZombie
|
|
case "137349560610586624": // craze
|
|
case "305491553198145539": // Sam
|
|
case "174356437550497792": // NoobWantsHacks
|
|
case "308412988329558017": // Revanite
|
|
// TODO: Recieve sound files from trusted parties?
|
|
return;
|
|
default:
|
|
console.log(`Ignoring DM from ${message.author.tag}: ${message.content}`);
|
|
return;
|
|
}
|
|
}
|
|
// Filter Authorized Guilds
|
|
switch (message.guild.id) {
|
|
case "860344888301846538": // ZeSkypeBot Incubator
|
|
case "305204527152365581": // Goldar Squad
|
|
break;
|
|
default:
|
|
console.log(`Message from Unauthorized Guild ${message.guild.name}!`);
|
|
return;
|
|
}
|
|
|
|
// *** 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}`);
|
|
if (!message.member.voice.channel) {
|
|
message.channel.send('you can\'t stop the party if you\'re not here');
|
|
} else {
|
|
const connection = Voice.getVoiceConnection(message.guild.id);
|
|
connection.destroy();
|
|
}
|
|
return;
|
|
}
|
|
// Try to find a sound file that matches the message
|
|
if (!message.content.match(/[ \/;]/)) {
|
|
// we had to add '/' because moose tried to 'play' ../../../../etc/shadow
|
|
// Construct the expected sound path
|
|
const soundPath = `./Sounds/${message.content}.ogg`;
|
|
if (message.author.id === "172538370440953867") {
|
|
switch(message.content) {
|
|
case "butthash":
|
|
message.react('859687015247511561');
|
|
default:
|
|
}
|
|
}
|
|
if (message.author.id === "144317130026778624") {
|
|
switch(message.content) {
|
|
case "daytona":
|
|
case "e":
|
|
if ([0,5,6].includes(d.getDay()) && Math.random() < 0.8) return;
|
|
default:
|
|
}
|
|
}
|
|
// Test if there is a file at the expected path
|
|
if (message.member.voice.channel && existsSync(soundPath)) {
|
|
console.log(`Sound '${message.content}' issued by ${message.author.tag} ${message.author.id}`);
|
|
// Join the Voice Channel of the author
|
|
const connection = Voice.joinVoiceChannel({
|
|
channelId: message.member.voice.channelId,
|
|
guildId: message.guild.id,
|
|
adapterCreator: message.guild.voiceAdapterCreator,
|
|
});
|
|
// Create an audio player for the desired sound
|
|
const resource = Voice.createAudioResource(createReadStream(soundPath));
|
|
const player = Voice.createAudioPlayer();
|
|
// Play the resource through the player to the connection (i hate discord v16)
|
|
player.on('Playing', () => {
|
|
console.log(`Playing ${soundpath}`);
|
|
});
|
|
player.play(resource);
|
|
connection.subscribe(player);
|
|
// Setup Callbacks for when the player completes
|
|
player.on('Idle', () => {
|
|
console.log(`Finished ${soundpath}`);
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
// Otherwise process commands
|
|
if (message.content === "!help" ) {
|
|
console.log(`!help issued by ${message.author.tag} ${message.author.id}`);
|
|
var response = 'ZeSkypeBot v7.1.0 \'Table Salt\'\n';
|
|
response += 'Original by moosecrap#5953 for Skype, Remade by Weegee#6402 for Discord\n';
|
|
response += 'Changelog:\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 += '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';
|
|
response += 'v6.0.1 Added !play for youtube videos\n';
|
|
response += 'v6.0.0 Public release, plays sounds from Goldar Squad\'s collection of micspam audio files. No list will be provided\n';
|
|
response += 'Commands:\n';
|
|
response += '~ stop any sounds in progress and disconnect from voice\n';
|
|
response += '!help print this message\n';
|
|
response += 'To play Sounds, first join a voice channel then send a message in the text chat with the name of the sound\n';
|
|
response += '!play [youtube url] play a youtube video over chat\n';
|
|
response += '!play [search terms] play the first video that comes up after searching youtube with your terms\n';
|
|
message.channel.send(response);
|
|
return;
|
|
}
|
|
// Check for youtube play command
|
|
if (message.content.startsWith('!play') && message.member.voice.channel) {
|
|
// Determine if we have a URL or a search term
|
|
const ytmatch = message.content.match(/(?<=(https?:\/\/)(www\.)?youtu((be\.com\/watch\?v=)|(\.be\/)))[\w-]{11}/);
|
|
var yturl = null;
|
|
var timestamp = '0'
|
|
if (ytmatch) {
|
|
// 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) {
|
|
timestamp = timematch[1];
|
|
}
|
|
// Reconstruct a URL for the supplied video
|
|
yturl = `https://youtu.be/${ytmatch[0]}`;
|
|
} else {
|
|
// Message may be a search term
|
|
let args = message.content.split('play')[1];
|
|
// Search youtube with the term
|
|
let yt_info = await play.search(args, { limit: 1 });
|
|
// and get the first result as a URL
|
|
yturl = yt_info[0].url;
|
|
}
|
|
// Create a stream reference to the provided URL
|
|
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,
|
|
});
|
|
// 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();
|
|
// Play the resource through the player (i hate discord v16)
|
|
player.on('Playing', () => {
|
|
console.log(`Playing ${yturl}`);
|
|
});
|
|
player.play(resource);
|
|
// Connect the player to the voice connection (internal screaming intensifies)
|
|
connection.subscribe(player);
|
|
// Setup Callbacks for when the player completes
|
|
player.on('Idle', () => {
|
|
console.log(`Finished ${yturl}`);
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Generic message responses
|
|
if (message.content.match(/https:\/\/ootbingo\.github\.io/)) {
|
|
var response = 'ooh its bingo time, i call ';
|
|
if (Math.floor(Math.random()*2)) { response += 'over'; }
|
|
else { response += 'under'; }
|
|
response += ' on gateskip';
|
|
message.channel.send(response);
|
|
return;
|
|
}
|
|
});
|
|
// Now our client is prepared, we can log in.
|
|
client.login(token);
|