首页 > 解决方案 > 如何制作不和谐机器人以在其他公会中创建邀请

问题描述

我无法让机器人向该机器人所在的另一个公会发出邀请。

我尝试为我的机器人赋予管理员角色以确保它具有权限。

if(msg.content.startsWith('-createinvite')) {
    const args = msg.content.split(' ').slice(1)
    if(!args[0])
        var invitechannels = guild.channels.filter(c => c.members.get(bot.user.id).hasPermission("CREATE_INSTANT_INVITE")).first();
    var guild = client.guilds.get(args[0]);
    if (!guild) return msg.reply("The bot isn't in the guild with this ID.");
    if(!invitechannels) return msg.channel.send('No Channels found with permissions to create Invite in!')
    console.log(invitechannels)
    invitechannels.random().createInvite().then(invite=> msg.channel.send('Found Invite:\n' + invite.code))
}

我尝试了不同类型的代码,但我似乎无法让它工作。

标签: javascriptnode.jsdiscorddiscord.js

解决方案


if(msg.content.startsWith('-createinvite')) {
const args = msg.content.split(' ').slice(1)
let guild = client.guilds.get(args[0]);

if (!guild) return message.reply("The bot isn't in the guild with this ID.");

let invitechannels = guild.channels.filter(c=> c.permissionsFor(guild.me).has('CREATE_INSTANT_INVITE'))
if(!invitechannels) return message.channel.send('No Channels found with permissions to create Invite in!')

invitechannels.random().createInvite()
  .then(invite=> message.channel.send('Found Invite:\n' + invite.code))
}

在获得公会之前,您正在验证机器人是否具有公会权限。这不是一回事c.members.get(bot.user.id).hasPermission("CREATE_INSTANT_INVITE")).first()


推荐阅读