all repos — simple-discord-music-bot @ f94eeec38208cc3cb098cb453aed7d33e263114c

A Discord bot making use of discord.js and play-yt.

move outros and radios to config
Bi-Rabittoh andronacomarco@gmail.com
Sun, 07 Jan 2024 11:56:49 +0100
commit

f94eeec38208cc3cb098cb453aed7d33e263114c

parent

a7ad8ea3524e373bd9fc9b70422850b6bbf0b178

5 files changed, 34 insertions(+), 26 deletions(-)

jump to
M commands/outro.jscommands/outro.js

@@ -1,13 +1,13 @@

const { SlashCommandBuilder } = require('discord.js'); const { playUrl, getChannel } = require('../functions/music'); +const path = require('node:path'); +const { outros } = require(path.join(process.cwd(), 'config.json')); -const outros = [ - { name: 'Random!', value: 'random' }, - { name: 'TheFatRat - Xenogenesis', value: 'https://www.youtube.com/watch?v=6N8zvi1VNSc' }, - { name: 'OMFG - Hello', value: 'https://www.youtube.com/watch?v=5nYVNTX0Ib8' }, - { name: 'Pegboard Nerds - Disconnected', value: 'https://www.youtube.com/watch?v=YdBtx8qG68w' }, - { name: 'Gym Class Heroes - Stereo Hearts', value: 'https://www.youtube.com/watch?v=ThctmvQ3NGk' }, -]; +function getOutroUrl(outro) { + if (outro) return outro; + const randomIndex = Math.floor(Math.random() * (outros.length)); + return outros[randomIndex].value; +} module.exports = { data: new SlashCommandBuilder()

@@ -22,7 +22,7 @@ .addStringOption(option =>

option.setName('kick') .setDescription('Do you actually want to log off?') .setRequired(false) - .addChoices({ name: 'Yes', value: 'true' }, { name: 'No', value: 'false' })), + .addChoices({ name: 'Yes', value: 'true', default: 'true' }, { name: 'No', value: 'false' })), async execute(interaction) { const channel = await getChannel(interaction);

@@ -31,9 +31,8 @@ return await interaction.reply({ content: channel, ephemeral: true });

const outro = interaction.options.getString('which'); const kick = interaction.options.getString('kick'); - const randomIndex = Math.floor(Math.random() * (outros.length - 1)) + 1; - const outro_file = outro ? outro : outros[randomIndex].value; - await playUrl(outro_file, channel); + const outroUrl = getOutroUrl(outro); + await playUrl(outroUrl, channel); const kick_switch = kick ? kick : 'true'; if (kick_switch == 'true') {
M commands/radio.jscommands/radio.js

@@ -1,14 +1,7 @@

const { SlashCommandBuilder } = require('discord.js'); const { playStream, getChannel } = require('../functions/music'); - -// const reg = /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/; - -const radios = [ - { name: 'EusariRadio', value: 'https://onair7.xdevel.com/proxy/xautocloud_cnou_1049?mp=/;stream/' }, - { name: 'Radio 24', value: 'https://shoutcast3.radio24.ilsole24ore.com/stream.mp3' }, - { name: 'Radio Delfino', value: 'https://nr8.newradio.it/proxy/emaamo00?mp=/stream?ext=.mp3' }, - { name: 'Radio 105', value: 'https://icy.unitedradio.it/Radio105.mp3' }, -]; +const path = require('node:path'); +const { radios } = require(path.join(process.cwd(), 'config.json')); module.exports = { data: new SlashCommandBuilder()

@@ -27,7 +20,6 @@ return await interaction.reply({ content: channel, ephemeral: true });

await interaction.deferReply(); - // Get the YouTube URL or search query const radio = interaction.options.getString('which'); const streamUrl = radio ? radio : radios[0].value;
M commands/stop.jscommands/stop.js

@@ -10,8 +10,8 @@ async execute(interaction) {

const channel = await getChannel(interaction); if (typeof channel == 'string') return await interaction.reply({ content: channel, ephemeral: true }); - - player.stop(); + + if (player) player.stop(); return await interaction.reply({ content: 'Stopped.', ephemeral: true }); }, };
M config.json.exampleconfig.json.example

@@ -1,4 +1,18 @@

{ "applicationId": "your-client-id-here", - "token": "your-token-here" + "token": "your-token-here", + "outros": + [ + { "name": "TheFatRat - Xenogenesis", "value": "https://www.youtube.com/watch?v=6N8zvi1VNSc" }, + { "name": "OMFG - Hello", "value": "https://www.youtube.com/watch?v=5nYVNTX0Ib8" }, + { "name": "Pegboard Nerds - Disconnected", "value": "https://www.youtube.com/watch?v=YdBtx8qG68w" }, + { "name": "Gym Class Heroes - Stereo Hearts", "value": "https://www.youtube.com/watch?v=ThctmvQ3NGk" } + ], + "radios": + [ + { "name": "EusariRadio", "value": "https://onair7.xdevel.com/proxy/xautocloud_cnou_1049?mp=/;stream/" }, + { "name": "Radio 24", "value": "https://shoutcast3.radio24.ilsole24ore.com/stream.mp3" }, + { "name": "Radio Delfino", "value": "https://nr8.newradio.it/proxy/emaamo00?mp=/stream?ext=.mp3" }, + { "name": "Radio 105", "value": "https://icy.unitedradio.it/Radio105.mp3" } + ] }
M index.jsindex.js

@@ -7,12 +7,15 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });

client.commands = new Collection(); const commandsPath = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); -let player; for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); - client.commands.set(command.data.name, command); + if ('data' in command && 'execute' in command) { + client.commands.set(command.data.name, command); + } else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } } client.once('ready', () => {