Discord JS获取当前频道ID

时间:2020-09-06 03:47:46

标签: discord.js

是否有办法获取发送消息的当前频道的ID?我想将频道ID存储在我已经设置的变量中。只需获取发送我的消息的频道的ID

3 个答案:

答案 0 :(得分:0)

使用message.channel.id,当您收到由client.on("message", message => {})事件引起的消息时,可以从其属性调用message.channel.id,有关更多说明,请阅读文档{{3} }。一个例子是:

client.on("message", message => {
  // The channel id can be received through `message.channel.id` and is stored in the variable `messageChannelId`
  const messageChannelId = message.channel.id;
  // This will log in the console the id of the channel that it's being sent from
  console.log(messageChannelId);
})

如果您想了解message事件的构造方式,请改用:

client.on("message", message => {
  // Logs in the console the `message` property
  console.log(message);
})

如果您想查看message.channel属性的外观,请使用:

client.on("message", message => {
  // Logs in the console the `message.channel` prroperty
  console.log(message.channel);
})

答案 1 :(得分:0)

您可以调用channel对象的message属性(指消息发送到的通道)。从那里,您可以访问整个TextChannel对象!

message.channel.name; // name of channel
message.channel.guild; // guild the channel's in
message.channel.topic; // topic of the channel
message.channel.parent; // category of which the channel is in

message.channel.id; // and finally, the id of the channel

答案 2 :(得分:-1)

对于消息事件,请使用消息对象并获取其公会。 像这样:

public T GetItemAsync(string id)
{
    IQueryable<T> queryable = container.GetItemLinqQueryable<T>(true);
    queryable = queryable.Where<T>(item => item.Id == id);
    return queryable.ToArray().FirstOrDefault();
}

以下是client.on('message', async message => { var channel = message.channel; }) 的文档:https://discord.js.org/#/docs/main/stable/class/Channel