首页 > 解决方案 > Discord JS V13 无法向公会所有者发送消息

问题描述

当机器人加入服务器时,我正在尝试向公会所有者发送消息:

client.on('guildCreate', guild => {
    const introembed = new MessageEmbed()
        .setTitle('Hiya!')
        .setColor('RANDOM')
        .setDescription(`Thank you for adding me to your server!\nRun \`${prefix}help\` to get my commands!\nThings to know: I am still under developement, and will have a few bugs, feel free to report them with \`${prefix}bugreport\`\nMy GitHub can be found here: https://github.com/*********/*****`)
        guild.fetchOwner().then(send({ embeds: [introembed]}).catch(console.error()))
})

这确实有效,从 discord.js v13 开始,我不能再做guild.owner.send(). 我如何在 v13 中做到这一点?

标签: node.jsdiscord.js

解决方案


您快到了。您只是为您的.then(). 我建议使用asyncand await

试试这个:

client.on("guildCreate", async (guild) => {
  const owner = await guild.fetchOwner();
  owner.send("This is a test message to the owner of this guild!");
});

或者,如果您想坚持.then()使用语法,请使用:

client.on("guildCreate", async (guild) => {
  guild.fetchOwner().then((owner) =>
    owner.send({
      embeds: [
        new MessageEmbed({
          title: "Hiya!",
          color: "RANDOM",
          description: "This is a test message to the owner of this guild!",
        }),
      ],
    })
  );
});

无论您选择哪种方式,在运行代码时,您都会收到这样的不和谐 DM: DM给公会老板


推荐阅读