Skip to content

Commit

Permalink
feat: added hook with Discord for sending game start messages (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagogebrimm authored Jul 19, 2022
1 parent a4d5103 commit 993502a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/me/roinujnosde/titansbattle/BaseGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ public void broadcastKey(@NotNull String key, Object... args) {
broadcast(getLang(key), args);
}

public void discordAnnounce(@NotNull String key, Object... args) {
plugin.sendDiscordMessage(getLang(key, args));
}

public void broadcast(@Nullable String message, Object... args) {
if (message == null || message.isEmpty()) {
return;
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/me/roinujnosde/titansbattle/TitansBattle.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import me.roinujnosde.titansbattle.challenges.ChallengeRequest;
import me.roinujnosde.titansbattle.dao.ConfigurationDao;
import me.roinujnosde.titansbattle.games.Game;
import me.roinujnosde.titansbattle.hooks.discord.DiscordWebhook;
import me.roinujnosde.titansbattle.hooks.papi.PlaceholderHook;
import me.roinujnosde.titansbattle.managers.*;
import me.roinujnosde.titansbattle.types.GameConfiguration;
Expand All @@ -41,13 +42,16 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;

import static net.sacredlabyrinth.phaed.simpleclans.chat.ChatHandler.plugin;

/**
* @author RoinujNosde
*
*/
public final class TitansBattle extends JavaPlugin {

Expand Down Expand Up @@ -251,4 +255,19 @@ private void loadGroupsPlugin() {
}
}

public void sendDiscordMessage(String message) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
String url = getConfig().getString("discord_webhook_url");
if (url != null && !url.isEmpty()) {
DiscordWebhook webhook = new DiscordWebhook(url);
webhook.setContent(message.replace("\n", "\\n"));
try {
webhook.execute();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Error sending webhook message", e);
}
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void start(CommandSender sender, @Values("@games") GameConfiguration game
sender.sendMessage(plugin.getLang("starting-or-started", currentGame.orElse(null)));
return;
}

gameManager.start(game);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/roinujnosde/titansbattle/games/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Warrior findKiller() {
@Override
public void start() {
super.start();
discordAnnounce("discord_game_starting");
gameManager.setCurrentGame(this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.roinujnosde.titansbattle.hooks.discord;

import com.google.gson.JsonObject;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

/**
* Class used to execute Discord Webhooks
*/
public class DiscordWebhook {

private final String url;
private String content;

/**
* Constructs a new DiscordWebhook instance
*
* @param url The webhook URL obtained in Discord
*/
public DiscordWebhook(String url) {
this.url = url;
}

public void setContent(String content) {
this.content = content;
}

public void execute() throws IOException {
JsonObject json = new JsonObject();
json.addProperty("content", content);

URL url = new URL(this.url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("User-Agent", "TitansBattle");
connection.setDoOutput(true);
connection.setRequestMethod("POST");

OutputStream stream = connection.getOutputStream();
stream.write(json.toString().getBytes());
stream.flush();
stream.close();

connection.getInputStream().close();
connection.disconnect();
}

}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ blocked_commands_everyone:
- "/f kick"
- "/marry tp"

#Send a webhook in game start, leave empty to disable
discord_webhook_url: ""

#Destinations
destinations:
general_exit:
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/language-en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ error-saving-game-config: "&b» Error saving the game config!"
error-saving-config: "&c» Error saving config!"
challenge_starting_game: " \n&b» Starting challenge in {0} seconds! \n» Needed: {1} groups, {2} players\n» Participating: {3} groups, {4} players.\n "
starting_game: "!!broadcast \n&b» Starting game in {0} seconds! Join using /tb join.\n» Needed: {1} groups, {2} players\n» Participating: {3} groups, {4} players.\n "
discord_game_starting: "» The countdown to a new game has started! \nJoin the server to participate."
players-ranking:
victories-title: "V"
kills-title: "K"
Expand All @@ -139,4 +140,4 @@ groups-ranking:
defeats-title: "D"
name-title: "Name"
title: "&b--- TB Groups Ranking ---\n \n# %name-title%n-space [%v-space%v-title |%k-space%k-title |%deaths-space%deaths-title |%defeats-space%defeats-title]"
line: "&b%position. %name%n-space [%v-space%victories |%k-space%kills |%deaths-space%deaths |%defeats-space%defeats]"
line: "&b%position. %name%n-space [%v-space%victories |%k-space%kills |%deaths-space%deaths |%defeats-space%defeats]"
1 change: 1 addition & 0 deletions src/main/resources/language-pt_BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ error-saving-game-config: "&b» Erro ao salvar a configuração do jogo!"
error-saving-config: "&c» Erro ao salvar configuração!"
challenge_starting_game: " \n&b» Iniciando desafio em {0} segundos! \n» Necessários: {1} grupos, {2} jogadores\n» Participando: {3} grupos, {4} jogadores.\n "
starting_game: "!!broadcast \n&b» Iniciando jogo em {0} segundos! Entre usando /tb join.\n» Necessários: {1} grupos, {2} jogadores\n» Participando: {3} grupos, {4} jogadores.\n "
discord_game_starting: "» Iniciada a contagem para um novo jogo! \nEntre no servidor para participar."
players-ranking:
victories-title: "V"
kills-title: "A"
Expand Down

0 comments on commit 993502a

Please sign in to comment.