use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project Gary by help-chat.
the class EventUtils method pullReaction.
public static Future<MessageReaction> pullReaction(Message message, User user) {
JDA jda = message.getJDA();
CompletableFuture<MessageReaction> future = new CompletableFuture<>();
GuildMessageReactionAddEvent e = (GuildMessageReactionAddEvent) pullEvent(EventsEnum.MESSAGE_REACTION_ADD, jda);
while (message.getChannel().retrieveMessageById(e.getMessageId()).complete() != message || e.getUser() != user) {
e = (GuildMessageReactionAddEvent) pullEvent(EventsEnum.MESSAGE_REACTION_ADD, jda);
}
future.complete(e.getReaction());
return future;
}
use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project Gary by help-chat.
the class RMSReactionHandler method execute.
@Override
protected void execute(GenericEvent event) {
GuildMessageReactionAddEvent e = (GuildMessageReactionAddEvent) event;
if (e.getChannel().getName().equalsIgnoreCase("rate-my-server") && !e.getUser().isBot()) {
Message message = e.getChannel().retrieveMessageById(e.getMessageId()).complete();
List<MessageReaction> reactions = new ArrayList<>(message.getReactions());
// this exception will never happen, so can be ignored
// noinspection OptionalGetWithoutIsPresent
reactions.remove(reactions.stream().filter(r -> r.toString().equals(e.getReaction().toString())).findFirst().get());
if (reactions.stream().anyMatch(d -> d.retrieveUsers().complete().contains(e.getUser()))) {
e.getReaction().removeReaction(e.getUser()).queue();
}
}
}
use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project botdarr by shayaantx.
the class DiscordBootstrap method init.
@Override
public void init() throws Exception {
try {
ChatClientBootstrap.ApisAndCommandConfig config = buildConfig();
ChatClientResponseBuilder<DiscordResponse> responseBuilder = new DiscordResponseBuilder();
JDA jda = JDABuilder.createDefault((Config.getProperty(Config.Constants.DISCORD_TOKEN))).addEventListeners(new ListenerAdapter() {
@Override
public void onGenericEvent(@Nonnull GenericEvent event) {
super.onGenericEvent(event);
}
@Override
public void onReady(@Nonnull ReadyEvent event) {
LogManager.getLogger("com.botdarr.clients.discord").info("Connected to discord");
ChatClient<DiscordResponse> chatClient = new DiscordChatClient(event.getJDA());
// start the scheduler threads that send notifications and cache data periodically
initScheduling(chatClient, responseBuilder, config.getApis());
super.onReady(event);
}
@Override
public void onGuildMessageReactionAdd(@Nonnull GuildMessageReactionAddEvent event) {
if (event.getReactionEmote().getName().equalsIgnoreCase(THUMBS_UP_EMOTE)) {
MessageHistory.MessageRetrieveAction me = event.getChannel().getHistoryAround(event.getMessageId(), 1);
me.queue(messageHistory -> {
List<Message> messageHistories = messageHistory.getRetrievedHistory();
messageLoop: for (Message message : messageHistories) {
List<MessageEmbed> embeds = message.getEmbeds();
for (MessageEmbed.Field field : embeds.get(0).getFields()) {
String name = field.getName();
if (Strings.isEmpty(name)) {
continue;
}
if (name.equals(ADD_MOVIE_COMMAND_FIELD_PREFIX) || name.equals(ADD_SHOW_COMMAND_FIELD_PREFIX) || name.equals(ADD_ARTIST_COMMAND_FIELD_PREFIX)) {
// capture/process the command
handleCommand(event.getJDA(), field.getValue(), event.getUser().getName(), event.getChannel().getName());
break messageLoop;
}
}
}
});
}
super.onGuildMessageReactionAdd(event);
}
@Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
handleCommand(event.getJDA(), event.getMessage().getContentStripped(), event.getAuthor().getName(), event.getChannel().getName());
LogManager.getLogger("com.botdarr.clients.discord").debug(event.getMessage().getContentRaw());
super.onMessageReceived(event);
}
private void handleCommand(JDA jda, String message, String author, String channelName) {
// build chat client
DiscordChatClient discordChatClient = new DiscordChatClient(jda);
// capture/process command
Scheduler.getScheduler().executeCommand(() -> {
runAndProcessCommands(message, author, responseBuilder, chatClientResponse -> {
discordChatClient.sendMessage(chatClientResponse, channelName);
});
return null;
});
}
private static final String THUMBS_UP_EMOTE = "\uD83D\uDC4D";
}).build();
jda.awaitReady();
} catch (Throwable e) {
LogManager.getLogger("com.botdarr.clients.discord").error("Error caught during main", e);
throw e;
}
}
use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project pokeraidbot by magnusmickelsson.
the class EventLoggingListener method onEvent.
@Override
public void onEvent(GenericEvent event) {
if (LOGGER.isTraceEnabled()) {
if (event instanceof GuildMessageReactionAddEvent) {
final GuildMessageReactionAddEvent reactionAddEvent = (GuildMessageReactionAddEvent) event;
LOGGER.trace("Reaction: " + reactionAddEvent.getUser() + " - " + reactionAddEvent.getReaction() + " - " + reactionAddEvent.getReactionEmote());
} else if (event instanceof GuildMessageReceivedEvent) {
final GuildMessageReceivedEvent guildMessageReceivedEvent = (GuildMessageReceivedEvent) event;
LOGGER.trace("Message from " + guildMessageReceivedEvent.getAuthor() + ": " + guildMessageReceivedEvent.getMessage());
} else if (event instanceof GuildMessageEmbedEvent) {
final GuildMessageEmbedEvent guildMessageReceivedEvent = (GuildMessageEmbedEvent) event;
final List<MessageEmbed> messageEmbeds = guildMessageReceivedEvent.getMessageEmbeds();
for (MessageEmbed embed : messageEmbeds) LOGGER.trace("Embed message from " + embed.getAuthor() + ": " + String.valueOf(embed.getTitle()) + " - " + String.valueOf(embed.getDescription()));
}
}
}
use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project pokeraidbot by magnusmickelsson.
the class EmoticonSignUpMessageListener method onEvent.
@Override
public void onEvent(GenericEvent event) {
String reactionMessageId = null;
User user = null;
if (emoteMessageId == null && infoMessageId == null) {
LOGGER.trace("This listener haven't received a emote message id or info message id yet.");
return;
}
try {
if (event instanceof GuildMessageReactionAddEvent) {
final GuildMessageReactionAddEvent reactionEvent = (GuildMessageReactionAddEvent) event;
user = reactionEvent.getUser();
// If the bot added any reactions, don't respond to them
if (user.isBot())
return;
reactionMessageId = reactionEvent.getReaction().getMessageId();
if (emoteMessageId == null || !emoteMessageId.equals(reactionMessageId)) {
return;
}
// If this is a reaction for a user that just triggered an error with his/her reaction, skip it
if (user.getName().equals(userHadError)) {
userHadError = null;
return;
}
final MessageReaction.ReactionEmote emote = reactionEvent.getReaction().getReactionEmote();
if (emote != null) {
switch(emote.getName()) {
case Emotes.ONE:
addToSignUp(user, 0, 0, 0, 1);
break;
case Emotes.TWO:
addToSignUp(user, 0, 0, 0, 2);
break;
case Emotes.THREE:
addToSignUp(user, 0, 0, 0, 3);
break;
case Emotes.FOUR:
addToSignUp(user, 0, 0, 0, 4);
break;
case Emotes.FIVE:
addToSignUp(user, 0, 0, 0, 5);
break;
default:
}
}
} else if (event instanceof GuildMessageReactionRemoveEvent) {
final GuildMessageReactionRemoveEvent reactionEvent = (GuildMessageReactionRemoveEvent) event;
// If the bot added any reactions, don't respond to them
user = reactionEvent.getUser();
if (user.isBot())
return;
// If this is a reaction for a user that just triggered an error with his/her reaction, skip it
if (user.getName().equals(userHadError)) {
userHadError = null;
return;
}
reactionMessageId = reactionEvent.getReaction().getMessageId();
if (!emoteMessageId.equals(reactionMessageId)) {
return;
}
final MessageReaction.ReactionEmote emote = reactionEvent.getReaction().getReactionEmote();
if (emote != null) {
switch(emote.getName()) {
case Emotes.ONE:
removeFromSignUp(user, 0, 0, 0, 1);
break;
case Emotes.TWO:
removeFromSignUp(user, 0, 0, 0, 2);
break;
case Emotes.THREE:
removeFromSignUp(user, 0, 0, 0, 3);
break;
case Emotes.FOUR:
removeFromSignUp(user, 0, 0, 0, 4);
break;
case Emotes.FIVE:
removeFromSignUp(user, 0, 0, 0, 5);
break;
default:
}
}
}
} catch (Throwable t) {
if (event instanceof GenericGuildMessageReactionEvent) {
final GenericGuildMessageReactionEvent guildMessageReactionEvent = (GenericGuildMessageReactionEvent) event;
reactionMessageId = guildMessageReactionEvent.getReaction().getMessageId();
if (emoteMessageId == null) {
LOGGER.warn("Emote message ID = null, this should get cleaned up!" + " Event: " + printInfoAbout(event));
return;
}
if (!emoteMessageId.equals(reactionMessageId)) {
LOGGER.warn("We got a guild reaction event throwing exception, but not one we were listening for!" + " Event: " + printInfoAbout(event));
return;
}
// Since we got an error, remove last reaction
if (reactionMessageId != null && reactionMessageId.equals(emoteMessageId)) {
// Do this with a slight delay to prevent graphical glitches client side.
guildMessageReactionEvent.getReaction().removeReaction(user).queueAfter(30, TimeUnit.MILLISECONDS);
userHadError = user.getName();
}
if (user != null && t.getMessage() != null) {
MessageBuilder messageBuilder = new MessageBuilder();
if (!t.getMessage().contains(user.getAsMention())) {
messageBuilder.append(user.getAsMention()).append(": ");
}
messageBuilder.append(t.getMessage());
guildMessageReactionEvent.getChannel().sendMessage(messageBuilder.build()).queue();
} else {
LOGGER.warn("We have a situation where user " + user + " or exception (of type " + t.getClass().getSimpleName() + ") message is null! Event: " + printInfoAbout(event));
}
} else {
LOGGER.warn("Exception in event listener! Event: " + printInfoAbout(event));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Stacktrace:", t);
}
}
}
}
Aggregations