Skip to content

Commit d4a495e

Browse files
authored
Merge pull request #3 from janesth/csbot-features
expanded wow feature
2 parents eca0d1b + 6f05ee8 commit d4a495e

7 files changed

Lines changed: 89 additions & 28 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<artifactId>gson</artifactId>
4444
<version>2.10.1</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>mysql</groupId>
48+
<artifactId>mysql-connector-java</artifactId>
49+
<version>8.0.33</version>
50+
</dependency>
51+
4652

4753
</dependencies>
4854
<repositories>

src/main/java/services/CsFunService.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
66
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
77

8+
import java.sql.SQLException;
89
import java.util.*;
910
import java.util.regex.Matcher;
1011
import java.util.regex.Pattern;
1112

1213
public 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
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package services;
2+
3+
import java.sql.*;
4+
import java.util.HashMap;
5+
import java.util.Properties;
6+
7+
public class DataService {
8+
Properties properties;
9+
Connection connection;
10+
Statement statement;
11+
12+
public DataService(Properties properties) throws SQLException {
13+
this.properties = properties;
14+
connection = DriverManager.getConnection(properties.getProperty("db.url"));
15+
statement = connection.createStatement();
16+
}
17+
18+
public void setupConnection() throws SQLException {
19+
String query = "CREATE TABLE IF NOT EXISTS wow (wow_id int PRIMARY KEY AUTO_INCREMENT, username varchar(50) NOT NULL, url varchar(200) NOT NULL)";
20+
statement.execute(query);
21+
}
22+
23+
public void addWowEvent(String username, String url) throws SQLException {
24+
String query = "INSERT INTO wow(username, url) VALUES('" + username + "', '" + url + "');";
25+
statement.execute(query);
26+
}
27+
28+
public HashMap<String, String> returnAllWowEntries() throws SQLException{
29+
HashMap<String, String> returnMap = new HashMap<String, String>();
30+
String query = "SELECT * FROM wow";
31+
try (ResultSet resultSet = statement.executeQuery(query)) {
32+
while(resultSet.next()) {
33+
returnMap.put(resultSet.getString("username"), resultSet.getString("url"));
34+
}
35+
}
36+
return returnMap;
37+
}
38+
}

src/main/resources/config.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ steam.api=
1919

2020
## Various Settings
2121
server.delay=
22-
csgo.maps=
22+
csgo.maps=
23+
24+
## Database setting
25+
db.url=

src/main/resources/localization.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error.interruptedException=Connection issues. :(
2-
error.ioException=Wow! Something major just broke. Sorry. :(
32
error.privacySettings=No stats for %s could be loaded. (Steam Privacy Settings?)
43
error.wrongQueryParameters=The players were not submitted properly.
54
error.cantwowabot=You can not see the highlight of a bot.
65
error.hasnowow=This user has no highlights. Add one with ``/wow url``.
7-
error.invalidwow=The submitted url is not valid.
6+
error.invalidwow=The submitted url is not valid. Only Discord or YouTube links are accepted.
7+
error.majorerror=Something broke. We are working on this issue.
88

99
stats.title=__Stats for %s__
1010
stats.author=Powered by YOINC.
@@ -19,6 +19,7 @@ compare.title=__Comparison between %s and %t__
1919
compare.equal=%s both.
2020

2121
wow.done=A highlight has been added.
22+
wow.highlightMessage=%s's wow moment:
2223
wow.messageSent=A message was sent to csgo-stuff.
2324
2425
command.map.description=Change the map

src/main/resources/localization_de.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error.interruptedException=Leider gab es Verbindungsprobleme. :(
2-
error.ioException=Wow! Da ging etwas kaputt. Sorry. :(
32
error.privacySettings=Für %s können keine Stats geladen werden. (Steam Privacy Settings?)
43
error.wrongQueryParameters=Die Spieler wurden nicht korrekt mitgegeben.
54
error.cantwowabot=Bots haben keine Highlights. :(
65
error.hasnowow=Dieser Benutzer hat kein Highlight. Füge eines hinzu mit ``/wow url``.
7-
error.invalidwow=Die URL ist nicht gültig.
6+
error.invalidwow=Die URL ist nicht gültig. Nur Discord oder YouTube Links sind erlaubt.
7+
error.majorerror=Etwas lief schief. Wir arbeiten dran.
88

99
stats.title=__Stats für %s__
1010
stats.author=Powered by YOINC.
@@ -19,6 +19,7 @@ compare.title=__Vergleich zwischen %s und %t__
1919
compare.equal=%s für beide.
2020

2121
wow.done=Ein Highlight wurde hinzugefügt.
22+
wow.highlightMessage=%s's WOW Moment:
2223
wow.messageSent=Es gibt eine neue Nachricht in csgo-stuff.
2324
2425
command.map.description=Ändere die Map

src/main/resources/localization_en.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error.interruptedException=Connection issues. :(
2-
error.ioException=Wow! Something major just broke. Sorry. :(
32
error.privacySettings=No stats for %s could be loaded. (Steam Privacy Settings?)
43
error.wrongQueryParameters=The players were not submitted properly.
54
error.cantwowabot=You can not see the highlight of a bot.
65
error.hasnowow=This user has no highlights. Add one with ``/wow url``.
7-
error.invalidwow=The submitted url is not valid.
6+
error.invalidwow=The submitted url is not valid. Only Discord or YouTube links are accepted.
7+
error.majorerror=Something broke. We are working on this issue.
88

99
stats.title=__Stats for %s__
1010
stats.author=Powered by YOINC.
@@ -19,6 +19,7 @@ compare.title=__Comparison between %s and %t__
1919
compare.equal=%s both.
2020

2121
wow.done=A highlight has been added.
22+
wow.highlightMessage=%s's wow moment:
2223
wow.messageSent=A message was sent to csgo-stuff.
2324
2425
command.map.description=Change the map

0 commit comments

Comments
 (0)