簡體   English   中英

Discord.js bot 離開公會

[英]Discord.js bot leave guild

如何讓我的機器人離開公會? 我試過這個:

case `leave`:
    if(message.author.id !=='196701848239865866') 
        return message.channel.send(`**»** ${message.author}, you don't have permission to do that!`);
    var guildID = bot.guild.find()
    guildID.leave()
    break;

您不能通過從公會 id 運行.leave()離開公會。 我建議做message.guild.leave(); 離開公會。 如果您需要公會 ID,您可以通過執行message.guild.id從消息中獲取。

為了擴展您的代碼不起作用的原因,您做錯了一些事情。

bot.guild不存在,因此執行bot.guild.find()也不起作用。 機器人被設計在多個公會中,因此它們存儲在一個集合中,您可以在其中通過執行bot.guilds.find("id", guildId);來搜索它們bot.guilds.find("id", guildId); . 此外,一旦你得到了“id”,它就會作為一個Snowflake返回,因此它沒有方法,你不能運行.leave(); 從它。

在 discord.js v12 中,您必須使用

bot.guilds.cache.get(id).leave()
.catch(err => {
    console.log(`there was an error leaving the guild: \n ${err.message}`);

我認為這就是它的工作原理! 如果您有任何問題,請隨時 hmu!

添加到 Splinxyy 的答案,而不是bot.guild.find(); (因為它不存在)嘗試message.guild

就像他們上面說的那樣, message.guild.leave()會離開發送消息的當前服務器。 但是,您也可以對 forEach 算法使用類似的方法。 這是一個例子: bot.guilds.cache.forEach(guild => {guild.leave()})這將留下你的機器人所在的所有服務器。如果你想用你的機器人離開一個特定的服務器,你可以使用像這樣: bot.guild.leave(server id here)

通過查看您的代碼,我會說,請執行以下操作:

case `leave`:
    if(message.author.id !=='196701848239865866') 
        return message.channel.send(`**»** ${message.author}, you don't have permission to do that!`);
    var guildID = bot.guilds.cache.get(args[0]) //your argument would be the server id
    guildID.leave()
    break;

如果您想讓消息離開服務器,請執行以下操作:

    if(message.author.id !=='196701848239865866') 
        return message.channel.send(`**»** ${message.author}, you don't have permission to do that!`);
message.guild.leave()

希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM