Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mas281 committed Aug 20, 2019
0 parents commit 93f0c33
Show file tree
Hide file tree
Showing 38 changed files with 1,941 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
target/

*.iml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# BoostSync plugin and Discord bot
85 changes: 85 additions & 0 deletions bot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.samdev.boostsync</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>bot</artifactId>
<packaging>jar</packaging>

<repositories>
<repository>
<id>jcenter-repo</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>

<dependencies>
<!-- JDA -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.0.0_42</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>

<!-- Google Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>

<!-- Common -->
<dependency>
<groupId>io.samdev.boostsync</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<finalName>BoostSync-bot-${project.version}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>

<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>*.json</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>io.samdev.boostsync.bot.BoostSync</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
122 changes: 122 additions & 0 deletions bot/src/main/java/io/samdev/boostsync/bot/BoostSync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package io.samdev.boostsync.bot;

import io.samdev.boostsync.bot.boost.BoostManager;
import io.samdev.boostsync.bot.command.CommandListener;
import io.samdev.boostsync.common.database.BoostSyncDatabase;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Guild;

import static io.samdev.boostsync.bot.util.Logging.info;
import static io.samdev.boostsync.bot.util.Logging.severe;

public class BoostSync
{
public static void main(String[] args)
{
new BoostSync();
}

private BoostSync()
{
if (!loadConfig())
{
severe("Unable to load config");
return;
}

if (!connectToDatabase())
{
severe("Unable to connect to database");
return;
}

if (!connectToDiscord())
{
severe("Unable to connect to Discord");
}

new CommandListener(this);
boostManager = new BoostManager(this);
}

private BoostManager boostManager;

public BoostManager getBoostManager()
{
return boostManager;
}

private BotConfig config;

public BotConfig getConfig()
{
return config;
}

private boolean loadConfig()
{
config = new BotConfig(this);
return config.init();
}

private BoostSyncDatabase database;

public BoostSyncDatabase getDatabase()
{
return database;
}

private boolean connectToDatabase()
{
try
{
database = new BoostSyncDatabase(config.getDatabaseCredentials());
return true;
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
}

private JDA jda;

public JDA getJda()
{
return jda;
}

private Guild guild;

public Guild getGuild()
{
return guild;
}

private boolean ready = false;

private boolean connectToDiscord()
{
try
{
jda = new JDABuilder(AccountType.BOT)
.setToken(config.getBotToken())
.build();

jda.awaitReady();

info("Connected to bot " + jda.getSelfUser().getAsTag());
guild = jda.getGuildById(config.getGuildId());

return true;
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
}
}
88 changes: 88 additions & 0 deletions bot/src/main/java/io/samdev/boostsync/bot/BotConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.samdev.boostsync.bot;

import com.google.gson.JsonObject;
import io.samdev.boostsync.bot.util.Message;
import io.samdev.boostsync.bot.util.UtilFile;
import io.samdev.boostsync.bot.util.UtilJson;
import io.samdev.boostsync.common.database.SqlCredentials;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;

import java.io.File;

import static io.samdev.boostsync.bot.util.Logging.info;
import static io.samdev.boostsync.bot.util.Logging.severe;

public class BotConfig
{
private final BoostSync bot;

BotConfig(BoostSync bot)
{
this.bot = bot;
}

public String getBotToken()
{
return config.get("bot_token").getAsString();
}

public String getGuildId()
{
return config.get("guild_id").getAsString();
}

public SqlCredentials getDatabaseCredentials()
{
JsonObject dbConfig = config.getAsJsonObject("database");

return new SqlCredentials(
dbConfig.get("host").getAsString(),
dbConfig.get("port").getAsInt(),
dbConfig.get("database").getAsString(),
dbConfig.get("username").getAsString(),
dbConfig.get("password").getAsString()
);
}

public TextChannel getBotChannel()
{
return bot.getJda().getTextChannelById(config.get("bot_channel_id").getAsString());
}

public Role getBoostingRole()
{
return bot.getGuild().getRoleById(config.get("boosting_role_id").getAsString());
}

public String getSyncCommand()
{
return config.get("sync_command").getAsString();
}

private JsonObject config;

boolean init()
{
File file = new File("config.json");

if (!file.exists())
{
UtilFile.copyResource("config.json");
info("Created config file - please enter information and re-run the bot");

return false;
}

config = UtilJson.parse(file);

if (config == null)
{
severe("Error parsing config from file");
return false;
}

Message.init(config);
return true;
}
}
59 changes: 59 additions & 0 deletions bot/src/main/java/io/samdev/boostsync/bot/boost/BoostManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.samdev.boostsync.bot.boost;

import io.samdev.boostsync.bot.BoostSync;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class BoostManager extends ListenerAdapter
{
private final BoostSync bot;

public BoostManager(BoostSync bot)
{
this.bot = bot;
this.boostingRole = bot.getConfig().getBoostingRole();

bot.getJda().addEventListener(this);
}

private final Role boostingRole;

public boolean isBoosting(Member member)
{
return member.getRoles().contains(boostingRole);
}

@Override
public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event)
{
Member member = event.getMember();

if (member.getUser().isBot() || !event.getRoles().contains(boostingRole))
{
return;
}

updateBoostingStatus(member, true);
}

@Override
public void onGuildMemberRoleRemove(GuildMemberRoleRemoveEvent event)
{
Member member = event.getMember();

if (member.getUser().isBot() || !event.getRoles().contains(boostingRole))
{
return;
}

updateBoostingStatus(member, false);
}

private void updateBoostingStatus(Member member, boolean boosting)
{
bot.getDatabase().updateBoostingStatus(member.getId(), boosting);
}
}
Loading

0 comments on commit 93f0c33

Please sign in to comment.