我正在使用discord-jda创建一个discord机器人,并且我想创建一个eval命令,该命令使用discord服务器中的消息执行代码?
答案 0 :(得分:0)
要从聊天中读取消息,您需要一个EventListener
public class Test implements EventListener {
@Override
public void onEvent(GenericEvent event){
//First you check if this event is whatever event you're listening for
if(event instanceof MessageReceivedEvent){
//Do whatever
//Note that you can cast that event here, since it 100% is a MessageReceivedEvent
MessageReceivedEvent messageReceivedEvent = (MessageReceivedEvent) event;
//Or to operate directly
TextChannel channel = ((MessageReceivedEvent)event).getChannel();
}
}
}
您可以在这里https://github.com/DV8FromTheWorld/JDA/wiki/1)-Events
找到相关信息然后,您可以使用基本的Java处理该事件并执行所需的任何操作,之后,如果您想将内容发送回去,则需要引用TextChannel
(MessageReceivedEvent具有getChannel()
简便的方法),您可以在其上使用sendMessage()
。
请注意,这只是一个概述,可帮助您正确定位。很难根据您的要求给出更具体的信息,如果您使用更多信息来编辑答案,我也会很乐意在此处进行更新。