use of at.xirado.bean.data.ReactionRole in project Bean by Xirado.
the class ReactionRoleCommand method executeCommand.
@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
Guild guild = event.getGuild();
Member bot = guild.getSelfMember();
String subcommand = event.getSubcommandName();
if (subcommand == null) {
ctx.reply(ctx.getLocalized("commands.invalid_subcommand")).setEphemeral(true).queue();
return;
}
if (subcommand.equalsIgnoreCase("remove")) {
TextChannel channel = (TextChannel) event.getOption("channel").getAsGuildChannel();
if (channel == null) {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.channel_not_exists")).setEphemeral(true).queue();
return;
}
long messageID = 0;
try {
messageID = Long.parseLong(event.getOption("message_id").getAsString());
} catch (Exception e) {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_invalid")).setEphemeral(true).queue();
return;
}
channel.retrieveMessageById(messageID).queue((message) -> {
ctx.getGuildData().removeReactionRoles(message.getIdLong()).update();
message.clearReactions().queue(s -> {
}, e -> {
});
ctx.reply(SlashCommandContext.SUCCESS + " " + ctx.getLocalized("commands.reactionroles.removed_success")).setEphemeral(true).queue();
}, new ErrorHandler().handle(ErrorResponse.UNKNOWN_MESSAGE, (err) -> ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_not_exists")).setEphemeral(true).queue()).handle(EnumSet.allOf(ErrorResponse.class), (err) -> {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("general.unknown_error_occured")).setEphemeral(true).queue();
LOGGER.error("An error occurred while trying to remove reaction roles!", err);
}));
} else if (subcommand.equalsIgnoreCase("create")) {
TextChannel channel = (TextChannel) event.getOption("channel").getAsGuildChannel();
if (channel == null) {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.channel_not_exists")).setEphemeral(true).queue();
return;
}
long messageID = 0;
try {
messageID = Long.parseLong(event.getOption("message_id").getAsString());
} catch (Exception e) {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_invalid")).setEphemeral(true).queue();
return;
}
channel.retrieveMessageById(messageID).queue((message) -> {
Role role = event.getOption("role").getAsRole();
if (!bot.canInteract(role)) {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.cannot_interact_role", role.getAsMention())).setEphemeral(true).queue();
return;
}
String emoticon = event.getOption("emote").getAsString();
String emote = emoticon;
Pattern pattern = Message.MentionType.EMOTE.getPattern();
Matcher matcher = pattern.matcher(emoticon);
if (matcher.matches()) {
emoticon = "emote:" + matcher.group(2);
emote = matcher.group(2);
}
String finalEmote = emote;
message.addReaction(emoticon).queue((success) -> {
ReactionRole reactionRole = new at.xirado.bean.data.ReactionRole(finalEmote, message.getIdLong(), role.getIdLong());
ctx.getGuildData().addReactionRoles(reactionRole).update();
ctx.reply(SlashCommandContext.SUCCESS + " " + ctx.getLocalized("commands.reactionroles.added_success")).setEphemeral(true).queue();
}, new ErrorHandler().handle(ErrorResponse.UNKNOWN_EMOJI, (e) -> ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.emote_invalid")).setEphemeral(true).queue()).handle(EnumSet.allOf(ErrorResponse.class), (e) -> {
ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("general.unknown_error_occured")).setEphemeral(true).queue();
LOGGER.error("An error occurred while adding reaction-role!", e);
}));
}, (error) -> ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_not_exists")).setEphemeral(true).queue());
}
}
use of at.xirado.bean.data.ReactionRole in project Bean by Xirado.
the class MessageReactionAddListener method onMessageReactionAdd.
@Override
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent e) {
if (!e.isFromGuild())
return;
if (GuildJoinListener.isGuildBanned(e.getGuild().getIdLong()))
return;
try {
if (e.getMember().getUser().isBot())
return;
Guild g = e.getGuild();
long id = e.getMessageIdLong();
ReactionEmote reactionemote = e.getReactionEmote();
String reacted = reactionemote.isEmoji() ? reactionemote.getAsReactionCode() : reactionemote.getEmote().getId();
GuildData data = GuildManager.getGuildData(e.getGuild());
ReactionRole reactionRole = data.getReactionRoles().stream().filter(x -> x.getMessageId() == id && x.getEmote().equals(reacted)).findFirst().orElse(null);
if (reactionRole != null) {
Role role = e.getGuild().getRoleById(reactionRole.getRoleId());
if (role != null)
g.addRoleToMember(e.getMember(), role).queue(s -> {
}, ex -> {
});
}
} catch (Exception e2) {
LOGGER.error("An error occured whilst executing reaction role event!", e2);
}
}
Aggregations