[英]How can I implement a !leaveguild <guildID> command (that leaves the specified guild) into my Discord (discord.js) bot?
[英]Discord.js how can I create an invite for every guild my bot is in?
我正在尝试发出命令,在那里我可以获得该机器人当前所在的每个公会邀请。当前代码:
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);
});
例如,这是我运行命令后得到的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.