use of net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent in project JDA by DV8FromTheWorld.
the class InteractionCreateHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
int type = content.getInt("type");
if (content.getInt("version", 1) != 1) {
WebSocketClient.LOG.debug("Received interaction with version {}. This version is currently unsupported by this version of JDA. Consider updating!", content.getInt("version", 1));
return null;
}
long guildId = content.getUnsignedLong("guild_id", 0);
if (api.getGuildSetupController().isLocked(guildId))
return guildId;
if (guildId != 0 && api.getGuildById(guildId) == null)
// discard event if it is not from a guild we are currently in
return null;
switch(InteractionType.fromKey(type)) {
case // slash commands
COMMAND:
handleCommand(content);
break;
case // buttons/components
COMPONENT:
handleAction(content);
break;
case COMMAND_AUTOCOMPLETE:
api.handleEvent(new CommandAutoCompleteInteractionEvent(api, responseNumber, new CommandAutoCompleteInteractionImpl(api, content)));
break;
default:
api.handleEvent(new GenericInteractionCreateEvent(api, responseNumber, new InteractionImpl(api, content)));
}
return null;
}
use of net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent in project Discord-Core-Bot-Apple by demodude4u.
the class DiscordBot method startUp.
@Override
protected void startUp() throws Exception {
info.addTechnology("[DCBA](https://github.com/demodude4u/Discord-Core-Bot-Apple)", Optional.empty(), "Discord Core Bot Apple");
info.addTechnology("[JDA](https://github.com/DV8FromTheWorld/JDA)", Optional.of("5.0 alpha"), "Java Discord API");
if (configJson.has("command_prefix")) {
setCommandPrefix(Optional.of(configJson.getString("command_prefix")));
}
JDABuilder builder = //
JDABuilder.createDefault(configJson.getString("bot_token")).setEnableShutdownHook(//
false).addEventListeners(new ListenerAdapter() {
@Override
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
SlashCommandDefinition commandDefinition = commandSlash.get(event.getCommandPath());
Optional<AutoCompleteHandler> autoCompleteHandler = commandDefinition.getAutoCompleteHandler();
if (autoCompleteHandler.isPresent()) {
AutoCompleteEvent autoCompleteEvent = new AutoCompleteEvent(event);
autoCompleteHandler.get().handleAutoComplete(autoCompleteEvent);
}
}
@Override
public void onMessageContextInteraction(MessageContextInteractionEvent event) {
MessageCommandDefinition commandDefinition = commandMessage.get(event.getName());
boolean ephemeral = commandDefinition.hasRestriction(CommandRestriction.EPHEMERAL);
CommandReporting reporting = createReporting(event);
InteractionHook hook = event.deferReply(ephemeral).complete();
MessageCommandEvent commandEvent = new MessageCommandEvent(event, reporting, hook, ephemeral);
commandService.submit(() -> {
try {
commandDefinition.getHandler().handleCommand(commandEvent);
} catch (Exception e) {
reporting.addException(e);
} finally {
if (!commandDefinition.hasRestriction(CommandRestriction.NO_REPORTING)) {
submitReport(reporting);
}
if (!reporting.getExceptions().isEmpty()) {
commandEvent.replyEmbed(new EmbedBuilder().setColor(Color.red).appendDescription("Sorry, there was a problem completing your request.\n" + reporting.getExceptions().stream().map(e -> "`" + e.getMessage() + "`").distinct().collect(Collectors.joining("\n"))).build());
}
if (!commandEvent.hasReplied()) {
hook.deleteOriginal().complete();
}
}
});
}
@Override
public void onMessageDelete(MessageDeleteEvent event) {
if (textWatcher.isPresent()) {
textWatcher.get().deletedMessage(event);
}
}
@Override
public void onMessageReactionAdd(MessageReactionAddEvent event) {
if (reactionWatcher.isPresent()) {
reactionWatcher.get().seenReaction(event);
}
}
@Override
public void onMessageReactionRemove(MessageReactionRemoveEvent event) {
if (reactionWatcher.isPresent()) {
reactionWatcher.get().seenReactionRemoved(event);
}
}
@Override
public void onMessageReactionRemoveAll(MessageReactionRemoveAllEvent event) {
if (reactionWatcher.isPresent()) {
reactionWatcher.get().seenAllReactionRemoved(event);
}
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (textWatcher.isPresent()) {
textWatcher.get().seenMessage(event);
}
Message message = event.getMessage();
MessageChannel channel = message.getChannel();
String rawContent = message.getContentRaw().trim();
String mentionMe = "<@!" + event.getJDA().getSelfUser().getId() + ">";
// String mentionMe = event.getJDA().getSelfUser().getAsMention();
boolean isPrivateChannel = channel instanceof PrivateChannel;
if (isPrivateChannel && ignorePrivateChannels) {
return;
}
boolean startsWithMentionMe = message.getMentionedUsers().stream().anyMatch(u -> u.getIdLong() == event.getJDA().getSelfUser().getIdLong()) && rawContent.startsWith(mentionMe);
if (startsWithMentionMe) {
rawContent = rawContent.substring(mentionMe.length()).trim();
}
Optional<String> effectivePrefix = getOldCommandPrefix(event);
boolean startsWithCommandPrefix = effectivePrefix.isPresent() && rawContent.startsWith(effectivePrefix.get());
if (startsWithCommandPrefix) {
rawContent = rawContent.substring(effectivePrefix.get().length()).trim();
}
if (!event.getAuthor().isBot() && (isPrivateChannel || startsWithMentionMe || startsWithCommandPrefix)) {
String[] split = rawContent.split("\\s+");
if (split.length > 0) {
String command = split[0];
SlashCommandDefinition commandDefinition = commandLegacy.get(command.toLowerCase());
if (commandDefinition != null) {
boolean isPermitted = checkPermitted(channel, event.getMember(), commandDefinition);
if (isPermitted) {
event.getChannel().sendMessageEmbeds(new EmbedBuilder().appendDescription("Please use /" + commandDefinition.getPath().replace("/", " ")).build()).complete();
}
}
}
}
}
@Override
public void onMessageUpdate(MessageUpdateEvent event) {
if (textWatcher.isPresent()) {
textWatcher.get().editedMessage(event);
}
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
SlashCommandDefinition commandDefinition = commandSlash.get(event.getCommandPath());
boolean ephemeral = commandDefinition.hasRestriction(CommandRestriction.EPHEMERAL);
CommandReporting reporting = createReporting(event);
InteractionHook hook = event.deferReply(ephemeral).complete();
SlashCommandEvent commandEvent = new SlashCommandEvent(event, reporting, hook, ephemeral);
commandService.submit(() -> {
try {
commandDefinition.getHandler().handleCommand(commandEvent);
} catch (Exception e) {
System.err.println("Uncaught Exception!");
e.printStackTrace();
reporting.addException(e);
} finally {
if (!commandDefinition.hasRestriction(CommandRestriction.NO_REPORTING)) {
submitReport(reporting);
}
if (!reporting.getExceptions().isEmpty()) {
commandEvent.replyEmbed(new EmbedBuilder().setColor(Color.red).appendDescription("Sorry, there was a problem completing your request.\n" + reporting.getExceptions().stream().map(e -> "`" + e.getMessage() + "`").distinct().collect(Collectors.joining("\n"))).build());
}
if (!commandEvent.hasReplied()) {
hook.deleteOriginal().complete();
}
}
});
}
});
if (customSetup != null) {
builder = customSetup.apply(builder);
}
jda = builder.build().awaitReady();
jda.setRequiredScopes("bot", "applications.commands");
reportingUserID = Optional.ofNullable(configJson.optString("reporting_user_id", null));
reportingChannelID = Optional.ofNullable(configJson.optString("reporting_channel_id", null));
if (configJson.has("debug_guild_commands")) {
String guildId = configJson.getString("debug_guild_commands");
Guild guild = jda.getGuildById(guildId);
CommandListUpdateAction updateCommands = guild.updateCommands();
buildUpdateCommands(updateCommands);
updateCommands.queue();
}
CommandListUpdateAction updateCommands = jda.updateCommands();
buildUpdateCommands(updateCommands);
updateCommands.queue();
}
Aggregations