【问题标题】:How to get server id and channel id in discord.js using command?如何使用命令在 discord.js 中获取服务器 ID 和频道 ID?
【发布时间】:2021-06-18 12:59:40
【问题描述】:

我试图使用一个命令保存公会 ID 和频道 ID,使用 lowdb,但是当我使用 var server = client.guilds.get(message.guild.id).id; 时,我收到错误消息,无法读取未定义的属性“获取”。我不知道为什么。

var server = client.guilds.get(message.guild.id).id;
var channel = client.guilds.get(message.channel.id).id;

db.get('posts')
  .push({ id: server, title: channel})
  .write();

我编写了所有代码,我在代码开始时定义了所有内容

const client = require('<there's path but I don't want to show it cuz there's my name>/my_bot.js');
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')
const db = low(adapter)

如果你知道出了什么问题,请帮忙

【问题讨论】:

  • 您好,您使用的是哪个版本的 discord.js?您确定在 ready 事件之后运行该代码?
  • 我正在使用 v12 discord.js 我想这可能是它的问题,但我不确定如何修复它以及如何正确设置它,我试图在文档中找到解决方案,但我我不能只找到 .id 以及如何使用它,但它并没有解决我的问题。是的,因为我正在使用命令处理程序,并且我在文件中都有所有 const 以及激活它的命令,并且在主文件中

标签: javascript discord.js


【解决方案1】:

如果您使用的是 discord.js@v12,那么您应该记住,您应该通过 .cache 属性才能使您的代码正常工作。您可以通过查看Client.guilds 的文档自行检查。
此外,您不需要从他们的 ID 中获取频道和公会,因为您已经将它们存储为消息属性。
以下是我的做法:

let server = message.guild.id, // ID of the guild the message was sent in
  channel = message.channel.id // ID of the channel the message was sent in

公平地说,我无法解释您为什么会收到该错误。

【讨论】:

  • 感谢一切正常,现在我只需要弄清楚何时以及如何替换现有“帖子”的数据,这样就不会在每次有人设置频道时创建新帖子,并向一台服务器上有两个不同的频道
【解决方案2】:

好的,所以你得到“get”不是一个函数,因为在 v12 中它必须是 client.guilds.cache.get(whatever you wanna put here)

【讨论】:

    猜你喜欢
    • 2021-04-10
    • 2020-07-26
    • 2021-05-04
    • 2020-09-08
    • 2021-10-19
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多