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

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

src/commands/shoot.ts (view raw)

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