【问题标题】:How can I embed messages ,in multiple channels, using a Discord bot?如何使用 Discord 机器人在多个渠道中嵌入消息?
【发布时间】:2021-03-21 19:33:32
【问题描述】:

我在其他用户的问题中寻找并找到了我正在搜索的内容。我的问题是我希望同一个机器人在具有不同标题、页脚等的不同频道上执行相同的工作......

主要代码是:

const Discord = require("discord.js");

const client = new Discord.Client();

const channel = client.channels.cache.get("channel id");

client.on("ready", async () => {
    console.log(`Bot has started`);

    client.user.setPresence({ activity: { name: "some status " }, status: "online" });
});
client.on("message", (message) => {
    if (message.author.bot) 
    return;
    const embed = new MessageEmbed()
    .setDescription(message.content)
    .setTitle("")
    .setAuthor(message.author.tag,message.author.displayAvatarURL())
    .setImage(message.attachments.first().url)
    .setFooter("", "")
    .setTimestamp();
    channel.send(embed).catch(console.error);
});

client.login("token");

【问题讨论】:

  • 能否请您链接上述问题?这将有助于我们更好地了解您的问题。另外,您提供的代码是否遇到任何错误?
  • 当然在这里!非常感谢你 ! stackoverflow.com/questions/64072895/…@Jakye
  • 不,我没有错误

标签: javascript node.js discord discord.js bots


【解决方案1】:

首先,您可以在对象中声明您的消息,然后使用它们发送到不同的渠道。

    //Here goes the dynamic setting for different channels
    const messages = {
      'IdChannel':{
        embed:{
          title:'foo',
          description: message.content,
          footer: {text:'foo'}
        }
      },
      'scndChannelID':{
        embed:{
          title:'bar',
          description: message.content,
          footer: {text:'bar'}
        }
      }
    }

然后在事件消息中使用它发送到您需要在对象键上声明的通道

      Object.keys(messagex).forEach((value,index) => {
        //Here can go the static things
        message.guild.channels.cache.get(value).send(
          {embed:{...messagex[value].embed,
          timestamp: new Date(),
          image: {url:""}
          }})
      })

【讨论】:

  • 从那以后我一直在尝试此代码,但是当有人尝试附加图像时,它会发送一条空白消息!
  • 你把图片的url改成你需要的了吗?
  • 什么意思?它不是一个特定的图像。在这个特定的频道中,每个用户都会上传图片。所以我需要找到一种方法来为图像设置自定义代码,比如 ```message.content ``` 非常适合短信!
  • 所以只需像在 coe 中所做的那样更改 url:message.attachments.first().url
猜你喜欢
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 2019-09-16
  • 2019-09-12
  • 2021-07-20
  • 2019-02-20
  • 2020-08-24
相关资源
最近更新 更多