需要有人帮助我处理这个discord.js代码

问题描述 投票:0回答:1

我有这段代码,可以将消息记录到我的其他服务器上,它工作得很好,但它以消息形式发送它们,例如:“@dyno said: hello”,这就是格式,但我更喜欢将它们以嵌入形式发送,标题为用户谁发送了消息以及消息内容的描述,这是我现有的代码

client.on('messageCreate', message => {


  if (message.author.bot) {
    return;
  }


  console.log("Message received:", message.content); // Checkpoint
  if (message.guild && message.guild.id === 'my_server_id') {
    console.log("Message received from the correct guild."); // Checkpoint
    const destinationChannel = client.channels.cache.get('my_destination_channel_id');
    if (destinationChannel) {
      console.log("Destination channel found."); // Checkpoint
      destinationChannel.send(`**${message.author.tag}** said: ${message.content}`);
    } else {
      console.log(`Destination channel not found in the destination guild.`);
    }
  }
});

discord discord.js
1个回答
0
投票

好的,据我所知,您希望机器人发送给您的消息作为嵌入式消息收到。

还有 stackoverflow 的朋友,我不是专业人士,仍在学习

如果我正确理解这一点并且目前无法测试它,但阅读discord.js 文档应该会得出这样的结论。

// at the top of your file
const { EmbedBuilder } = require('discord.js');

// inside a command, event listener, etc.
const exampleEmbed = new EmbedBuilder()
    .setColor(0x0099FF)
    .setTitle({message.author.tag})
    .addFields(
        { name: 'Message', value: {message.content} },
    )

Channel.send({ embeds: [exampleEmbed] });

如果它有效的话,我希望你的代码会变成这样,

    // at the top of your file
const { EmbedBuilder } = require('discord.js')
client.on('messageCreate', message => {


  if (message.author.bot) {
    return;
  }


  console.log("Message received:", message.content); // Checkpoint
  if (message.guild && message.guild.id === 'my_server_id') {
    console.log("Message received from the correct guild."); // Checkpoint
    const destinationChannel = client.channels.cache.get('my_destination_channel_id');
    if (destinationChannel) {
      console.log("Destination channel found."); // Checkpoint
const exampleEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle({message.author.tag})
.addFields(
    { name: 'Message', value: {message.content} },
)

destinationChannel.send({ embeds: [exampleEmbed] });
    } else {
      console.log(`Destination channel not found in the destination guild.`);
    }
  }
});

© www.soinside.com 2019 - 2024. All rights reserved.