我正在用JDA做一个不和谐的机器人,我想知道如何等待消息。像这样
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class list extends ListenerAdapter {
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String message = event.getMessage().getContentRaw();
boolean isBot = event.getAuthor().isBot();
// Check if message is not from a bot
if (!isBot) {
if (message.equalsIgnoreCase("hi")) {
event.getChannel().sendMessage("Hello, what's your name?").queue();
// Wait for second message
String name = event.getMessage().getContentRaw(); // Saving content from second message
event.getChannel().sendMessage("Your name is: " + name).queue();
}
}
}
}
用户:您好
Bot:您好,您叫什么名字?
用户:发送名称
Bot:您的名字是:名称
我如何获得第二条消息?
答案 0 :(得分:1)
Minn在评论中已经指出。您可以使用线程中给定的方法。但是,我建议使用JDA-Utilities EventWaiter。
如果您使用的是Maven或Gradle,请使用以下任一选项:
<!--- Place this in your repositories block --->
<repository>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
<!--- Place this in your dependencies block --->
<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>jda-utilities</artifactId>
<version>JDA-UTILITIES-VERSION</version> <!--- This will be the latest JDA-Utilities version. Currently: 3.0.4 --->
<scope>compile</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId> <!--- This will be your JDA Version --->
<version>JDA-VERSION</version>
</dependency>
对于gradle来说,它要容易得多
# Add this to the dependencies (What's inside this block, not the block itself.
dependencies {
compile 'com.jagrosh:jda-utilities:JDA-UTILITIES-VERSION'
compile 'net.dv8tion:JDA:JDA-VERSION'
}
# Same story for the repo's
repositories {
jcenter()
}
加载完毕后,建议您检查ExampleBot main class.以便添加自己的内容。因为我将仅说明使EventWaiter正常工作的基础。
这将是开始的机器人的主要类别,当您运行它时,它将使用以下命令启动机器人。 ShutdownCommand和PingCommand是实用程序的一部分。因此,这些将自动添加。
public static void main(String[] args) throws IOException, LoginException, IllegalArgumentException, RateLimitedException
{
// the first is the bot token
String token = "The token of your bot"
// the second is the bot's owner's id
String ownerId = "The ID of your user account"
// define an eventwaiter, dont forget to add this to the JDABuilder!
EventWaiter waiter = new EventWaiter();
// define a command client
CommandClientBuilder client = new CommandClientBuilder();
// sets the owner of the bot
client.setOwnerId(ownerId);
// sets the bot prefix
client.setPrefix("!!");
// adds commands
client.addCommands(
// command to say hello
new HelloCommand(waiter),
// command to check bot latency
new PingCommand(),
// command to shut off the bot
new ShutdownCommand());
// start getting a bot account set up
new JDABuilder(AccountType.BOT)
// set the token
.setToken(token)
// set the game for when the bot is loading
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.setActivity(Activity.playing("loading..."))
// add the listeners
.addEventListeners(waiter, client.build())
// start it up!
.build();
}
现在,为HelloCommand。首先创建一个名为“ HelloCommand”的新类。在此类中,您将扩展实用程序的“命令”部分。
public class HelloCommand extends Command
{
private final EventWaiter waiter; // This variable is used to define the waiter, and call it from anywhere in this class.
public HelloCommand(EventWaiter waiter)
{
this.waiter = waiter; // Define the waiter
this.name = "hello"; // The command
this.aliases = new String[]{"hi"}; // Any aliases about the command
this.help = "says hello and waits for a response"; // Description of the command
}
@Override
protected void execute(CommandEvent event)
{
// ask what the user's name is
event.reply("Hello. What is your name?"); // Respond to the command with a message.
// wait for a response
waiter.waitForEvent(MessageReceivedEvent.class,
// make sure it's by the same user, and in the same channel, and for safety, a different message
e -> e.getAuthor().equals(event.getAuthor())
&& e.getChannel().equals(event.getChannel())
&& !e.getMessage().equals(event.getMessage()),
// respond, inserting the name they listed into the response
e -> event.reply("Hello, `"+e.getMessage().getContentRaw()+"`! I'm `"+e.getJDA().getSelfUser().getName()+"`!"),
// if the user takes more than a minute, time out
1, TimeUnit.MINUTES, () -> event.reply("Sorry, you took too long."));
}
}
现在,这是我们了解多汁的东西的地方。使用事件服务程序时。您需要检查一些区域。
// wait for a response
waiter.waitForEvent(MessageReceivedEvent.class,
// make sure it's by the same user, and in the same channel, and for safety, a different message
e -> e.getAuthor().equals(event.getAuthor())
&& e.getChannel().equals(event.getChannel())
&& !e.getMessage().equals(event.getMessage()),
// respond, inserting the name they listed into the response
e -> event.reply("Hello, `"+e.getMessage().getContentRaw()+"`! I'm `"+e.getJDA().getSelfUser().getName()+"`!"),
// if the user takes more than a minute, time out
1, TimeUnit.MINUTES, () -> event.reply("Sorry, you took too long."));
详细解释:
waiter.waitForEvent(GuildMessageReceivedEvent.class, // What event do you want to wait for?
p -> p.getAuthor().equals(message.getAuthor()) && p.getChannel().equals(message.getChannel()), // This is important to get correct, check if the user is the same as the one who executed the command. Otherwise you'll get a user who isn't the one who executed it and the waiter responds to it.
run -> {
// Run whatever stuff you want here.
}, 1, // How long should it wait for an X amount of Y?
TimeUnit.MINUTES, // Y == TimeUnit.<TIME> (Depending on the use case, a different time may be needed)
() -> message.getChannel().sendMessage("You took longer then a minute to respond. So I've reverted this message.").queue()); // If the user never responds. Do this.
这应该是使用EventWaiter的基本知识。这可能取决于您的用例。而且也可以只使用eventwaiter类来完成。