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 all 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
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
defaultTasks 'build', 'shadowJar'

group = 'com.nguyenquyhy'
version = '2.4.0'
version = '2.5.1'
description = 'A plugin to connect Discord and Minecraft'
sourceCompatibility = 1.8

Expand All @@ -26,6 +26,10 @@ repositories {
name 'javacord-repo'
url 'http://repo.bastian-oppermann.de'
}
maven {
name 'twitter'
url 'http://twitter4j.org/maven2'
}
}

spongestart{
Expand All @@ -43,6 +47,9 @@ dependencies {
compile('org.spongepowered:spongeapi:4.1.0')
//compile('org.spongepowered:spongeapi:5.0.0')
compile files('libs/javacord-2.0.14-shaded.jar')
compile('org.twitter4j:twitter4j-core:4.0.4')
compile('org.twitter4j:twitter4j-async:4.0.4')
compile('org.twitter4j:twitter4j-stream:4.0.4')
// compile('de.btobastian.javacord:javacord:2.0.11:shaded')
// compile "com.github.austinv11:Discord4j:2.4.9:shaded"
// compile 'org.apache.httpcomponents:httpcore:4.3.2'
Expand All @@ -64,6 +71,9 @@ shadowJar {
include dependency('org.apache.commons:commons-lang3')
include dependency('org.apache.httpcomponents:httpcore')
include dependency('org.apache.httpcomponents:httpclient')
include dependency('org.twitter4j:twitter4j-core')
include dependency('org.twitter4j:twitter4j-async')
include dependency('org.twitter4j:twitter4j-stream')
// include dependency('com.googlecode.json-simple:json-simple')
// include dependency('org.java-websocket:Java-WebSocket')
// include dependency('net.jodah:typetools')
Expand Down
6 changes: 6 additions & 0 deletions examples/config-multiple.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
"deathTemplate": "**%s**"
}
},
"twitter": {
"twitterAccessToken": "",
"twitterAccessTokenSecret": "",
"twitterConsumerSecret": "",
"twitterConsumerKey": ""
},
{
"discordId": "DISCORD_STAFF_BROADCAST_CHANNEL_ID",
"minecraft": {
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,33 @@
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.entities.Channel;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import twitter4j.Twitter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
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.*;

/**
* 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"})
@Plugin(id = "discordbridge", name = "Discord Bridge", version = "2.5.0",
description = "A Sponge plugin to connect your Minecraft server with Discord", authors = {"Hy", "Mohron, Axle2005"})
public class DiscordBridge {

private DiscordAPI consoleClient = null;
private final Map<UUID, DiscordAPI> humanClients = new HashMap<>();
private DiscordAPI botClient = null;

private Twitter twitterAPI;

private final Set<UUID> unauthenticatedPlayers = new HashSet<>(100);

Expand All @@ -57,6 +59,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,10 +72,14 @@ public void onPreInitialization(GamePreInitializationEvent event) throws IOExcep
}

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


}



@Listener
public void onServerStop(GameStoppingServerEvent event) {
Expand All @@ -84,11 +91,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 @@ -131,6 +141,14 @@ public DiscordAPI getBotClient() {
public void setBotClient(DiscordAPI botClient) {
this.botClient = botClient;
}

public Twitter getTwitterAPI() {
return twitterAPI;
}

public void setTwitterAPI(Twitter twitter) {
this.twitterAPI = twitter;
}

public Map<UUID, DiscordAPI> getHumanClients() {
return humanClients;
Expand All @@ -148,6 +166,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,14 @@
package com.nguyenquyhy.discordbridge.discordcommands;

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

import de.btobastian.javacord.entities.Channel;

public class BanCommand {


public static void run(DiscordBridge mod, Channel channel, String content){
ChannelUtil.banPlayer(channel, content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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.ErrorMessages;
import com.nguyenquyhy.discordbridge.utils.DiscordUtil;
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.entities.Channel;
import de.btobastian.javacord.entities.User;
import de.btobastian.javacord.entities.message.Message;
import de.btobastian.javacord.entities.permissions.PermissionState;
import de.btobastian.javacord.entities.permissions.PermissionType;
import de.btobastian.javacord.entities.permissions.Permissions;
import de.btobastian.javacord.entities.permissions.Role;

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("players")) PlayerCommand.run(mod, channel);
else if(strings[0].equals("tps")) TpsCommand.run(mod, channel);
else if(strings[0].equals("kick") && discordPermCheck(user, channel, PermissionType.KICK_MEMBERS)) KickCommand.run(mod, channel, strings[1]);
else if(strings[0].equals("ban") && discordPermCheck(user, channel, PermissionType.BAN_MEMBERS)) BanCommand.run(mod,channel,strings[1]);
}

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 boolean discordPermCheck(User user, Channel channel, PermissionType perm){
for(Role role: user.getRoles(channel.getServer())){
if(role.getPermissions().getState(perm).equals(PermissionState.ALLOWED)) return true;
}
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,14 @@
package com.nguyenquyhy.discordbridge.discordcommands;

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

import de.btobastian.javacord.entities.Channel;

public class KickCommand {


public static void run(DiscordBridge mod, Channel channel, String content){
ChannelUtil.kickPlayer(channel, content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nguyenquyhy.discordbridge.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
@@ -0,0 +1,17 @@
package com.nguyenquyhy.discordbridge.discordcommands;

import org.spongepowered.api.Sponge;
import com.nguyenquyhy.discordbridge.DiscordBridge;
import com.nguyenquyhy.discordbridge.utils.ChannelUtil;

import de.btobastian.javacord.entities.Channel;

public class TpsCommand {


public static void run(DiscordBridge mod, Channel channel){
String content = (double)Sponge.getServer().getTicksPerSecond()+"/20";
ChannelUtil.sendMessage(channel, content);
}

}
8 changes: 8 additions & 0 deletions src/main/java/com/nguyenquyhy/discordbridge/hooks/Boop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nguyenquyhy.discordbridge.hooks;

public class Boop {

public static String getMessageChannelClass(){
return "flavor.pie.boop.BoopableChannel";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nguyenquyhy.discordbridge.hooks;

public class Nucleus {

public static String getStaffMessageChannelClass(){
return "io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nguyenquyhy.discordbridge.listeners;

import com.nguyenquyhy.discordbridge.DiscordBridge;
import com.nguyenquyhy.discordbridge.hooks.Boop;
import com.nguyenquyhy.discordbridge.hooks.Nucleus;
import com.nguyenquyhy.discordbridge.models.ChannelConfig;
import com.nguyenquyhy.discordbridge.models.GlobalConfig;
import com.nguyenquyhy.discordbridge.utils.ChannelUtil;
Expand Down Expand Up @@ -45,8 +47,10 @@ private void sendToDiscord(MessageChannelEvent.Chat event) {
boolean isStaffChat = false;
if (event.getChannel().isPresent()) {
MessageChannel channel = event.getChannel().get();
if (channel.getClass().getName().equals("io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel"))
if (channel.getClass().getName().equals(Nucleus.getStaffMessageChannelClass()))
isStaffChat = true;
else if(channel.getClass().getName().equals(Boop.getMessageChannelClass()))
isStaffChat = false;
else if (!channel.getClass().getName().startsWith("org.spongepowered.api.text.channel.MessageChannel"))
return; // Ignore all other types
}
Expand Down
Loading