【问题标题】:Trouble making a command to force bot to leave a server. (Discord,js) [duplicate]无法发出强制机器人离开服务器的命令。 (不和谐,js)[重复]
【发布时间】:2021-11-25 02:11:36
【问题描述】:
const Discord = require('discord.js')
const client = new Discord.Client({ws: {intents: Discord.Intents.ALL}});
const botowner = 'xxx'

exports.run = async (bot,message,args) => {
   let guildid = message.guild.id
 if (message.author.id = botowner) {
    toleave = client.get_server(guildid)
    await client.leave_server(toleave)
 } else {message.channel.send("You are not the bot owner")}
    
}

exports.help = {
    name: ['leavediscordserver']
    }

每当我运行此代码时,都会显示以下内容:

不知道为什么会这样。

【问题讨论】:

  • 首先 if (message.author.id = botowner) 这不是一个只有 1 个 = 的 if 语句。这意味着get_server 在client 上不存在
  • 这能回答你的问题吗? Discord.js bot leave guild
  • “不知道为什么会这样。”我对您为什么期望它起作用感到困惑。查看clienthere 的官方文档。 djs v12 或 v13 中没有 get_server() 方法或 leave_server() 方法。您不能只是编造一个方法名称并期望它起作用。查看文档并使用执行此操作所需的实际方法。您必须通过client.guilds.cache 访问公会缓存,然后从那里获取公会,如答案所示,如下所示:client.guilds.cache.get(guildid)。

标签: javascript discord.js


【解决方案1】:

错误信息告诉你所有你需要知道的。您尝试使用名为get_server() 的函数,但该函数不存在,因此您不能使用它。

Discord.js 中的不和谐服务器被称为公会,因此要获得您的公会,您只需调用 const guild = client.guilds.cache.fetch(guildid) 并留下它,您只需调用 guild.leave()。

作为一个单独的问题,if 语句中的比较使用 2 个等号 == 而不是 1,就像您在 if (message.author.id = botowner) 行中所做的那样。

【讨论】:

  • 你错了,因为在 v12 或更高版本中,您还必须在 fetch 之前添加 cache
  • @MegaMix_Craft 废话,你是对的。一定是打字太快了哈哈。
  • 没事的,不用担心=3
【解决方案2】:
var guildID = client.guilds.cache.get(args[0]) //your argument would be the server id
    if (message.author.id == botowner) { // use == to check if author and bot owner id match
    await guildID.leave() // waiting for bot to leave the server
 } else {message.channel.send("You are not the bot owner")}
    
}

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 2020-11-07
    • 1970-01-01
    • 2018-07-21
    • 2018-11-10
    • 2020-07-09
    • 2021-02-01
    • 2021-06-25
    • 2017-05-31
    相关资源
    最近更新 更多