use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project LightboltMeeting by LightboltMeeting.
the class EndMeetingSubcommand method handleMeetingCommand.
@Override
protected ReplyCallbackAction handleMeetingCommand(SlashCommandInteractionEvent event, LocaleConfig locale, SystemsConfig.MeetingConfig config, MeetingRepository repo) throws SQLException {
OptionMapping idOption = event.getOption("meeting-id");
if (idOption == null) {
return Responses.error(event, locale.getCommand().getMISSING_ARGUMENTS());
}
int id = (int) idOption.getAsLong();
Optional<Meeting> meetingOptional = repo.getById(id);
if (meetingOptional.isEmpty()) {
return Responses.error(event, String.format(locale.getMeeting().getCommand().getMEETING_NOT_FOUND(), id));
}
Meeting meeting = meetingOptional.get();
if (!MeetingManager.canEditMeeting(meeting, event.getUser().getIdLong())) {
return Responses.error(event, locale.getMeeting().getMEETING_NO_PERMISSION());
}
MeetingManager manager = new MeetingManager(event.getJDA(), meeting);
var com = locale.getMeeting().getCommand();
if (meeting.getStatus() != MeetingStatus.ONGOING) {
return Responses.error(event, com.getMEETING_END_FAILED_DESCRIPTION());
}
manager.endMeeting();
return Responses.success(event, com.getMEETING_END_SUCCESS_TITLE(), String.format(com.getMEETING_END_SUCCESS_DESCRIPTION(), id));
}
use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project Bean by Xirado.
the class InteractionCommandHandler method handleSlashCommand.
public void handleSlashCommand(@NotNull SlashCommandInteractionEvent event, @Nullable Member member) {
Runnable r = () -> {
try {
if (!event.isFromGuild())
return;
Guild guild = event.getGuild();
SlashCommand command = null;
long guildId = event.getGuild().getIdLong();
if (registeredGuildCommands.containsKey(guildId)) {
List<SlashCommand> guildCommands = registeredGuildCommands.get(guildId).stream().filter(cmd -> cmd instanceof SlashCommand).map(cmd -> (SlashCommand) cmd).toList();
SlashCommand guildCommand = guildCommands.stream().filter(cmd -> cmd.getCommandName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
if (guildCommand != null)
command = guildCommand;
}
if (command == null) {
SlashCommand globalCommand = getRegisteredSlashCommands().stream().filter(cmd -> cmd.getCommandName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
if (globalCommand != null)
command = globalCommand;
}
if (command != null) {
SlashCommandContext ctx = new SlashCommandContext(event);
List<Permission> neededPermissions = command.getRequiredUserPermissions();
List<Permission> neededBotPermissions = command.getRequiredBotPermissions();
if (neededPermissions != null && !member.hasPermission((GuildChannel) event.getChannel(), neededPermissions)) {
event.reply(LocaleLoader.ofGuild(guild).get("general.no_perms", String.class)).queue();
return;
}
if (neededBotPermissions != null && !event.getGuild().getSelfMember().hasPermission((GuildChannel) event.getChannel(), neededBotPermissions)) {
event.reply(LocaleLoader.ofGuild(guild).get("general.no_bot_perms1", String.class)).queue();
return;
}
if (command.getCommandFlags().contains(CommandFlag.DJ_ONLY)) {
if (!ctx.getGuildData().isDJ(member)) {
event.replyEmbeds(EmbedUtil.errorEmbed("You need to be a DJ to do this!")).queue();
return;
}
}
if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_VC)) {
GuildVoiceState guildVoiceState = member.getVoiceState();
if (guildVoiceState == null || !guildVoiceState.inAudioChannel()) {
event.replyEmbeds(EmbedUtil.errorEmbed("You are not connected to a voice-channel!")).queue();
return;
}
}
if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_SAME_VC)) {
GuildVoiceState voiceState = member.getVoiceState();
AudioManager manager = event.getGuild().getAudioManager();
if (manager.isConnected()) {
if (!manager.getConnectedChannel().equals(voiceState.getChannel())) {
event.replyEmbeds(EmbedUtil.errorEmbed("You must be listening in " + manager.getConnectedChannel().getAsMention() + "to do this!")).setEphemeral(true).queue();
return;
}
}
}
if (command.getCommandFlags().contains(CommandFlag.REQUIRES_LAVALINK_NODE)) {
if (!ctx.isLavalinkNodeAvailable()) {
event.replyEmbeds(EmbedUtil.errorEmbed("There are currently no voice nodes available!\nIf the issue persists, please leave a message on our support server!")).addActionRow(Util.getSupportButton()).queue();
return;
}
}
command.executeCommand(event, ctx);
Metrics.COMMANDS.labels("success").inc();
}
} catch (Exception e) {
Metrics.COMMANDS.labels("failed").inc();
LinkedDataObject translation = event.getGuild() == null ? LocaleLoader.getForLanguage("en_US") : LocaleLoader.ofGuild(event.getGuild());
if (event.isAcknowledged())
event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
}, ex -> {
});
else
event.replyEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
}, ex -> {
});
LOGGER.error("Could not execute slash-command", e);
StringBuilder path = new StringBuilder("/" + event.getCommandPath().replace("/", " "));
for (OptionMapping option : event.getOptions()) {
path.append(" *").append(option.getName()).append("* : ").append("`").append(option.getAsString()).append("`");
}
EmbedBuilder builder = new EmbedBuilder().setTitle("An error occurred while executing a slash-command!").addField("Guild", event.getGuild() == null ? "None (Direct message)" : event.getGuild().getIdLong() + " (" + event.getGuild().getName() + ")", true).addField("Channel", event.getGuild() == null ? "None (Direct message)" : event.getChannel().getName(), true).addField("User", event.getUser().getAsMention() + " (" + event.getUser().getAsTag() + ")", true).addField("Command", path.toString(), false).setColor(EmbedUtil.ERROR_COLOR);
event.getJDA().openPrivateChannelById(Bean.OWNER_ID).flatMap(c -> c.sendMessageEmbeds(builder.build()).content("```fix\n" + ExceptionUtils.getStackTrace(e) + "\n```")).queue();
}
};
Bean.getInstance().getCommandExecutor().execute(r);
}
use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project Bean by Xirado.
the class AvatarCommand method executeCommand.
@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
OptionMapping option = event.getOption("user");
User user = option != null ? option.getAsUser() : event.getUser();
EmbedBuilder b = new EmbedBuilder().setImage(user.getEffectiveAvatarUrl() + "?size=512").setColor(Color.magenta).setTimestamp(Instant.now()).setAuthor(ctx.getLocalized("commands.avatar_title", user.getAsTag()), null, user.getEffectiveAvatarUrl());
ctx.reply(b.build()).queue();
}
use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project Bean by Xirado.
the class RankCommand method executeCommand.
@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
InteractionHook commandHook = event.getHook();
event.deferReply(false).queue();
OptionMapping optionData = event.getOption("user");
User user = optionData == null ? event.getUser() : optionData.getAsUser();
long xp = RankingSystem.getTotalXP(event.getGuild().getIdLong(), user.getIdLong());
if (xp < 100) {
if (optionData == null)
commandHook.sendMessage("You are not yet ranked!").queue();
else
commandHook.sendMessage("This member is not yet ranked!").queue();
return;
}
byte[] rankCard = RankingSystem.generateLevelCard(user, event.getGuild());
if (rankCard == null) {
commandHook.sendMessageEmbeds(EmbedUtil.errorEmbed("Could not load rank card! Please try again later!")).queue();
return;
}
commandHook.sendFile(rankCard, "card.png").queue();
}
use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project LightboltMeeting by LightboltMeeting.
the class DiscardMeetingSubcommand method handleMeetingCommand.
@Override
protected ReplyCallbackAction handleMeetingCommand(SlashCommandInteractionEvent event, LocaleConfig locale, SystemsConfig.MeetingConfig config, MeetingRepository repo) throws SQLException {
OptionMapping idOption = event.getOption("meeting-id");
if (idOption == null) {
return Responses.error(event, locale.getCommand().getMISSING_ARGUMENTS());
}
int id = (int) idOption.getAsLong();
Optional<Meeting> meetingOptional = repo.getById(id);
if (meetingOptional.isEmpty()) {
return Responses.error(event, String.format(locale.getMeeting().getCommand().getMEETING_NOT_FOUND(), id));
}
Meeting meeting = meetingOptional.get();
if (!MeetingManager.canEditMeeting(meeting, event.getUser().getIdLong())) {
return Responses.error(event, locale.getMeeting().getMEETING_NO_PERMISSION());
}
MeetingManager manager = new MeetingManager(event.getJDA(), meeting);
var com = locale.getMeeting().getCommand();
if (meeting.getStatus() == MeetingStatus.ONGOING) {
return Responses.error(event, com.getMEETING_DISCARD_FAILED_DESCRIPTION());
}
manager.endMeeting();
return Responses.success(event, com.getCANCEL_MEETING_TITLE(), String.format(com.getCANCEL_MEETING_DESCRIPTION(), id));
}
Aggregations