55import net .dv8tion .jda .api .events .interaction .command .GenericCommandInteractionEvent ;
66import net .dv8tion .jda .api .events .interaction .command .UserContextInteractionEvent ;
77
8+ import java .sql .SQLException ;
89import java .util .*;
910import java .util .regex .Matcher ;
1011import java .util .regex .Pattern ;
1112
1213public class CsFunService {
1314
1415 private Properties properties ;
16+ private DataService dataService ;
1517 ResourceBundle resourceBundle ;
1618
1719 Map <String , String > wowList ;
1820
1921 public CsFunService (Properties properties ) {
2022 this .properties = properties ;
21- wowList = new HashMap <String , String >();
22- wowList .put ("jay_th" , "https://cdn.discordapp.com/attachments/449281855175393280/1221510017354563674/loud.mov" );
23- wowList .put ("vi24ra" , "https://cdn.discordapp.com/attachments/288367861515419649/1167948820525621248/Dropshot.mp4" );
24- wowList .put ("aatha" , "https://cdn.discordapp.com/attachments/844510835241910303/1225082807110336653/Me_Is_Sorry_Janes.mp4" );
23+ setupWowList ();
2524 }
2625
2726 public String handleWowEvent (UserContextInteractionEvent event , String locale ) {
@@ -31,33 +30,40 @@ public String handleWowEvent(UserContextInteractionEvent event, String locale) {
3130 resourceBundle = ResourceBundle .getBundle ("localization" , new Locale (locale ));
3231 String targetUserName = targetUser .getName ();
3332
34- if ("CSBot" . equals (targetUserName )) {
35- //teehee.
36- return sendMessageInCorrectChannel (event ,dedicatedChannel , "https://www.youtube.com/watch?v=2qTHmSyqrok" );
33+ if (wowList . containsKey (targetUserName )) {
34+ String message = resourceBundle . getString ( "wow.highlightMessage" ). replace ( "%s" , targetUserName ) + " " + wowList . get ( targetUserName );
35+ return sendMessageInCorrectChannel (event , dedicatedChannel , message );
3736 } else if (targetUser .isBot ()) {
3837 return sendMessageInCorrectChannel (event , dedicatedChannel , resourceBundle .getString ("error.cantwowabot" ));
3938 } else {
40- if (wowList .containsKey (targetUserName )) {
41- return sendMessageInCorrectChannel (event , dedicatedChannel , wowList .get (targetUserName ));
42- } else {
43- return sendMessageInCorrectChannel (event , dedicatedChannel , resourceBundle .getString ("error.hasnowow" ));
44- }
39+ return sendMessageInCorrectChannel (event , dedicatedChannel , resourceBundle .getString ("error.hasnowow" ));
40+ }
41+ }
42+
43+ private void setupWowList () {
44+ wowList = new HashMap <String , String >();
45+ try {
46+ dataService = new DataService (properties );
47+ dataService .setupConnection ();
48+ wowList = dataService .returnAllWowEntries ();
49+ } catch (SQLException exception ) {
50+ System .out .println ("Exception thrown." );
4551 }
4652 }
4753
48- private String sendMessageInCorrectChannel (GenericCommandInteractionEvent event , String dedicatedChannel , String originalMessage ) {
54+ private String sendMessageInCorrectChannel (GenericCommandInteractionEvent event , String dedicatedChannel , String message ) {
4955 if (dedicatedChannel .equals (event .getMessageChannel ().getId ())) {
50- return originalMessage ;
56+ return message ;
5157 } else {
5258 TextChannel dedicatedTextChannel = event .getHook ().getInteraction ().getGuild ().getTextChannelById (dedicatedChannel );
5359
5460 //this means no dedicated channel was found for this ID. either no dedicated channel was set or it doesn't exist on this server.
5561 //either way, this means that the event is going to be returned in the current active channel. that's a bit messy but hey,
5662 //if that's what they want..?
5763 if (dedicatedTextChannel == null ) {
58- return originalMessage ;
64+ return message ;
5965 } else {
60- dedicatedTextChannel .sendMessage (originalMessage ).queue ();
66+ dedicatedTextChannel .sendMessage (message ).queue ();
6167 return resourceBundle .getString ("wow.messageSent" );
6268 }
6369 }
@@ -76,11 +82,16 @@ public String handleAddWowEvent(GenericCommandInteractionEvent event, String loc
7682 Matcher ytMatcher = ytPattern .matcher (url );
7783 Matcher dMatcher = dPattern .matcher (url );
7884
79- if (ytMatcher .find () || dMatcher .find ()) {
80- wowList .put (user , url );
81- return resourceBundle .getString ("wow.done" );
82- } else {
83- return resourceBundle .getString ("error.invalidwow" );
85+ try {
86+ if (ytMatcher .find () || dMatcher .find ()) {
87+ dataService .addWowEvent (user , url );
88+ wowList .put (user , url );
89+ return resourceBundle .getString ("wow.done" );
90+ } else {
91+ return resourceBundle .getString ("error.invalidwow" );
92+ }
93+ } catch (SQLException ex ) {
94+ return resourceBundle .getString ("error.majorerror" );
8495 }
8596 }
8697}
0 commit comments