import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Listener 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();
}
}
}
}
我如何获得第二条信息?
正如明恩在评论中所说的那样。您可以使用线程中给出的方法。但是,我建议使用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>
对格雷德尔来说,这要容易得多:
# 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()
}
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.
Hi akka古鲁们:)你能在这一次指导我吗? 我要做的是-演员A向演员B要消息,然后等一个回来。但是,不知何故,演员B给A的不是一条信息,而是其中的4条信息。A正确完成,但rest消息中有3条被算作死信。为什么?这样对吗?我是说,演员A有一个合适的处理人,那为什么信都死了?:-( [INFO][11/22/2013 22:00:38.975][ForkJoinPool-2-worker-7][a
我正在使用hiredis C库连接到redis服务器。我不知道在订阅新消息后如何等待新消息。 我的代码如下所示: 现在如何告诉雇佣者在频道上等待消息?
问题内容: 通过Rabbitmq中的示例,消费者可以一次从队列中获取所有消息。如何使用一条消息并退出? 问题答案: 您必须声明basicQos设置,才能一次从ACK到NACK状态获取一条消息,并禁用自动ACK以便显式给出确认。 希望能帮助到你!
我第一次做硒测试。在主页上,我调用了一些AJAX,我希望Selenium等待元素加载完成。我不确定它是否有效,但我只是键入selenium,waitForCondition可以选择。 无论我选择什么,它总是返回“false”。我现在连等待条件都不工作吗? 我如何测试它是否有效?在这些代码中我做错了什么? 如果由自己的类实现,则返回“true” isElementPresent(By.xpath(“
问题内容: 我正在使用hiredis C库连接到redis服务器。我无法弄清楚订阅新消息后如何等待新消息。 我的代码如下所示: 现在如何告诉hiredis在频道上等待消息? 问题答案: 您无需告诉hiredis您需要在通道上等待:事件循环将仅在先前已注册的Redis连接上等待。 这是一个完整的示例: 您可以通过使用以下命令发布内容来对其进行测试: event_base_dispatch函数是实际启
问题内容: 我希望脚本等待用户按下任何键。 我怎么做? 问题答案: 在 Python 3中 使用: 在 Python 2中 使用: 不过,这仅等待用户按下Enter键。 可能要使用 msvcrt ((仅Windows / DOS)使用 msvcrt 模块可以访问Microsoft Visual C / C ++运行时库(MSVCRT)中的许多功能): 这应该等待按键。 附加信息: Python 3