【发布时间】:2021-09-18 08:28:55
【问题描述】:
public class HelloWorldBot extends ListenerAdapter
{
public static void main(String[] args) throws LoginException
{
if (args.length < 1) {
System.out.println("You have to provide a token as first argument!");
System.exit(1);
}
// args[0] should be the token
// We don't need any intents for this bot. Slash commands work without any intents!
JDA jda = JDABuilder.createLight(args[0], Collections.emptyList())
.addEventListeners(new HelloWorldBot())
.setActivity(Activity.playing("Type /ping"))
.build();
jda.upsertCommand("ping", "Calculate ping of the bot").queue(); // This can take up to 1 hour to show up in the client
}
@Override
public void onSlashCommand(SlashCommandEvent event)
{
if (!event.getName().equals("ping")) return; // make sure we handle the right command
long time = System.currentTimeMillis();
event.reply("Pong!").setEphemeral(true) // reply or acknowledge
.flatMap(v ->
event.getHook().editOriginalFormat("Pong: %d ms", System.currentTimeMillis() - time) // then edit original
).queue(); // Queue both reply and edit
}
}
通过上面的代码,我可以通过 DM 机器人使用斜杠命令。但是,如何在频道中使用斜杠命令?
discord 开发者文档中说:
为了使 Slash Commands 在公会中起作用,公会必须 使用 applications.commands 范围授权您的应用程序。这 bot 范围不够。
JDA 究竟是如何做到这一点的?
【问题讨论】:
标签: discord-jda