发送消息到频道

时间:2019-09-13 18:43:07

标签: javascript node.js discord.js

我有一个发送消息的功能,但由于它不在内部而无法正常工作

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

我该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以从外部client.on('message'...,发送消息,您只需要频道ID,然后您就可以这样做:

#define MAX_NUM_LIGHTS 5
struct R3D_DirectionalLight
{
    Vec3 direction;
    float padding1;
    Vec3 color;
    float padding2;
    Vec3 ambient;
    float padding3;
    float strength;
    Vec3 padding4;
};

struct R3D_DirectionalLights
{
    int numDirectionalLights;
    char padding1[sizeof(Vec4) - sizeof(int)] = {0};
    R3D_DirectionalLight directionalLights[MAX_NUM_LIGHTS];
};

编辑 您可以通过将其作为参数传递来在函数内部使用它:

const myChannel = client.channels.get('CHANNEL_ID');
myChannel.send(`your message here!`);

现在您可以使用myChannel调用函数:

const myChannel = client.channels.get('CHANNEL_ID');
function myFunc(channel) {
  channel.send('MESSAGE');
}

答案 1 :(得分:0)

client.on("message")是一个事件,该事件每次在机器人可以查看的频道中发送新消息时触发。 (包括DM。)

要将消息发送到频道ID,首先必须使用client.channels集合查找频道。

const Discord = require("discord.js");
const Client = new Discord.Client(); // Creating a new Discord Client.

Client.on("ready", () => {
  const Channel = Client.channels.get("your channel id"); // Searching the channel in the collection channels.
  if (!Channel) {
    return console.error("No channel found!"); // If the channels does not exist or the bot cannot see it, it will return an error.
  } else {
    return Channel.send("Your message to send here!"); // We found the channel. Sending a message to it.
  };
}); // Making sure the client is ready before sending anything.

Client.login("your bot token here"); // Logging into Discord with the bot's token.