Add DiscordSRV support.

This commit is contained in:
cnaude 2016-09-19 22:32:21 -07:00
parent 79f3b80a48
commit 32ac3e2057
9 changed files with 259 additions and 20 deletions

13
pom.xml
View File

@ -84,6 +84,11 @@
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>Scarsz-Jenkins</id>
<url>http://scarsz.tech:8080/plugin/repository/everything/</url>
</repository>
</repositories>
<dependencies>
@ -403,6 +408,14 @@
<scope>provided</scope>
</dependency>
<!-- DiscordSRV -->
<dependency>
<groupId>com.scarsz.discordsrv</groupId>
<artifactId>DiscordSRV</artifactId>
<version>11.0</version>
<scope>provided</scope>
</dependency>
<!-- Testing only -->
<dependency>
<groupId>junit</groupId>

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc.GameListeners;
import com.cnaude.purpleirc.PurpleBot;
import com.cnaude.purpleirc.PurpleIRC;
import com.scarsz.discordsrv.api.DiscordSRVListener;
import com.scarsz.discordsrv.api.events.ProcessChatEvent;
import com.scarsz.discordsrv.jda.events.message.MessageReceivedEvent;
import org.bukkit.event.EventHandler;
/**
*
* @author Chris Naude
*/
public class DiscordListener extends DiscordSRVListener {
private final PurpleIRC plugin;
/**
*
* @param plugin the PurpleIRC plugin
*/
public DiscordListener(PurpleIRC plugin) {
this.plugin = plugin;
}
/**
*
* @param event
*/
@EventHandler
public void onProcessChatEvent(ProcessChatEvent event) {
}
@Override
public void onDiscordMessageReceived(MessageReceivedEvent event) {
plugin.logDebug("onDiscordMessageReceived: " + event.getMessage().getContent());
for (PurpleBot ircBot : plugin.ircBots.values()) {
ircBot.discordChat(event.getMessage().getAuthor().getUsername(),
event.getMessage().getChannelId(),
event.getMessage().getContent());
}
}
/*
@Override
public void onProcessChat(ProcessChatEvent event) {
plugin.logDebug("onProcessChat: " + event.message);
for (PurpleBot ircBot : plugin.ircBots.values()) {
ircBot.discordChat(event.sender, event.channel, event.message);
}
}
*/
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc.Hooks;
import com.cnaude.purpleirc.GameListeners.DiscordListener;
import com.cnaude.purpleirc.PurpleIRC;
import com.scarsz.discordsrv.DiscordSRV;
import com.scarsz.discordsrv.api.DiscordSRVAPI;
import com.scarsz.discordsrv.api.DiscordSRVListener;
import com.scarsz.discordsrv.jda.entities.TextChannel;
/**
*
* @author Chris Naude
*/
public class DiscordSRVHook {
private final PurpleIRC plugin;
private final DiscordSRVListener discordListener;
/**
*
* @param plugin the PurpleIRC plugin
*/
public DiscordSRVHook(PurpleIRC plugin) {
this.plugin = plugin;
discordListener = new DiscordListener(this.plugin);
DiscordSRVAPI.addListener(discordListener);
}
public void removeListener() {
DiscordSRVAPI.removeListener(discordListener);
}
public void sendMessage(String channelName, String message) {
TextChannel textChannel = DiscordSRV.getTextChannelFromChannelName(channelName);
if (textChannel != null) {
plugin.logDebug("DiscordSRVHook[" + channelName + "]: " + message);
textChannel.sendMessage(message);
} else {
plugin.logDebug("DiscordSRVHook: " + message);
DiscordSRV.sendMessageToChatChannel(message);
}
}
}

View File

@ -153,6 +153,7 @@ public final class PurpleBot {
public CaseInsensitiveMap<String> heroChannel;
public CaseInsensitiveMap<String> ventureChatChannel;
public CaseInsensitiveMap<String> townyChannel;
public CaseInsensitiveMap<String> discordChannel;
public CaseInsensitiveMap<Collection<String>> opsList;
public CaseInsensitiveMap<Collection<String>> banList;
public CaseInsensitiveMap<Collection<String>> voicesList;
@ -239,6 +240,7 @@ public final class PurpleBot {
this.heroChannel = new CaseInsensitiveMap<>();
this.ventureChatChannel = new CaseInsensitiveMap<>();
this.townyChannel = new CaseInsensitiveMap<>();
this.discordChannel = new CaseInsensitiveMap<>();
this.invalidCommandCTCP = new CaseInsensitiveMap<>();
this.logIrcToHeroChat = new CaseInsensitiveMap<>();
this.logIrcToVentureChat = new CaseInsensitiveMap<>();
@ -950,6 +952,9 @@ public final class PurpleBot {
townyChannel.put(channelName, config.getString("channels." + enChannelName + ".towny-channel", ""));
plugin.logDebug(" TownyChannel => " + townyChannel.get(channelName));
discordChannel.put(channelName, config.getString("channels." + enChannelName + ".discord-channel", ""));
plugin.logDebug(" DiscordChannel => " + discordChannel.get(channelName));
logIrcToHeroChat.put(channelName, config.getBoolean("channels." + enChannelName + ".log-irc-to-hero-chat", false));
plugin.logDebug(" LogIrcToHeroChat => " + logIrcToHeroChat.get(channelName));
@ -1465,6 +1470,28 @@ public final class PurpleBot {
}
}
/**
* Called from Discord ProcessChatEvent
*
* @param username
* @param channelId
* @param message
*/
public void discordChat(String username, String channelId, String message) {
if (!this.isConnected()) {
return;
}
for (String channelName : botChannels) {
if (isMessageEnabled(channelName, TemplateName.GAME_DISCORD_CHAT)) {
asyncIRCMessage(channelName, plugin.tokenizer
.gameChatToIRCTokenizer(username, plugin.getMessageTemplate(
botNick, channelName, TemplateName.GAME_DISCORD_CHAT), message)
.replace("%CHANNEL%", channelId)
);
}
}
}
/**
* Called from SimpleTicketEvent
*
@ -2867,6 +2894,21 @@ public final class PurpleBot {
}
}
/*
Send messages to VentureChat if enabled
*/
if (isMessageEnabled(channelName, TemplateName.IRC_DISCORD_CHAT) && plugin.discHook != null) {
String discordChannelName = discordChannel.get(channelName);
String discordTemplate = plugin.getMessageTemplate(botNick, channelName, TemplateName.IRC_DISCORD_CHAT);
plugin.logDebug("broadcastChat [Discord]: " + discordChannelName + ": " + discordTemplate);
String rawDiscordMessage = filterMessage(
plugin.tokenizer.ircChatToDiscordTokenizer(this, user, channel, discordTemplate, message, discordChannelName), channelName);
if (!rawDiscordMessage.isEmpty()) {
plugin.discHook.sendMessage(discordChannelName, rawDiscordMessage);
messageSent = true;
}
}
/*
Notify IRC user that message was sent.
*/

View File

@ -20,6 +20,7 @@ import com.cnaude.purpleirc.Events.IRCMessageEvent;
import com.cnaude.purpleirc.GameListeners.AdminChatListener;
import com.cnaude.purpleirc.GameListeners.CleverNotchListener;
import com.cnaude.purpleirc.GameListeners.DeathMessagesListener;
import com.cnaude.purpleirc.GameListeners.DiscordListener;
import com.cnaude.purpleirc.GameListeners.DynmapListener;
import com.cnaude.purpleirc.GameListeners.EssentialsListener;
import com.cnaude.purpleirc.GameListeners.GamePlayerChatListener;
@ -45,6 +46,7 @@ import com.cnaude.purpleirc.GameListeners.TownyChatListener;
import com.cnaude.purpleirc.GameListeners.VanishNoPacketListener;
import com.cnaude.purpleirc.Hooks.AdminPrivateChatHook;
import com.cnaude.purpleirc.Hooks.CommandBookHook;
import com.cnaude.purpleirc.Hooks.DiscordSRVHook;
import com.cnaude.purpleirc.Hooks.DynmapHook;
import com.cnaude.purpleirc.Hooks.FactionChatHook;
import com.cnaude.purpleirc.Hooks.GriefPreventionHook;
@ -68,6 +70,7 @@ import com.cnaude.purpleirc.Utilities.UpdateChecker;
import com.google.common.base.Joiner;
import com.onarandombox.MultiverseCore.api.MVPlugin;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.scarsz.discordsrv.api.DiscordSRVAPI;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
@ -192,6 +195,7 @@ public class PurpleIRC extends JavaPlugin {
public FactionChatHook fcHook;
public TownyChatHook tcHook;
public VentureChatHook vcHook;
public DiscordSRVHook discHook;
public DynmapHook dynmapHook;
public JobsHook jobsHook;
public AdminPrivateChatHook adminPrivateChatHook;
@ -245,6 +249,7 @@ public class PurpleIRC extends JavaPlugin {
final String PL_HEROCHAT = "Herochat";
final String PL_GRIEFPREVENTION = "GriefPrevention";
final String PL_PLACEHOLDERAPI = "PlaceholderAPI";
final String PL_DISCORDSRV = "DiscordSRV";
List<String> hookList = new ArrayList<>();
public static final String PURPLETAG = "UHVycGxlSVJDCg==";
public static final String TOWNYTAG = "VG93bnlDaGF0Cg==";
@ -254,6 +259,7 @@ public class PurpleIRC extends JavaPlugin {
public String smsgReplyAlias = "/r";
public CaseInsensitiveMap<String> privateMsgReply;
public PurpleIRC() {
this.MAINCONFIG = "MAIN-CONFIG";
this.sampleFileName = "SampleBot.yml";
@ -350,6 +356,10 @@ public class PurpleIRC extends JavaPlugin {
*/
@Override
public void onDisable() {
if (discHook != null) {
logDebug("Disabling discHook ...");
discHook.removeListener();
}
if (channelWatcher != null) {
logDebug("Disabling channelWatcher ...");
channelWatcher.cancel();
@ -1745,6 +1755,14 @@ public class PurpleIRC extends JavaPlugin {
} else {
hookList.add(hookFormat(PL_PLACEHOLDERAPI, false));
}
if (isPluginEnabled(PL_DISCORDSRV)) {
discHook = new DiscordSRVHook(this);
} else {
hookList.add(hookFormat(PL_DISCORDSRV, false));
}
}
public void getPurpleHooks(CommandSender sender, boolean colors) {

View File

@ -149,6 +149,9 @@ public class TemplateName {
public final static String GAME_ADMIN_CHAT = "game-a-chat";
public final static String IRC_ADMIN_CHAT = "irc-a-chat";
public final static String GAME_DISCORD_CHAT = "discord-chat";
public final static String IRC_DISCORD_CHAT = "irc-discord-chat";
public final static String FAKE_JOIN = "fake-join";
public final static String FAKE_QUIT = "fake-quit";

View File

@ -306,6 +306,34 @@ public class ChatTokenizer {
.replace("%CHANNEL%", channel.getName()));
}
/**
* IRC to Discord channel tokenizer
*
* @param ircBot
* @param user
* @param channel
* @param template
* @param message
* @param hChannel
* @return
*/
public String ircChatToDiscordTokenizer(PurpleBot ircBot, User user, org.pircbotx.Channel channel, String template, String message, String hChannel) {
String ircNick = user.getNick();
String tmpl;
Player player = this.getPlayer(ircNick);
if (player != null) {
tmpl = playerTokenizer(player, template);
} else {
tmpl = playerTokenizer(ircNick, template);
}
return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user, ircBot)
.replace("%DISCORDCHANNEL%", hChannel)
.replace("%NICKPREFIX%", ircBot.getNickPrefix(user, channel))
.replace("%CHANNELPREFIX%", ircBot.getChannelPrefix(channel))
.replace("%MESSAGE%", message)
.replace("%CHANNEL%", channel.getName()));
}
/**
* IRC to Hero chat channel tokenizer
*

View File

@ -148,6 +148,10 @@ message-format:
game-a-chat: '[%WORLD%] <%NAME%> -> [AdminChat]: %MESSAGE%'
# AdminPrivateChat message from IRC to game
irc-a-chat: '[AdminChat] [&4IRC&r] %MESSAGE%'
# Messages from Discord
discord-chat: '[Discord] %MESSAGE%'
# Messages from IRC to Discord
irc-discord-chat: '[&4IRC&r]<%NAME%> %MESSAGE%'
# Message template for Clevernotch bot to IRC messages
clever-send: '[&4BOT]<%NAME%> %MESSAGE%'
# Message templates for mcMMO to IRC messages

View File

@ -24,6 +24,7 @@ softdepend:
- PremiumVanish
- VanishNoPacket
- PlaceholderAPI
- DiscordSRV
commands:
irc:
description: Various irc commands