Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes Several Issues. #68

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.event.game.state.GameStartingServerEvent;
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
import org.spongepowered.api.plugin.Plugin;

import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
Expand All @@ -33,7 +32,7 @@
* Created by Hy on 1/4/2016.
*/
@Plugin(id = "discordbridge", name = "Discord Bridge", version = "2.3.0",
description = "A Sponge plugin to connect your Minecraft server with Discord", authors = {"Hy", "Mohron"})
description = "A Sponge plugin to connect your Minecraft server with Discord", authors = {"Hy", "Mohron, Axle2005"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One PR makes you an Author? Interesting...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly :P If he wants to deny this one commit, I don't care. The rest of the PR is good, but I'm still going to be hobbling along on this plugin anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just think it's funny when contributors add themselves.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time for another commit then: Instead of author, Grand Pumba of Randomness

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LMAO.

public class DiscordBridge {

private DiscordAPI consoleClient = null;
Expand All @@ -57,6 +56,7 @@ public class DiscordBridge {
private IStorage storage;

private static DiscordBridge instance;
private int playerCount=0;

@Listener
public void onPreInitialization(GamePreInitializationEvent event) throws IOException, ObjectMappingException {
Expand All @@ -69,11 +69,12 @@ public void onPreInitialization(GamePreInitializationEvent event) throws IOExcep
}

@Listener
public void onServerStart(GameStartedServerEvent event) {
CommandRegistry.register();
public void onServerStarting(GameStartingServerEvent event) {
CommandRegistry.register();
LoginHandler.loginBotAccount();
}


@Listener
public void onServerStop(GameStoppingServerEvent event) {
if (botClient != null) {
Expand All @@ -84,11 +85,14 @@ public void onServerStop(GameStoppingServerEvent event) {
Channel channel = botClient.getChannelById(channelConfig.discordId);
if (channel != null) {
ChannelUtil.sendMessage(channel, channelConfig.discord.serverDownMessage);
ChannelUtil.setDescription(channel, "Offline");
} else {
ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId);
}
}
}


}
}

Expand Down Expand Up @@ -148,6 +152,12 @@ public void addClient(UUID player, DiscordAPI client) {
}
}

public void setPlayerCount(int change){
playerCount+=change;
}
public int getPlayerCount(){
return playerCount;
}
public void removeAndLogoutClient(UUID player) {
if (player == null) {
consoleClient.disconnect();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.nguyenquyhy.discordbridge.discordcommands;

import java.util.Optional;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import com.nguyenquyhy.discordbridge.DiscordBridge;
import com.nguyenquyhy.discordbridge.models.ChannelConfig;
import com.nguyenquyhy.discordbridge.models.GlobalConfig;
import com.nguyenquyhy.discordbridge.utils.ChannelUtil;
import com.nguyenquyhy.discordbridge.utils.ErrorMessages;
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.entities.Channel;
import de.btobastian.javacord.entities.User;
import de.btobastian.javacord.entities.message.Message;

public class CommandHandler {

private static String id = "discordbridge";

public static void handle(Optional<Player> player, String substring) {
DiscordBridge mod = DiscordBridge.getInstance();
mod.getLogger().info("Working");
queueCommand(player, substring.toLowerCase().split(" "));

}

public static void handle(Message message) {
DiscordBridge mod = DiscordBridge.getInstance();
String content = message.getContent().substring(1);
queueCommand(message.getAuthor(), message.getChannelReceiver(),mod, content.toLowerCase().split(" "));
}

private static void queueCommand(Optional<Player> player, String[] strings) {
if (!(strings.length > 1)) {
sendMessageToSource(player, 0);
return;
}

if (strings[0].equals("ban") && permCheck(player, id + "ban")) {

}

}
private static void queueCommand(User user,Channel channel,DiscordBridge mod, String[] strings) {
if(strings[0].equals("tps")) TpsCommand.run(mod);
else if(strings[0].equals("players")) PlayerCommand.run(mod, channel);

}

private static boolean permCheck(Optional<Player> player, String permission) {
if (!player.isPresent())
return true;
else if (player.get().hasPermission(permission))
return true;
else
return false;
}

private static void sendMessageToSource(Optional<Player> player, int code) {
CommandSource src;
if (player.isPresent())
src = Sponge.getServer().getPlayer(player.get().getUniqueId()).get().getCommandSource().get();
else
src = Sponge.getServer().getConsole();

if (code == 0) {
src.sendMessage(Text.of(TextColors.RED, "Not enough arguments"));
}

}

private static void runCommand(String substring) {
DiscordBridge mod = DiscordBridge.getInstance();
GlobalConfig config = mod.getConfig();

DiscordAPI client = mod.getBotClient();


for (ChannelConfig channelConfig : config.channels) {
Channel channel = client.getChannelById(channelConfig.discordId);
if (channel == null) {
ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId);
return;
}
//ChannelUtil.banPlayer(channel, substring);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nguyenquyhy.discordbridge.database.discordcommands;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;


import com.nguyenquyhy.discordbridge.DiscordBridge;
import com.nguyenquyhy.discordbridge.utils.ChannelUtil;

import de.btobastian.javacord.entities.Channel;

public class PlayerCommand {

public static void PlayerCommand(){

}
public static void run(DiscordBridge mod, Channel channel){
ChannelUtil.sendMessage(channel, "Players Currently Online: ");
for(Player player : Sponge.getServer().getOnlinePlayers()){
ChannelUtil.sendMessage(channel, player.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import com.nguyenquyhy.discordbridge.utils.TextUtil;
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.entities.Channel;

import org.apache.commons.lang3.StringUtils;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.filter.cause.Root;
import org.spongepowered.api.event.network.ClientConnectionEvent;

import java.util.Optional;
Expand All @@ -22,68 +24,74 @@
*/
public class ClientConnectionListener {
@Listener
public void onJoin(ClientConnectionEvent.Join event) {
DiscordBridge mod = DiscordBridge.getInstance();
GlobalConfig config = mod.getConfig();
public void onJoin(ClientConnectionEvent.Join event, @Root Player player) {
if (!player.hasPermission("nucleus.connectionmessages.disable")) {
DiscordBridge mod = DiscordBridge.getInstance();
GlobalConfig config = mod.getConfig();

UUID playerId = player.getUniqueId();

boolean loggingIn = false;
if (!mod.getHumanClients().containsKey(playerId)) {
loggingIn = LoginHandler.loginHumanAccount(player);
}

Optional<Player> player = event.getCause().first(Player.class);
if (player.isPresent()) {
UUID playerId = player.get().getUniqueId();
boolean loggingIn = false;
if (!mod.getHumanClients().containsKey(playerId)) {
loggingIn = LoginHandler.loginHumanAccount(player.get());
}
if (!loggingIn && mod.getBotClient() != null) {
// Use Bot client to send joined message
for (ChannelConfig channelConfig : config.channels) {
if (StringUtils.isNotBlank(channelConfig.discordId) && channelConfig.discord != null
&& StringUtils.isNotBlank(channelConfig.discord.joinedTemplate)) {
Channel channel = mod.getBotClient().getChannelById(channelConfig.discordId);
if (channel != null) {
String content = String.format(channelConfig.discord.joinedTemplate, TextUtil
.escapeForDiscord(player.getName(), channelConfig.discord.joinedTemplate, "%s"));
ChannelUtil.sendMessage(channel, content);
} else {
ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId);
}
mod.setPlayerCount(1);
ChannelUtil.setDescription(channel, "Online - Number of Players: "+mod.getPlayerCount());
}

if (!loggingIn && mod.getBotClient() != null) {
// Use Bot client to send joined message
for (ChannelConfig channelConfig : config.channels) {
if (StringUtils.isNotBlank(channelConfig.discordId)
&& channelConfig.discord != null
&& StringUtils.isNotBlank(channelConfig.discord.joinedTemplate)) {
Channel channel = mod.getBotClient().getChannelById(channelConfig.discordId);
if (channel != null) {
String content = String.format(channelConfig.discord.joinedTemplate,
TextUtil.escapeForDiscord(player.get().getName(), channelConfig.discord.joinedTemplate, "%s"));
ChannelUtil.sendMessage(channel, content);
} else {
ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId);
}
}
}
}
}
}
}

}
}

@Listener
public void onDisconnect(ClientConnectionEvent.Disconnect event) {
DiscordBridge mod = DiscordBridge.getInstance();
GlobalConfig config = mod.getConfig();
public void onDisconnect(ClientConnectionEvent.Disconnect event, @Root Player player) {
if (!player.hasPermission("nucleus.connectionmessages.disable")) {
DiscordBridge mod = DiscordBridge.getInstance();
GlobalConfig config = mod.getConfig();

Optional<Player> player = event.getCause().first(Player.class);
if (player.isPresent()) {
UUID playerId = player.get().getUniqueId();
UUID playerId = player.getUniqueId();

DiscordAPI client = mod.getHumanClients().get(playerId);
if (client == null) client = mod.getBotClient();
if (client != null) {
for (ChannelConfig channelConfig : config.channels) {
if (StringUtils.isNotBlank(channelConfig.discordId)
&& channelConfig.discord != null
&& StringUtils.isNotBlank(channelConfig.discord.leftTemplate)) {
Channel channel = client.getChannelById(channelConfig.discordId);
if (channel != null) {
String content = String.format(channelConfig.discord.leftTemplate,
TextUtil.escapeForDiscord(player.get().getName(), channelConfig.discord.leftTemplate, "%s"));
ChannelUtil.sendMessage(channel, content);
} else {
ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId);
}
}
mod.removeAndLogoutClient(playerId);
//unauthenticatedPlayers.remove(playerId);
mod.getLogger().info(player.get().getName() + " has disconnected!");
}
}
}
DiscordAPI client = mod.getHumanClients().get(playerId);
if (client == null)
client = mod.getBotClient();
if (client != null) {
for (ChannelConfig channelConfig : config.channels) {
if (StringUtils.isNotBlank(channelConfig.discordId) && channelConfig.discord != null
&& StringUtils.isNotBlank(channelConfig.discord.leftTemplate)) {
Channel channel = client.getChannelById(channelConfig.discordId);
if (channel != null) {
String content = String.format(channelConfig.discord.leftTemplate, TextUtil
.escapeForDiscord(player.getName(), channelConfig.discord.leftTemplate, "%s"));
ChannelUtil.sendMessage(channel, content);
} else {
ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId);
}
mod.setPlayerCount(-1);
ChannelUtil.setDescription(channel, "Online - Number of Players: "+mod.getPlayerCount());
}
mod.removeAndLogoutClient(playerId);
// unauthenticatedPlayers.remove(playerId);
mod.getLogger().info(player.getName() + " has disconnected!");

}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static boolean loginHumanAccount(Player player) {
} else {
client = Javacord.getApi(cachedToken, false);
}

prepareHumanClient(client, player);
return true;
}
Expand Down Expand Up @@ -320,6 +320,7 @@ private static void channelJoined(DiscordAPI client, GlobalConfig config, Channe
logger.info("Bot account has connected to Discord channel " + channelConfig.discordId + ".");
if (StringUtils.isNotBlank(channelConfig.discord.serverUpMessage)) {
ChannelUtil.sendMessage(channel, channelConfig.discord.serverUpMessage);
ChannelUtil.setDescription(channel, "Online - Number of Players: 0");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nguyenquyhy.discordbridge.logics;

import com.nguyenquyhy.discordbridge.DiscordBridge;
import com.nguyenquyhy.discordbridge.database.discordcommands.CommandHandler;
import com.nguyenquyhy.discordbridge.models.ChannelConfig;
import com.nguyenquyhy.discordbridge.models.ChannelMinecraftConfigCore;
import com.nguyenquyhy.discordbridge.models.GlobalConfig;
Expand Down Expand Up @@ -33,6 +34,7 @@ public static void discordMessageReceived(Message message) {
GlobalConfig config = mod.getConfig();

for (ChannelConfig channelConfig : config.channels) {

if (config.prefixBlacklist != null) {
for (String prefix : config.prefixBlacklist) {
if (StringUtils.isNotBlank(prefix) && message.getContent().startsWith(prefix)) {
Expand All @@ -46,6 +48,12 @@ public static void discordMessageReceived(Message message) {
if (message.getNonce() != null && message.getNonce().equals(ChannelUtil.SPECIAL_CHAR + ChannelUtil.BOT_RANDOM)) {
return;
}
if(message.getContent().startsWith("!")){

message.delete();
CommandHandler.handle(message);
return;
}
if (StringUtils.isNotBlank(channelConfig.discordId)
&& channelConfig.minecraft != null
&& message.getChannelReceiver() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public class ChannelUtil {
public static void sendMessage(Channel channel, String content) {
channel.sendMessage(content, null, false, SPECIAL_CHAR + BOT_RANDOM, null);
}
public static void setDescription(Channel channel, String content){
channel.updateTopic(content);
}
}