Skip to content

Commit 93f0c33

Browse files
committed
Initial commit
0 parents  commit 93f0c33

38 files changed

Lines changed: 1941 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
target/
3+
4+
*.iml

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# BoostSync plugin and Discord bot

bot/pom.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.samdev.boostsync</groupId>
9+
<artifactId>parent</artifactId>
10+
<version>1.0</version>
11+
</parent>
12+
13+
<artifactId>bot</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<repositories>
17+
<repository>
18+
<id>jcenter-repo</id>
19+
<url>https://jcenter.bintray.com</url>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<!-- JDA -->
25+
<dependency>
26+
<groupId>net.dv8tion</groupId>
27+
<artifactId>JDA</artifactId>
28+
<version>4.0.0_42</version>
29+
<exclusions>
30+
<exclusion>
31+
<groupId>club.minnced</groupId>
32+
<artifactId>opus-java</artifactId>
33+
</exclusion>
34+
</exclusions>
35+
<scope>compile</scope>
36+
</dependency>
37+
38+
<!-- Google Gson -->
39+
<dependency>
40+
<groupId>com.google.code.gson</groupId>
41+
<artifactId>gson</artifactId>
42+
<version>2.8.5</version>
43+
<scope>compile</scope>
44+
</dependency>
45+
46+
<!-- Common -->
47+
<dependency>
48+
<groupId>io.samdev.boostsync</groupId>
49+
<artifactId>common</artifactId>
50+
<version>1.0</version>
51+
<scope>compile</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<finalName>BoostSync-bot-${project.version}</finalName>
57+
<sourceDirectory>src/main/java</sourceDirectory>
58+
59+
<resources>
60+
<resource>
61+
<targetPath>.</targetPath>
62+
<filtering>true</filtering>
63+
<directory>${basedir}/src/main/resources/</directory>
64+
<includes>
65+
<include>*.json</include>
66+
</includes>
67+
</resource>
68+
</resources>
69+
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-jar-plugin</artifactId>
74+
<version>3.1.2</version>
75+
<configuration>
76+
<archive>
77+
<manifest>
78+
<mainClass>io.samdev.boostsync.bot.BoostSync</mainClass>
79+
</manifest>
80+
</archive>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
</project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package io.samdev.boostsync.bot;
2+
3+
import io.samdev.boostsync.bot.boost.BoostManager;
4+
import io.samdev.boostsync.bot.command.CommandListener;
5+
import io.samdev.boostsync.common.database.BoostSyncDatabase;
6+
import net.dv8tion.jda.api.AccountType;
7+
import net.dv8tion.jda.api.JDA;
8+
import net.dv8tion.jda.api.JDABuilder;
9+
import net.dv8tion.jda.api.entities.Guild;
10+
11+
import static io.samdev.boostsync.bot.util.Logging.info;
12+
import static io.samdev.boostsync.bot.util.Logging.severe;
13+
14+
public class BoostSync
15+
{
16+
public static void main(String[] args)
17+
{
18+
new BoostSync();
19+
}
20+
21+
private BoostSync()
22+
{
23+
if (!loadConfig())
24+
{
25+
severe("Unable to load config");
26+
return;
27+
}
28+
29+
if (!connectToDatabase())
30+
{
31+
severe("Unable to connect to database");
32+
return;
33+
}
34+
35+
if (!connectToDiscord())
36+
{
37+
severe("Unable to connect to Discord");
38+
}
39+
40+
new CommandListener(this);
41+
boostManager = new BoostManager(this);
42+
}
43+
44+
private BoostManager boostManager;
45+
46+
public BoostManager getBoostManager()
47+
{
48+
return boostManager;
49+
}
50+
51+
private BotConfig config;
52+
53+
public BotConfig getConfig()
54+
{
55+
return config;
56+
}
57+
58+
private boolean loadConfig()
59+
{
60+
config = new BotConfig(this);
61+
return config.init();
62+
}
63+
64+
private BoostSyncDatabase database;
65+
66+
public BoostSyncDatabase getDatabase()
67+
{
68+
return database;
69+
}
70+
71+
private boolean connectToDatabase()
72+
{
73+
try
74+
{
75+
database = new BoostSyncDatabase(config.getDatabaseCredentials());
76+
return true;
77+
}
78+
catch (Exception ex)
79+
{
80+
ex.printStackTrace();
81+
return false;
82+
}
83+
}
84+
85+
private JDA jda;
86+
87+
public JDA getJda()
88+
{
89+
return jda;
90+
}
91+
92+
private Guild guild;
93+
94+
public Guild getGuild()
95+
{
96+
return guild;
97+
}
98+
99+
private boolean ready = false;
100+
101+
private boolean connectToDiscord()
102+
{
103+
try
104+
{
105+
jda = new JDABuilder(AccountType.BOT)
106+
.setToken(config.getBotToken())
107+
.build();
108+
109+
jda.awaitReady();
110+
111+
info("Connected to bot " + jda.getSelfUser().getAsTag());
112+
guild = jda.getGuildById(config.getGuildId());
113+
114+
return true;
115+
}
116+
catch (Exception ex)
117+
{
118+
ex.printStackTrace();
119+
return false;
120+
}
121+
}
122+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.samdev.boostsync.bot;
2+
3+
import com.google.gson.JsonObject;
4+
import io.samdev.boostsync.bot.util.Message;
5+
import io.samdev.boostsync.bot.util.UtilFile;
6+
import io.samdev.boostsync.bot.util.UtilJson;
7+
import io.samdev.boostsync.common.database.SqlCredentials;
8+
import net.dv8tion.jda.api.entities.Role;
9+
import net.dv8tion.jda.api.entities.TextChannel;
10+
11+
import java.io.File;
12+
13+
import static io.samdev.boostsync.bot.util.Logging.info;
14+
import static io.samdev.boostsync.bot.util.Logging.severe;
15+
16+
public class BotConfig
17+
{
18+
private final BoostSync bot;
19+
20+
BotConfig(BoostSync bot)
21+
{
22+
this.bot = bot;
23+
}
24+
25+
public String getBotToken()
26+
{
27+
return config.get("bot_token").getAsString();
28+
}
29+
30+
public String getGuildId()
31+
{
32+
return config.get("guild_id").getAsString();
33+
}
34+
35+
public SqlCredentials getDatabaseCredentials()
36+
{
37+
JsonObject dbConfig = config.getAsJsonObject("database");
38+
39+
return new SqlCredentials(
40+
dbConfig.get("host").getAsString(),
41+
dbConfig.get("port").getAsInt(),
42+
dbConfig.get("database").getAsString(),
43+
dbConfig.get("username").getAsString(),
44+
dbConfig.get("password").getAsString()
45+
);
46+
}
47+
48+
public TextChannel getBotChannel()
49+
{
50+
return bot.getJda().getTextChannelById(config.get("bot_channel_id").getAsString());
51+
}
52+
53+
public Role getBoostingRole()
54+
{
55+
return bot.getGuild().getRoleById(config.get("boosting_role_id").getAsString());
56+
}
57+
58+
public String getSyncCommand()
59+
{
60+
return config.get("sync_command").getAsString();
61+
}
62+
63+
private JsonObject config;
64+
65+
boolean init()
66+
{
67+
File file = new File("config.json");
68+
69+
if (!file.exists())
70+
{
71+
UtilFile.copyResource("config.json");
72+
info("Created config file - please enter information and re-run the bot");
73+
74+
return false;
75+
}
76+
77+
config = UtilJson.parse(file);
78+
79+
if (config == null)
80+
{
81+
severe("Error parsing config from file");
82+
return false;
83+
}
84+
85+
Message.init(config);
86+
return true;
87+
}
88+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.samdev.boostsync.bot.boost;
2+
3+
import io.samdev.boostsync.bot.BoostSync;
4+
import net.dv8tion.jda.api.entities.Member;
5+
import net.dv8tion.jda.api.entities.Role;
6+
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
7+
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
8+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
9+
10+
public class BoostManager extends ListenerAdapter
11+
{
12+
private final BoostSync bot;
13+
14+
public BoostManager(BoostSync bot)
15+
{
16+
this.bot = bot;
17+
this.boostingRole = bot.getConfig().getBoostingRole();
18+
19+
bot.getJda().addEventListener(this);
20+
}
21+
22+
private final Role boostingRole;
23+
24+
public boolean isBoosting(Member member)
25+
{
26+
return member.getRoles().contains(boostingRole);
27+
}
28+
29+
@Override
30+
public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event)
31+
{
32+
Member member = event.getMember();
33+
34+
if (member.getUser().isBot() || !event.getRoles().contains(boostingRole))
35+
{
36+
return;
37+
}
38+
39+
updateBoostingStatus(member, true);
40+
}
41+
42+
@Override
43+
public void onGuildMemberRoleRemove(GuildMemberRoleRemoveEvent event)
44+
{
45+
Member member = event.getMember();
46+
47+
if (member.getUser().isBot() || !event.getRoles().contains(boostingRole))
48+
{
49+
return;
50+
}
51+
52+
updateBoostingStatus(member, false);
53+
}
54+
55+
private void updateBoostingStatus(Member member, boolean boosting)
56+
{
57+
bot.getDatabase().updateBoostingStatus(member.getId(), boosting);
58+
}
59+
}

0 commit comments

Comments
 (0)