所以,我写了 this 来让机器人发送关于它加入网络钩子的服务器的信息,比如服务器名称和所有者 ID 等等,但我想在机器人的嵌入中添加一个字段如果它有权限,则将服务器邀请附加到它刚刚加入的服务器,如果不只是将其替换为:“缺少权限”,我搜索了如何执行此操作,但我没有找到太多,我无法弄清楚,我也有这个代码在命令表单中,我在其中输入服务器 ID,机器人直接发送相同的嵌入,所以无论谁回答这个问题,请记住,每当我执行命令时,我都需要机器人生成新的邀请,然后不要不要使用任何以前的邀请,为此我需要邀请的有效期为 7 天,谢谢。
片段:
/**
* Starts checking...
* @param {object} client The Discord Client instance
*/
init(client) {
client.on("guildCreate", (guild) => {
let channel = new Discord.WebhookClient(
client.config.webhook.id,
client.config.webhook.token
);
const embed = new Discord.MessageEmbed()
.setColor(client.config.embed.color)
.setThumbnail(guild.iconURL({ dynamic: true }))
.setTitle("New Server!")
.addField("Server Name", guild.name, true)
.addField("Server ID", guild.id, true)
.addField("Owner ID", guild.ownerID, true)
.addField("Owner Mention", `<@${guild.ownerID}>`, true)
.addField("Member Count", guild.memberCount, true)
.setFooter(client.user.username, client.config.embed.thumbnail);
channel.send(embed);
});
答案 0 :(得分:2)
实际上,invites are not for the server, but for the channel。
<块引用>此代码显示了如何为机器人找到的第一个频道(不一定是频道列表中的第一个频道)创建邀请:
guild.channels.cache.first()
.createInvite() //you can add {options} if you want
.then(invite => embed.addField("Invite link", invite.url, true)) // if promise is accepted
.catch(() => embed.addField("Invite link", "Missing permissions")) // if promise is rejected (90% because you don't have permissions, 10% because of server lag etc., either way you don't get the link)
由于创建邀请可能比发送嵌入要长,我建议添加一些异步操作。 整体解决方案:
client.on("guildCreate", async (guild) => {
let channel = new Discord.WebhookClient(
client.config.webhook.id,
client.config.webhook.token
);
const embed = new Discord.MessageEmbed()
.setColor(client.config.embed.color)
.setThumbnail(guild.iconURL({ dynamic: true }))
.setTitle("New Server!")
.addField("Server Name", guild.name, true)
.addField("Server ID", guild.id, true)
.addField("Owner ID", guild.ownerID, true)
.addField("Owner Mention", `<@${guild.ownerID}>`, true)
.addField("Member Count", guild.memberCount, true)
.setFooter(client.user.username, client.config.embed.thumbnail);
await guild.channels.cache
.first()
.createInvite()
.then((invite) => embed.addField("Invite link", invite.url, true))
.catch(() => embed.addField("Invite link", "Missing permissions", true));
channel.send(embed);
});
最后提示:
您无需检查所有者 ID,因为可以通过他的提及轻松访问。
编辑:
回复您的评论,我自己进行了一些调试,但不断收到 UKNOWN CHANNEL
错误。出于某种奇怪的原因,创建指向 category
频道的邀请链接不起作用(尽管 documentation 另有声明)。
新代码片段:
client.on("guildCreate", async (guild) => {
let channel = new Discord.WebhookClient(
client.config.webhook.id,
client.config.webhook.token
);
const embed = new Discord.MessageEmbed()
.setColor(client.config.embed.color)
.setThumbnail(guild.iconURL({ dynamic: true }))
.setTitle("New Server!")
.addField("Server Name", guild.name, true)
.addField("Server ID", guild.id, true)
.addField("Owner ID", guild.ownerID, true)
.addField("Owner Mention", `<@${guild.ownerID}>`, true)
.addField("Member Count", guild.memberCount, true)
.setFooter(client.user.username, client.config.embed.thumbnail);
await guild.channels.cache
.filter(channel => channel.type === "text") //added this line, should work like a charm
.first()
.createInvite()
.then((invite) => embed.addField("Invite link", invite.url, true))
.catch(() => embed.addField("Invite link", "Missing permissions", true));
channel.send(embed);
});