Skip to content

Commit

Permalink
Support for Bot account
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquyhy committed Jun 21, 2016
1 parent a68f6d8 commit b3cc371
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defaultTasks 'build', 'shadowJar'

group = 'com.nguyenquyhy'
version = '1.2.0'
description = 'An example plugin'
description = 'A plugin to connect Discord and Minecraft'
sourceCompatibility = 1.8

repositories {
Expand All @@ -24,7 +24,7 @@ repositories {
name 'sponge'
url 'http://repo.spongepowered.org/maven'
}
maven { url "http://repo.maven.apache.org/maven2" }
//maven { url "http://repo.maven.apache.org/maven2" }
//maven { url "http://dl.bintray.com/austinv11/maven" }
}

Expand All @@ -40,6 +40,7 @@ spongestart{

dependencies {
compile 'org.spongepowered:spongeapi:4.0.3'
compile files('libs/Discord4J-2.4.9-shaded.jar')
// compile "com.github.austinv11:Discord4j:2.4.9:shaded"
// compile files('libs/Discord4J-2.2.7.jar')
// compile 'org.apache.httpcomponents:httpcore:4.3.2'
Expand Down
Binary file removed libs/Discord4J-2.2.7.jar
Binary file not shown.
Binary file modified libs/Discord4J-2.4.9-shaded.jar
Binary file not shown.
19 changes: 16 additions & 3 deletions src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
@Plugin(id = "com.nguyenquyhy.spongediscord", name = "Sponge Discord", version = "1.2.0")
public class SpongeDiscord {
public static String DEBUG = "";
public static String BOT_TOKEN = "";
public static String CHANNEL_ID = "";
public static String INVITE_CODE = "";
public static String JOINED_MESSAGE = "";
Expand Down Expand Up @@ -160,7 +161,18 @@ public void onServerStart(GameStartedServerEvent event) {
getLogger().info("/discord command registered.");

String cachedToken = getStorage().getDefaultToken();
if (null != cachedToken && !cachedToken.isEmpty()) {
if (StringUtils.isNotBlank(BOT_TOKEN)) {
getLogger().info("Logging in to bot Discord account...");

try {
ClientBuilder clientBuilder = new ClientBuilder();
IDiscordClient client = clientBuilder.withToken(BOT_TOKEN).asBot().build();
prepareDefaultClient(client, null);
client.login();
} catch (DiscordException e) {
e.printStackTrace();
}
} else if (StringUtils.isNotBlank(cachedToken)) {
getLogger().info("Logging in to default Discord account...");

try {
Expand Down Expand Up @@ -219,7 +231,7 @@ public void onJoin(ClientConnectionEvent.Join event) throws URISyntaxException {
if (!loggedIn) {
unauthenticatedPlayers.add(playerId);

if (JOINED_MESSAGE != null && defaultClient != null && defaultClient.isReady()) {
if (StringUtils.isNotBlank(JOINED_MESSAGE) && defaultClient != null && defaultClient.isReady()) {
try {
IChannel channel = defaultClient.getChannelByID(CHANNEL_ID);
channel.sendMessage(String.format(JOINED_MESSAGE, player.get().getName()), NONCE, false);
Expand All @@ -240,7 +252,7 @@ public void onDisconnect(ClientConnectionEvent.Disconnect event) throws IOExcept
Optional<Player> player = event.getCause().first(Player.class);
if (player.isPresent()) {
UUID playerId = player.get().getUniqueId();
if (CHANNEL_ID != null && !CHANNEL_ID.isEmpty() && LEFT_MESSAGE != null) {
if (CHANNEL_ID != null && !CHANNEL_ID.isEmpty() && StringUtils.isNotBlank(LEFT_MESSAGE)) {
IDiscordClient client = clients.get(playerId);
if (client != null && client.isReady()) {
IChannel channel = client.getChannelByID(CHANNEL_ID);
Expand Down Expand Up @@ -305,6 +317,7 @@ public void loadConfiguration() {
configNode = configLoader.load();

DEBUG = configNode.getNode("Debug").getString();
BOT_TOKEN = ConfigUtil.readString(configNode, "BotToken", "");
CHANNEL_ID = ConfigUtil.readString(configNode, "Channel", "");
INVITE_CODE = ConfigUtil.readString(configNode, "InviteCode", "");
JOINED_MESSAGE = ConfigUtil.readString(configNode, "JoinedMessageTemplate", "_%s just joined the server_");
Expand Down

0 comments on commit b3cc371

Please sign in to comment.