Rework
All checks were successful
Locusworks Team/eight-track/pipeline/head This commit looks good

This commit is contained in:
2020-10-23 11:56:48 -05:00
parent f051e98768
commit 4c40d33050
6 changed files with 30 additions and 30 deletions

View File

@@ -1,156 +0,0 @@
/**
*
* Project: Eight Track, File: DiscordEventHandler.java
*
* Copyright 2019-2020 Locusworks LLC.
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
* any means and imposes fines up to $25,000 for violation. No part of this material
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
* modified, duplicated, adapted or translated into another program language in any
* form or by any means, electronic, mechanical, photocopying, recording, or
* otherwise, without the prior written permission from Locusworks. Locusworks
* affirms that Eight-Track(R) software and data is subject to United States
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
*
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
package net.locusworks.discord.eighttrack.handlers;
import java.awt.Color;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.ExceptionEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
import net.locusworks.discord.eighttrack.services.ConfigurationService;
import net.locusworks.discord.eighttrack.services.RepositoryService;
import net.locusworks.logger.ApplicationLogger;
import net.locusworks.logger.ApplicationLoggerFactory;
@Service
public class DiscordEventHandler extends ListenerAdapter {
private Path musicDir;
private ApplicationLogger logger = ApplicationLoggerFactory.getLogger(DiscordEventHandler.class);
@Autowired
private RepositoryService guildSongRepoService;
@Autowired
private ReactionHandler reactionHandler;
private Map<String, AbstractMainEventHandler> events;
@Autowired
private void setup(ConfigurationService cService, List<AbstractMainEventHandler> eventHandlers) throws IOException {
this.musicDir = cService.getMusicDirectory();
if (!Files.exists(this.musicDir.toAbsolutePath())) {
Files.createDirectories(this.musicDir.toAbsolutePath());
}
events = new TreeMap<>();
for(AbstractMainEventHandler handler : eventHandlers) {
events.put(handler.getCommand(), handler);
}
}
@Scheduled(fixedRate = 300000L, initialDelay=300000L)
private void clearHandlerCache() {
logger.debug("Clearing reaction handler cache of dead reactions");
reactionHandler.cleanCache();
}
@Override
public void onMessageReactionAdd(MessageReactionAddEvent e) {
if (e.getUser().isBot()) {
return;
}
if (!e.getChannel().getType().equals(ChannelType.TEXT)) {
return;
}
TextChannel channel = (TextChannel) e.getChannel();
if (reactionHandler.canHandle(channel.getGuild().getIdLong(), e.getMessageIdLong())) {
reactionHandler.handle(channel, e.getMessageIdLong(), e.getUser().getIdLong(), e.getReaction());
}
}
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
if (event.getAuthor().isBot()) return;
try {
onGuildMessageReceivedHelper(event);
} catch (Throwable t) {
guildSongRepoService.logEntry(event.getGuild().getIdLong(), t, "An error occured: %s", t.getMessage());
logger.error(t);
}
}
@Override
public void onException(ExceptionEvent event) {
logger.error("A general exception occured: " + event.getCause().getMessage());
logger.error(event.getCause());
}
private void onGuildMessageReceivedHelper(GuildMessageReceivedEvent event) throws Exception {
List<String> commands = new ArrayList<String>(Arrays.asList(event.getMessage().getContentRaw().trim().toLowerCase().split(" ")));
String command = commands.remove(0);
AbstractMainEventHandler aeh = events.get(command.toLowerCase());
if (aeh != null) {
aeh.executeEvent(event, commands);
return;
}
if (!command.equalsIgnoreCase("-help")) return;
StringBuilder sb = new StringBuilder();
for (Entry<String, AbstractMainEventHandler> entry : events.entrySet()) {
sb.append(String.format("%s | %s%n", entry.getKey(), entry.getValue().help()));
}
EmbedBuilder builder = new EmbedBuilder()
.setColor(Color.GREEN)
.setThumbnail(event.getGuild().getIconUrl())
.setTitle("Eight Track Help Menu")
.setDescription(sb.toString())
.setTimestamp(OffsetDateTime.now())
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl());
event.getChannel().sendMessage(builder.build()).queue();
}
}