add intro selector
Marco Andronaco andronacomarco@gmail.com
Thu, 15 Sep 2022 23:32:23 +0200
6 files changed,
63 insertions(+),
36 deletions(-)
M
.eslintrc.json
→
.eslintrc.json
@@ -13,7 +13,7 @@ "brace-style": ["warn", "stroustrup", { "allowSingleLine": true }],
"comma-dangle": ["error", "always-multiline"], "comma-spacing": "error", "comma-style": "error", - "curly": ["error", "multi-line", "consistent"], + "curly": [0, "multi-line", "consistent"], "dot-location": ["error", "property"], "handle-callback-err": "off", "indent": ["error", "tab"],@@ -23,7 +23,7 @@ "max-statements-per-line": ["error", { "max": 2 }],
"no-console": "off", "no-empty-function": "error", "no-floating-decimal": "error", - "no-inline-comments": "error", + "no-inline-comments": 0, "no-lonely-if": "error", "no-multi-spaces": "error", "no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1, "maxBOF": 0 }],
M
.vscode/launch.json
→
.vscode/launch.json
@@ -12,6 +12,24 @@ "skipFiles": [
"<node_internals>/**" ], "program": "${workspaceFolder}/index.js" - } + }, + { + "name": "Deploy commands", + "program": "${workspaceFolder}/tools/deploy-commands.js", + "request": "launch", + "skipFiles": [ + "<node_internals>/**" + ], + "type": "node" + }, + { + "name": "Delete commands", + "program": "${workspaceFolder}/tools/delete-commands.js", + "request": "launch", + "skipFiles": [ + "<node_internals>/**" + ], + "type": "node" + }, ] }
M
commands/outro.js
→
commands/outro.js
@@ -1,51 +1,60 @@
const path = require('node:path'); const { SlashCommandBuilder } = require('discord.js'); -const { createAudioResource, createAudioPlayer, NoSubscriberBehavior, joinVoiceChannel, AudioPlayerStatus } = require('@discordjs/voice'); +const { createAudioResource, createAudioPlayer, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus } = require('@discordjs/voice'); -const OUTRO_PATH = path.join(process.cwd(), 'resources', 'outro.mp3'); +const outros = [ + { name: 'Random!', value: 'random' }, + { name: 'TheFatRat - Xenogenesis', value: 'thefatrat_xenogenesis.mp3' }, + { name: 'OMFG - Hello', value: 'omfg_hello.mp3' }, +]; -function get_player(resource) { +function get_player(resource, user_connection) { - const player = createAudioPlayer({ - behaviors: { - noSubscriber: NoSubscriberBehavior.Pause, - }, - }); + const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause } }); - player.on('error', error => { - console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`); - }); - + player.on('error', error => console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`)); player.on(AudioPlayerStatus.Idle, () => { player.stop(); player.subscribers.forEach(element => { element.connection.disconnect(); }); }); + player.on(AudioPlayerStatus.Playing, () => setTimeout(() => user_connection.disconnect(), 20_000)); player.play(createAudioResource(resource)); return player; } module.exports = { - data: new SlashCommandBuilder().setName('outro').setDescription('Leave with an outro.'), + data: new SlashCommandBuilder() + .setName('outro') + .setDescription('Leave with an outro.') + .addStringOption(option => + option.setName('outro') + .setDescription('Select which outro to play') + .setRequired(false) + .addChoices(...outros)), + async execute(interaction) { - const member = interaction.member; - - if (!member) return await interaction.reply('Please use this in your current server.'); + if (!member) return await interaction.reply({ content: 'Please use this in your current server.', ephemeral: true }); const user_connection = member.voice; const channel = user_connection.channel; - - if (!channel) return await interaction.reply('You\'re not in a voice channel.'); + if (!channel) return await interaction.reply({ content: 'You\'re not in a voice channel.', ephemeral: true }); const guild = channel.guild; const bot_connection = joinVoiceChannel({ channelId: channel.id, guildId: guild.id, adapterCreator: guild.voiceAdapterCreator }); - const player = get_player(OUTRO_PATH); - bot_connection.subscribe(player); - setTimeout(() => user_connection.disconnect(), 20_000); - return await interaction.reply('Prepare for takeoff.'); + const outro = interaction.options.getString('outro'); + let outro_file = outro ? outro : 'random'; + if (outro_file == 'random') + outro_file = outros[Math.floor(Math.random() * (outros.length - 1)) + 1].value; + + const song_path = path.join(process.cwd(), 'resources', outro_file); + bot_connection.subscribe(get_player(song_path, user_connection)); + + const outro_title = outros.find(element => element.value == outro_file).name; + return await interaction.reply({ content: `Prepare for takeoff with ${outro_title}!`, ephemeral: true }); }, };
M
index.js
→
index.js
@@ -1,6 +1,6 @@
const fs = require('node:fs'); const path = require('node:path'); -const { Client, Collection, GatewayIntentBits, ActivityType, PresenceUpdateStatus, Presence } = require('discord.js'); +const { Client, Collection, GatewayIntentBits, ActivityType } = require('discord.js'); const { token } = require(path.join(process.cwd(), 'config.json')); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });@@ -16,20 +16,20 @@ client.commands.set(command.data.name, command);
} const activities = [ - { activity: ActivityType.Watching, subject: 'i cantieri'}, // sta guardando i cantieri - { activity: ActivityType.Playing, subject: 'briscola'}, // sta giocando a briscola - { activity: ActivityType.Competing, subject: 'prato fiorito'}, // in competizione su prato fiorito + { activity: ActivityType.Watching, subject: 'i cantieri' }, // sta guardando i cantieri + { activity: ActivityType.Playing, subject: 'briscola' }, // sta giocando a briscola + { activity: ActivityType.Competing, subject: 'prato fiorito' }, // in competizione su prato fiorito - { activity: ActivityType.Watching, subject: 'i bambini'}, - { activity: ActivityType.Playing, subject: 'quel gioco là non mi viene il titolo'}, - { activity: ActivityType.Competing, subject: 'cacata ranked al cesso pubblico'}, -] + { activity: ActivityType.Watching, subject: 'i bambini' }, + { activity: ActivityType.Playing, subject: 'quel gioco là non mi viene il titolo' }, + { activity: ActivityType.Competing, subject: 'cacata ranked al cesso pubblico' }, +]; client.once('ready', () => { const random = Math.floor(Math.random() * (activities.length)); - const choice = activities[random] - - client.user.setActivity(choice.subject, { type: choice.activity }) + const choice = activities[random]; + + client.user.setActivity(choice.subject, { type: choice.activity }); console.log('Bot online!'); });