all repos — simple-discord-music-bot @ 3df93a72ede8d6132fe3d3ccb0ff5f2fdaf949c8

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

src/commands/shoot.ts (view raw)

 1import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
 2import { getChannel } from '../functions/voice';
 3import { getMagazine } from '../functions/magazines';
 4
 5module.exports = {
 6    data: new SlashCommandBuilder()
 7        .setName('shoot')
 8        .setDescription('Kick a random member of your voice channel.'),
 9
10    async execute(interaction: ChatInputCommandInteraction) {
11        const channel = await getChannel(interaction);
12        if (typeof channel == 'string')
13            return await interaction.reply({ content: channel, ephemeral: true });
14
15        const voiceChannelUsers = interaction.guild.voiceStates.cache.filter(
16            (voiceState) => voiceState.channelId === channel.id
17        );
18
19        const magazine = getMagazine(interaction.user.id);
20        if (!magazine.shoot())
21            return await interaction.reply({ content: `💨 Too bad... You're out of bullets.` });
22
23        const members = voiceChannelUsers.map((voiceState) => voiceState.member);
24        const l = members.length;
25        const randomIndex = Math.floor(Math.random() * l);
26        const toBeKicked = members[randomIndex].user.bot ? members[(randomIndex + 1) % l] : members[randomIndex];
27
28        toBeKicked.voice.disconnect();
29        const victimName = toBeKicked.nickname ?? toBeKicked.user.globalName;
30        return await interaction.reply({ content: `💥 Bang! **${victimName}** was shot. **${magazine.left}/${magazine.size} bullets** left in your magazine.` });
31    },
32};