目的:向公会所有者发送消息,然后创建邀请,然后将邀请发送到指定频道。
代码
client.on("guildCreate", async guild => {
// This event triggers when the bot joins a guild.
console.log(``);
console.log(greenBright(`[GUILD JOINED] ${guild.name} | [ID] ${guild.id} | [ (+) MEMBERCOUNT: ${guild.memberCount}]`));
console.log(``);
const Owner = guild.ownerID;
const fetchOwner = client.users.fetch(Owner);
const InviteLogGuild = client.guilds.cache.get("761134473395306507")
const InviteLogChannel = InviteLogGuild.channels.cache.find(ch => ch.id === "803512574573543454");
const getChennel = guild.channels.cache.first();
(await fetchOwner).send(`Thank you for allowing lovell to be added to your server, ${guild.name}, ${(await fetchOwner).username} !\nPrefix:\`${prefix}\`\nAll commands are displayed by saying \`${prefix}help\`\nFeel free to join lovell server: ${MainServer}`)
.then(console.log(cyanBright(`[GUILD OWNER MESSAGED]`)))
.catch(() => {
console.error(redBright(`[GUILD OWNER MESSAGE DEINIED]`));
});
if (!getChennel) {
return console.error(redBright('[CHANNEL COULD NOT BE FETCHED]'));
} else {
console.log(yellow('[CHANNEL FETCHED]'))
}
const link = getChennel.createInvite({
unique: true,
maxAge: 0,
temporary: false,
maxUses: 100
})
if (!link) {
return console.log(redBright(`[INVITE FAILED]`));
} else {
console.log(blueBright(`[INVITE INITIATED]`));
}
if (!InviteLogGuild) {
console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
} else if (InviteLogGuild || !InviteLogChannel) {
console.error(red(`\n[LOG GUILD IDENTIFIED | CHANNEL UNIDENTIFIED]\n`))
} else if (!InviteLogChannel) {
console.error(red(`\n[LOG CHANNEL NOT PRESENT]\n`));
} else if (InviteLogChannel) {
console.log(cyanBright(`[LOGGING INVITE]`));
const LogChannel = new Discord.MessageEmbed()
.setTitle(`New Guild Joined:`)
.setDescription(`**Guild Name:** ${guild.name} \n\n**MemberCount:** ${guild.memberCount} \n\n**Onwer:** \`${(await fetchOwner).tag}\` \n\n**Invite Link:** ${(await link).url}`)
.setThumbnail(`${guild.iconURL({ dynamic: true })}`);
InviteLogChannel.send(LogChannel);
console.log(cyan(`[INVITE LOGGED]\n`));
}
});
输出/错误:
[GUILD JOINED] Ghoulish Hangout | [ID] 618894612769275959 | [ (+) MEMBERCOUNT: 23]
[GUILD OWNER MESSAGED]
[CHANNEL FETCHED]
[INVITE INITIATED]
[LOG GUILD IDENTIFIED | CHANNEL UNIDENTIFIED] <---- Error
错误可能伴随着:
(node:12052) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Channel
我尝试了多种方法来获取不和谐频道,但它不会发送消息或识别频道。任何帮助将不胜感激;)
答案 0 :(得分:1)
我不太清楚为什么这会起作用,但我从 if 语句中取出了我的消息命令并且它起作用了。
if (!InviteLogGuild) {
return console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`));
} else if (InviteLogGuild) {
console.log(greenBright(`[LOG GUILD IDENTIFIED]`));
} else if (!InviteLogChannel) {
return console.error(red(`\n[LOG CHANNEL NOT PRESENT]\n`));
} else if (InviteLogChannel) {
console.log(cyanBright(`[LOG CHANNEL IDENTIFIED]`));
}
console.log(cyanBright(`[LOGGING INVITE]`));
const SendMessage = new Discord.MessageEmbed()
.setTitle(`New Guild Joined:`)
.setDescription(`**Guild Name:** ${guild.name} \n\n**MemberCount:** ${guild.memberCount} \n\n**Onwer:** \`${(await fetchOwner).tag}\` \n\n**Invite Link:** ${(await link).url}`)
.setThumbnail(`${guild.iconURL({ dynamic: true })}`);
InviteLogChannel.send(SendMessage);
console.log(cyan(`[INVITE LOGGED]\n`));
结果:
[GUILD JOINED] test | [ID] 2389372187328783 | [ (+) MEMBERCOUNT: 9]
[GUILD OWNER MESSAGED]
[CHANNEL FETCHED]
[INVITE INITIATED]
[LOG GUILD IDENTIFIED]
[LOGGING INVITE]
[INVITE LOGGED]
如果有人能解释为什么会这样,请随意!
答案 1 :(得分:0)
编辑:
你的 if 语句是错误的:
if (!InviteLogGuild) {
console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
} else if (InviteLogGuild || !InviteLogChannel) {
这是错误的,它总是会进入这种情况,因为您正在检查InviteLogGuild是否已定义或 InviteLogChannel是否未定义。由于第一种情况已经检查了要定义的 InviteLogGuild,因此这将始终返回 true
。要使其正常工作,您必须将其更改为:
if (!InviteLogGuild) {
console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
} else if (InviteLogGuild && !InviteLogChannel) {
但是您可以完全删除 &&
,因为第一种情况已经检查了 InviteLogGuild 是否已定义:
if (!InviteLogGuild) {
console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
} else if (!InviteLogChannel) {
如果您将这些因素考虑在内,您最终的“if-chain”将如下所示(并且有效):
if (!InviteLogGuild) {
console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`));
} else if (!InviteLogChannel) {
console.error(red(`\n[LOG GUILD IDENTIFIED | CHANNEL UNIDENTIFIED]\n`));
} else {
console.log(cyanBright(`[LOGGING INVITE]`));
const LogChannel = new Discord.MessageEmbed()
.setTitle(`New Guild Joined:`)
.setDescription(`**Guild Name:** ${guild.name} \n\n**MemberCount:** ${guild.memberCount} \n\n**Onwer:** \`${(await fetchOwner).tag}\` \n\n**Invite Link:** ${(await link).url}`)
.setThumbnail(`${guild.iconURL({ dynamic: true })}`);
InviteLogChannel.send(LogChannel);
console.log(cyan(`[INVITE LOGGED]\n`));
}
为了避免此类问题,我建议您改用 TypeScript 或开始使用 ESLint,它可以建议您进行不必要的检查。
旧答案
尝试使用 guild.channels.resolve(channelId)
和 client.guilds.resolve(guildId)
而不是使用缓存。可能是频道还没有加载到缓存中,找不到。