eritislami / evobot

🤖 EvoBot is a Discord Music Bot built with TypeScript + Discord.js, includes Docker builds & localization in 20+ languages
MIT License
1.83k stars 2.02k forks source link

Feature: Add a command to force bot to disconnect #1185

Open salut-c-leo opened 2 years ago

salut-c-leo commented 2 years ago

Is your feature request related to a problem? Please describe. Sometimes, the bot just won't leave after the timeout period is over and no music is being played. Rather than having a bot online even when no one is in the voice call, there should be a command to forcefully disconnect the bot.

Describe the solution you'd like Like [prefix]play or [prefix]ping, [prefix]leave should disconnect the bot.

Describe alternatives you've considered Restarting the npm service, but it's not as user-friendly and requires admin access to the console.

zPanic commented 2 years ago

Hi,

Bit late, but I've coded something in that accomplishes this goal. Create a disconnect.ts in your /commands/ folder, and put this in:

`import { Message } from "discord.js"; import { bot } from "../index"; import { i18n } from "../utils/i18n"; import { canModifyQueue } from "../utils/queue";

export default { name: "disconnect", aliases: ["dc"], description: i18n.__("disconnect.description"), execute(message: Message) { const queue = bot.queues.get(message.guild!.id);

if (!queue) return message.reply(i18n.__("disconnect.errorNotQueue")).catch(console.error);
if (!canModifyQueue(message.member!)) return i18n.__("common.errorNotChannel");

queue.stop();

queue.textChannel.send(i18n.__mf("disconnect.result", { author: message.author })).catch(console.error);

queue.connection.destroy();

} };`

In your /locales/en.json, add this:

"disconnect": { "description": "Stops the music and disconnects the bot", "errorNotQueue": "There is nothing playing.", "result": "🎶 Disconnected!" },

Unnil commented 1 year ago

Are you able to reproduce that issue or send some logs after it happens? What is your stay_time value?

nnyj commented 11 months ago

Updated code in case anyone needs. Thanks to @zPanic for the original code.

/commands/leave.ts:

import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";

export default {
  data: new SlashCommandBuilder().setName("leave").setDescription(i18n.__("leave.description")),
  async execute(interaction: ChatInputCommandInteraction) {
    const guildMember = interaction.guild!.members.cache.get(interaction.user.id);
    const queue = bot.queues.get(interaction.guild!.id);

    if (!queue) return interaction.reply({ content: i18n.__("stop.errorNotQueue") }).catch(console.error);

    if (!canModifyQueue(guildMember!)) return i18n.__("common.errorNotChannel");

    queue.stop();

    interaction.reply({ content: i18n.__mf("stop.result", { author: interaction.user.id }) }).catch(console.error);

    queue.connection.destroy();

    bot.queues.delete(interaction.guild!.id);
  }
}

/locales/en.json, add:

    "leave": {
        "description": "Stops the music and disconnects the bot"
    }
NAUTH05 commented 4 months ago

what i need to do after all for that command work?