Initial Database Migration
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
*
|
||||
* Project: Eight Track, File: DiscordEventHandler.java
|
||||
*
|
||||
* Copyright 2019 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.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Date;
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||
import net.locusworks.discord.eighttrack.database.repos.GuildRepository;
|
||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
||||
import net.locusworks.discord.eighttrack.services.GuildMusicService;
|
||||
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 ConfigurationService confService;
|
||||
|
||||
@Autowired
|
||||
private GuildRepository guildRepo;
|
||||
|
||||
@Autowired
|
||||
private GuildMusicService musicService;
|
||||
|
||||
@Autowired
|
||||
private Mp3UploadHandler uploadHandler;
|
||||
|
||||
@PostConstruct
|
||||
private void init() throws IOException {
|
||||
this.musicDir = confService.getMusicDirectory();
|
||||
if (!Files.exists(this.musicDir.toAbsolutePath())) {
|
||||
Files.createDirectories(this.musicDir.toAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildJoin(GuildJoinEvent event) {
|
||||
try {
|
||||
long guildId = event.getGuild().getIdLong();
|
||||
DiscordGuild discordGuild = guildRepo.findByGuildId(guildId);
|
||||
if (discordGuild != null) return;
|
||||
|
||||
logger.debug("Joining Server: " + event.getGuild().getName());
|
||||
|
||||
discordGuild = new DiscordGuild();
|
||||
discordGuild.setGuildId(guildId);
|
||||
discordGuild.setGuildName(event.getGuild().getName());
|
||||
discordGuild.setDateJoined(new Date());
|
||||
|
||||
guildRepo.save(discordGuild);
|
||||
} catch (Exception ex) {
|
||||
logger.error("Unable to persist server information to database: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
|
||||
if (event.getAuthor().isBot()) return;
|
||||
try {
|
||||
onGuildMessageReceivedHelper(event);
|
||||
} catch (Throwable t) {
|
||||
logger.error(t);
|
||||
}
|
||||
}
|
||||
|
||||
private void onGuildMessageReceivedHelper(GuildMessageReceivedEvent event) throws IOException {
|
||||
|
||||
if (!musicService.containsKey(event.getGuild().getIdLong())) {
|
||||
musicService.put(event.getGuild().getIdLong(), new GuildMusicHandler(musicDir, uploadHandler));
|
||||
}
|
||||
GuildMusicHandler gmh = musicService.get(event.getGuild().getIdLong());
|
||||
gmh.accept((id) -> musicService.remove(id));
|
||||
|
||||
String command = event.getMessage().getContentRaw().trim().toLowerCase();
|
||||
|
||||
switch(command) {
|
||||
case "-upload":
|
||||
gmh.upload(event);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
switch (command) {
|
||||
case "-play":
|
||||
gmh.isPlaying(true);
|
||||
gmh.play(event);
|
||||
return;
|
||||
case "-stop":
|
||||
gmh.isPlaying(false);
|
||||
gmh.stop(event);
|
||||
return;
|
||||
case "-next":
|
||||
gmh.next(event);
|
||||
return;
|
||||
case "-repeat":
|
||||
gmh.isPlaying(true);
|
||||
gmh.repeat(event);
|
||||
return;
|
||||
case "-whatsnext":
|
||||
gmh.whatsNext(event);
|
||||
break;
|
||||
case "-index":
|
||||
gmh.index(event);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user