Skip to content

Commit

Permalink
Merge pull request #1 from iGabyTM/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
GiansCode authored Aug 27, 2022
2 parents 9673531 + 7c5b4d9 commit 08ee9b4
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 142 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea/
target/
plugin/testServer/

*.json
*.iml
2 changes: 1 addition & 1 deletion bot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.0.0_42</version>
<version>5.0.0-alpha.18</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
Expand Down
205 changes: 96 additions & 109 deletions bot/src/main/java/io/samdev/boostsync/bot/BoostSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,107 @@
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 net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

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;
}
}
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 = JDABuilder.create(
GatewayIntent.GUILD_MEMBERS,
GatewayIntent.GUILD_MESSAGES,
GatewayIntent.MESSAGE_CONTENT
)
.disableCache(CacheFlag.ACTIVITY, CacheFlag.EMOJI, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.STICKER, CacheFlag.VOICE_STATE)
.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;
}
}

}
3 changes: 2 additions & 1 deletion bot/src/main/java/io/samdev/boostsync/bot/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public SqlCredentials getDatabaseCredentials()

public TextChannel getBotChannel()
{
return bot.getJda().getTextChannelById(config.get("bot_channel_id").getAsString());
final String id = config.get("bot_channel_id").getAsString();
return id.isEmpty() ? null : bot.getJda().getTextChannelById(id);
}

public Role getBoostingRole()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void removeCode(String code)
public void insertSyncData(UUID uuid, SyncData data)
{
asyncUpdate(
"INSERT INTO boostsync_synced VALUES (?,?,?,?);",
"INSERT INTO boostsync_synced (uuid, discord_id, boosting, last_boost_reward) VALUES (?,?,?,?);",
statement ->
{
statement.setString(1, uuid.toString());
Expand Down Expand Up @@ -159,7 +159,7 @@ private void createTables()
"`discord_id` VARCHAR(20) UNIQUE KEY NOT NULL, " +
"`boosting` BOOLEAN NOT NULL, " +
"`last_boost_reward` BIGINT NOT NULL, " +
"`one_time_reward` BOOLEAN NOT NULL" +
"`one_time_reward` BOOLEAN NOT NULL DEFAULT false" +
");"
);
}
Expand Down
24 changes: 17 additions & 7 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>

<repository>
<id>placeholderapi-repo</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>

<repository>
Expand All @@ -31,11 +31,11 @@
</repositories>

<dependencies>
<!-- Spigot API -->
<!-- Paper API -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.19.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -78,5 +78,15 @@
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>./testServer/plugins</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
21 changes: 8 additions & 13 deletions plugin/src/main/java/io/samdev/boostsync/plugin/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.samdev.boostsync.common.util.UtilString;
import io.samdev.boostsync.plugin.BoostSync;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

Expand Down Expand Up @@ -35,7 +36,7 @@ public void send(CommandSender sender, Object... params)
{
if (value != null && !value.isEmpty())
{
sender.sendMessage(UtilString.formatArgs(value, params));
sender.sendMessage(MiniMessage.miniMessage().deserialize(UtilString.formatArgs(value, params)));
}
}

Expand All @@ -44,27 +45,21 @@ public static void init(BoostSync plugin)
{
for (Message message : values())
{
Object object = plugin.getConfig().get("messages." + message.name().toLowerCase());
String value;

if (object == null)
{
plugin.getLogger().severe("value missing for message " + message.name());
continue;
if (plugin.getConfig().isList("messages." + message.name().toLowerCase())) {
value = String.join("\n", plugin.getConfig().getStringList("messages." + message.name().toLowerCase()));
} else {
value = plugin.getConfig().getString("messages." + message.name().toLowerCase());
}

String value = object instanceof String ?
(String) object :
object instanceof List<?> ?
String.join("\n", (List<String>) object) :
null;

if (value == null)
{
plugin.getLogger().severe("invalid data type for message " + message.name());
continue;
}

message.value = ChatColor.translateAlternateColorCodes('&', value);
message.value = value;
}
}
}
18 changes: 9 additions & 9 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ sync:
- "[CONSOLECOMMAND] give %player_name% emerald"

messages:
command_link_player_only: "&cOnly players can use this command"
command_link_usage: "&eUsage: /link"
command_link_already_synced: "&aYour account is already linked"
command_link_success: "&aYour code is &b{code}&a, use it within 60 seconds"
command_link_player_only: "<red>Only players can use this command"
command_link_usage: "<yellow>Usage: /link"
command_link_already_synced: "<green>Your account is already linked"
command_link_success: "<green>Your code is <aqua>{code}<green>, use it within 60 seconds"

command_unlink_permission: "boostsync.unlink"
command_unlink_no_permission: "&cYou don't have permission to use this command"
command_unlink_usage: "&eUsage: /unlink <player>"
command_unlink_not_synced: "&cThat player's account is not linked"
command_unlink_success: "&aUnlinked {player}'s account"
command_unlink_no_permission: "<red>You don't have permission to use this command"
command_unlink_usage: "<yellow>Usage: /unlink <player>"
command_unlink_not_synced: "<red>That player's account is not linked"
command_unlink_success: "<green>Unlinked {player}'s account"

account_linked: "&aYour Discord account is now linked!"
account_linked: "<green>Your Discord account is now linked!"

0 comments on commit 08ee9b4

Please sign in to comment.