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

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

src/commands/outro.ts (view raw)

 1import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
 2import { getQueue } from '../functions/music';
 3import { getChannel } from '../functions/voice';
 4import path from 'node:path';
 5const { outros } = require(path.join(process.cwd(), 'config.json'));
 6
 7function getOutroUrl(outro: string): string {
 8    if (outro) return outro;
 9    const randomIndex = Math.floor(Math.random() * (outros.length));
10    return outros[randomIndex].value;
11}
12
13module.exports = {
14    data: new SlashCommandBuilder()
15        .setName('outro')
16        .setDescription('Leave with an outro.')
17        .addStringOption(option =>
18            option.setName('which')
19                .setDescription('Select which outro to play')
20                .setRequired(false)
21                .addChoices(...outros))
22        .addStringOption(option =>
23            option.setName('kick')
24                .setDescription('Do you actually want to log off?')
25                .setRequired(false)
26                .addChoices({ name: 'Yes', value: 'true' }, { name: 'No', value: 'false' })),
27
28    async execute(interaction: ChatInputCommandInteraction) {
29        const channel = await getChannel(interaction);
30        if (typeof channel == 'string')
31            return await interaction.reply({ content: channel, ephemeral: true });
32
33        const outro = interaction.options.getString('which');
34        const kick = interaction.options.getString('kick');
35        const outroUrl = getOutroUrl(outro);
36        const queue = getQueue(interaction.guildId);
37        await queue.outro(outroUrl, channel);
38
39        if (kick !== 'false') {
40            const member = interaction.member;
41            if ("voice" in member)
42                setTimeout(() => member.voice.disconnect(), 20_000);
43            return await interaction.reply({ content: 'Prepare for takeoff!', ephemeral: true });
44        }
45        return await interaction.reply({ content: 'Playing outro.', ephemeral: true });
46    },
47};