(JDA)我如何在同一个方法中检查两种不同类型的事件?

huangapple go评论62阅读模式
英文:

(JDA) How do I check for two different event types in the same method?

问题

我知道这听起来非常简单和愚蠢,但老实说我刚接触Java,对于这个问题感到困惑。

我有一个名为Reactions的类,检查反应,以及一个名为onGameStart的方法,启动了我制作的(或将要制作的)一个“游戏”。

@Override
public void onMessageReactionAdd(MessageReactionAddEvent e) {
    Game start = new Game();
    TextChannel pOneChannel = e.getGuild().getTextChannelsByName("player-1", true).get(0);
    TextChannel pTwoChannel = e.getGuild().getTextChannelsByName("player-2", true).get(0);
    
    if (e.getTextChannel().getIdLong() == channelID) {
        pOneChannel.sendMessage(playerOne()).queue();
        pTwoChannel.sendMessage(playerTwo()).queue();
    }
    if (e.getTextChannel() == pOneChannel) {
        if (e.getTextChannel() != pTwoChannel)
            printMessage("Waiting for players...", e);
        else
            start.onGameStart();
    } else if (e.getTextChannel() == pTwoChannel) {
        if (e.getTextChannel() != pOneChannel)
            printMessage("Waiting for players...", e);
        else
            start.onGameStart();
    }    
}

之前,我的Game类需要一个MessageReceivedEvent。但是,当然这意味着我需要将其作为上述类的参数。当将其添加到上述类时,我就不能再覆盖它,而且它也不起作用。
所以我让我的Game类有一个使用MessageReceivedEvent运行的方法,只需传入一个空值。但是,当然这并不起作用,我需要一个真实的值。有人知道如何实现这一点吗?

另外,我还有另一个错误,我想在玩家1和玩家2的频道中检查表情符号的响应。当两者都有响应时,开始游戏。我尝试在上面编写了这个逻辑,但每次都只会运行“正在等待玩家…”。任何帮助都将不胜感激,谢谢。

英文:

I know this sounds super simple and dumb but honestly im new to java and am having trouble figuring this out.

I have a class Reactions that checks for a react and a method onGameStart that startups a "game" i made (or will make)

@Override
public void onMessageReactionAdd(MessageReactionAddEvent e) {
    Game start = new Game();
	TextChannel pOneChannel = e.getGuild().getTextChannelsByName("player-1",true).get(0);
	TextChannel pTwoChannel = e.getGuild().getTextChannelsByName("player-2",true).get(0);
	
	if(e.getTextChannel().getIdLong() == channelID) {
		pOneChannel.sendMessage(playerOne()).queue();
		pTwoChannel.sendMessage(playerTwo()).queue();
	}
	if(e.getTextChannel() == pOneChannel) {
		if(e.getTextChannel() != pTwoChannel)
			printMessage("Waiting for players...", e);
		else
			start.onGameStart();
	}
	else if(e.getTextChannel() == pTwoChannel) {
		if(e.getTextChannel() != pOneChannel)
			printMessage("Waiting for players...", e);
		else
			start.onGameStart();
	}	
}

Before, I had the class Game require a MessageReceivedEvent. But of course this would mean I would need that as a parameter in the above class. When adding it to the above class, I can no longer override and it doesnt work.
So then I made my Game class have a method that runs with a MessageReceivedEvent that is just passed in with a null value. But of course this doesnt work, I need a real value. Does anyone know how to accomplish this?

Also another error I have is that I'd like to check for an emote response in the player 1 and player two channels. When there exists a response in both, start the game. I tried to write that above but it just runs Waiting for players.. every time. Any help is appreciated, thank you.

答案1

得分: 3

你不能使用一个方法处理多个事件类型。很简单。事件结构调用每个单独事件上的通用方法 (EventListener#onEvent(GenericEvent e))。这包括接收消息或新的反应等所有内容。

不过,我假设你的监听器扩展了ListenerAdapter类。这个类实现了一些将所有事件拆分成单独方法的逻辑。这些方法是你要重写的。这些方法仅用于单一事件(虽然有一些事件的超类型)。如果你想在两种不同的事件类型上执行某些操作,基本上有两种选择:

  • 你可以使用GenericEvent并自己解析所有内容(我不建议这样做,但是是可能的)。
  • 你重写所有需要的事件并调用如下所示的辅助方法:
@Override
public void onMessageReceivedEvent(MessageReceivedEvent event) {
    utilMethod(/*添加参数*/);
}

@Override
public void onMessageReceivedEvent(MessageReceivedEvent event) {
    utilMethod(/*添加参数*/);
}

private void utilMethod(/*添加参数*/) {
    // 做一些操作
}

然而,这些方法仍然会为每个事件单独调用。假设你和朋友在完全相同的时间发送了两条消息(理论上)。JDA仍然会触发两次方法,每次提供每条消息的信息。如果你想进行准备检查(因为我假设你想做类似的操作),你需要保存你的游戏状态并根据你接收到的消息来更新它。

例如,你可以有一个类似这样的玩家类:

public class Player {
    long id; // 代表 Discord ID
    boolean ready = false; // 根据消息更新这个字段
    // ... 一些其他的东西
}

然后,在接收事件时更新Player#ready字段,然后检查是否所有玩家都准备好了。

希望这有助于你更好地理解JDA的事件架构:D

英文:

You can not process multiple Event-Types with one method. It's that simple. The Event Structure calls a generic method (EventListener#onEvent(GenericEvent e)) on each individual Event. That includes everything, like receiving a message or a new reaction.

However I assume your Listener extends the ListenerAdapter-Class. This class implements some logic that splits all events in seperate methods. These methods are the one you are overriding. These are only provided for Singular Events (although there are some supertypes of events). If you want something to happen on two different event types you have basically two options:

  • You use the GenericEvent and parse everything yourself (I do not recommend doing this, but it is possible)
  • You override all Events you need and call a helper Method as shown below:
@Override
public void onMessageReceivedEvent(MessageReceivedEvent event) {
    utilMethod(/*add Args here*/);
}

@Override
public void onMessageReceivedEvent(MessageReceivedEvent event) {
    utilMethod(/*add Args here*/);
}

private void utilMethod(/*add Args here*/) {
    // do stuff
}

However these methods are still called for every event individually. Assume you and a friend send two messages at the exact same time (theoretically). JDA would still trigger the method two times with information for each message individually. If you want to do a ready check (since I assume you want to do something like that) you have to save the state of your game and update it depending on the messages you receive.

E.g. you could have a class player like this:

public class Player {
    long id; // Represents the Discord ID
    boolean ready = false; // Update this on a message
    // ... some other stuff
}

and then update the Player#ready field when you receive the event and then check if all players are ready.

Hope this helps you understand the Event-Architecture of JDA a little better (JDA)我如何在同一个方法中检查两种不同类型的事件?

huangapple
  • 本文由 发表于 2020年9月18日 10:23:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63948464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定