提交 onMessageReceived/onSlashCommand 事件时如何在 JDA 中获取公会 ID

How to get guild ID in JDA when committing onMessageReceived/onSlashCommand event

我试过用这个方法:

    event.getGuild().getId();

但我总是得到 null。 (貌似Guild接口中根本就没有实现这个方法)

如何获取发生此事件或那个事件的公会 ID?

P.S。我需要公会 ID,这是在 json

中存储数据所必需的

确保您收到的消息来自公会而不是私人消息,使用 event.isFromGuild() returns 如果消息/命令是从公会发送的,则为真,或者您可以使用 event.getChannelType() 获取发送消息的频道类型。

如果您只需要公会消息而不需要私人消息,您也可以使用 onGuildMessageReceived 而不是 onMessageReceived,或者如果您只需要私人消息,则可以使用 onPrivateMessageReceived