gpt4 book ai didi

javascript - Discord.js 如何为我的机器人所在的每个公会创建邀请?

转载 作者:行者123 更新时间:2023-12-02 16:30:43 25 4
gpt4 key购买 nike

我正在尝试发出一个命令,在那里我可以获得机器人当前所在的每个公会邀请。当前代码:

client.on('message', async (message) => {
if (message.content.startsWith(prefix + 'invite')) {
let invite = client.guilds
.createInvite({
maxAge: 0, // 0 = infinite expiration
maxUses: 0, // 0 = infinite uses
})
.catch(console.error);
message.channel.send(invite);
}
});

错误:

DiscordAPIError: Cannot send an empty message

最佳答案

试试这个:

 var invites = []; // starting array
message.client.guilds.cache.forEach(async (guild) => { // iterate loop on each guild bot is in

// get the first channel that appears from that discord, because
// `.createInvite()` is a method for a channel, not a guild.
const channel = guild.channels.cache
.filter((channel) => channel.type === 'text')
.first();
if (!channel || guild.member(client.user).hasPermission('CREATE_INSTANT_INVITE') return;
await channel
.createInvite({ maxAge: 0, maxUses: 0 })
.then(async (invite) => {
invites.push(`${guild.name} - ${invite.url}`); // push invite link and guild name to array
})
.catch((error) => console.log(error));
console.log(invites);
});

例如,这是在运行命令后得到的:

Discord Example


关于javascript - Discord.js 如何为我的机器人所在的每个公会创建邀请?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63536924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com