all repos — simple-discord-music-bot @ 4025dc44c0749f0952822a9facce3b32010871a9

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

add bust logic on /shoot
Bi-Rabittoh andronacomarco@gmail.com
Sat, 27 Jan 2024 13:48:06 +0100
commit

4025dc44c0749f0952822a9facce3b32010871a9

parent

3df93a72ede8d6132fe3d3ccb0ff5f2fdaf949c8

1 files changed, 16 insertions(+), 6 deletions(-)

jump to
M src/commands/shoot.tssrc/commands/shoot.ts

@@ -1,6 +1,8 @@

-import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; +import { SlashCommandBuilder, ChatInputCommandInteraction, GuildMember, APIInteractionGuildMember } from 'discord.js'; import { getChannel } from '../functions/voice'; import { getMagazine } from '../functions/magazines'; + +const bustProbability = 0.5; module.exports = { data: new SlashCommandBuilder()

@@ -19,14 +21,22 @@

const magazine = getMagazine(interaction.user.id); if (!magazine.shoot()) return await interaction.reply({ content: `💨 Too bad... You're out of bullets.` }); + + const allMembers = voiceChannelUsers.map((voiceState) => voiceState.member); + const killerId = interaction.user.id - const members = voiceChannelUsers.map((voiceState) => voiceState.member); - const l = members.length; - const randomIndex = Math.floor(Math.random() * l); - const toBeKicked = members[randomIndex].user.bot ? members[(randomIndex + 1) % l] : members[randomIndex]; + let toBeKicked: GuildMember; + if (Math.random() >= bustProbability) { + toBeKicked = allMembers.find((e) => e.user.id === killerId); + } else { + const members = allMembers.filter((m) => !m.user.bot && m.user.id != killerId); + const l = members.length; + const randomIndex = Math.floor(Math.random() * l); + toBeKicked = members[randomIndex].user.bot ? members[(randomIndex + 1) % l] : members[randomIndex]; + } toBeKicked.voice.disconnect(); - const victimName = toBeKicked.nickname ?? toBeKicked.user.globalName; + const victimName = toBeKicked.nickname ?? toBeKicked.user.globalName ?? toBeKicked.user.tag; return await interaction.reply({ content: `💥 Bang! **${victimName}** was shot. **${magazine.left}/${magazine.size} bullets** left in your magazine.` }); }, };