Skip to content

Commit

Permalink
Discord bot: Add support for opchannels (channels which only staff ch…
Browse files Browse the repository at this point in the history
…at is read/sent)
  • Loading branch information
UnknownShadow200 committed Apr 16, 2021
1 parent c07c2bc commit 1ee8bcc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
17 changes: 3 additions & 14 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Expand Up @@ -26,8 +26,6 @@ namespace MCGalaxy.Modules.Relay.Discord {

public sealed class DiscordBot : RelayBot {
public bool Disconnected;
string[] readChannels;
string[] sendChannels;
string[] operatorIds;

DiscordApiClient api;
Expand All @@ -42,9 +40,9 @@ public sealed class DiscordBot : RelayBot {
config = conf;
socket = new DiscordWebsocket();

readChannels = conf.ReadChannels.SplitComma();
sendChannels = conf.SendChannels.SplitComma();
operatorIds = conf.OperatorUsers.SplitComma();
Channels = conf.Channels.SplitComma();
OpChannels = conf.OpChannels.SplitComma();
operatorIds = conf.OperatorUsers.SplitComma();

socket.Token = config.BotToken;
socket.Handler = HandleEvent;
Expand Down Expand Up @@ -140,15 +138,6 @@ public sealed class DiscordBot : RelayBot {
// TODO: implement this
}

public override void SendPublicMessage(string message) {
foreach (string chan in sendChannels) {
MessageChannel(chan, message);
}
}

// TODO implement this
public override void SendStaffMessage(string message) { }

void HandlePlayerAction(Player p, PlayerAction action, string message, bool stealth) {
if (action != PlayerAction.Hide && action != PlayerAction.Unhide) return;
socket.SendUpdateStatus();
Expand Down
18 changes: 9 additions & 9 deletions MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs
Expand Up @@ -22,20 +22,20 @@
namespace MCGalaxy.Modules.Relay.Discord {

public sealed class DiscordConfig {
[ConfigString("bot-token", null, "", true)]
public string BotToken = "";
[ConfigString("read-channel-ids", null, "", true)]
public string ReadChannels = "";
[ConfigString("send-channel-ids", null, "", true)]
public string SendChannels = "";
[ConfigString("operator-user-ids", null, "", true)]
public string OperatorUsers = "";

[ConfigBool("enabled", null, false)]
public bool Enabled;
[ConfigString("bot-token", null, "", true)]
public string BotToken = "";
[ConfigString("status-message", null, "with {PLAYERS} players")]
public string Status = "with {PLAYERS} players";

[ConfigString("channel-ids", null, "", true)]
public string Channels = "";
[ConfigString("op-channel-ids", null, "", true)]
public string OpChannels = "";
[ConfigString("operator-user-ids", null, "", true)]
public string OperatorUsers = "";

const string file = "properties/discordbot.properties";
static ConfigElement[] cfg;

Expand Down
47 changes: 14 additions & 33 deletions MCGalaxy/Modules/Relay/IRC/IRCBot.cs
Expand Up @@ -32,7 +32,6 @@ public enum IRCControllerVerify { None, HalfOp, OpChannel };
/// <summary> Manages a connection to an IRC server, and handles associated events. </summary>
public sealed class IRCBot : RelayBot {
internal Connection connection;
internal string[] channels, opchannels;
internal string nick, server;
internal bool resetting;
internal byte retries;
Expand All @@ -50,21 +49,14 @@ public sealed class IRCBot : RelayBot {
InitConnectionState();
}


/// <summary> Sends an IRC message to either the normal or operator IRC channel. </summary>
public void Say(string message, bool opchat) {
string[] chans = opchat ? opchannels : channels;
foreach (string chan in chans) { Message(chan, message); }
}

/// <summary> Sends an IRC private message to the given user. </summary>
public void Pm(string user, string message) {
public override void MessageUser(RelayUser user, string message) {
if (!Enabled) return;
message = ConvertMessage(message);
connection.Sender.PrivateMessage(user, message);
connection.Sender.PrivateMessage(user.Nick, message);
}

public void Message(string channel, string message) {
public override void MessageChannel(string channel, string message) {
if (!Enabled) return;
message = ConvertMessage(message);
connection.Sender.PublicMessage(channel, message);
Expand Down Expand Up @@ -117,10 +109,7 @@ public sealed class IRCBot : RelayBot {
/// <summary> Returns whether this bot is connected to IRC. </summary>
public bool Connected { get { return connection != null && connection.Connected; } }
/// <summary> Returns whether this bot is connected to IRC and is able to send messages. </summary>
public bool Enabled { get { return Server.Config.UseIRC && connection != null && connection.Connected; } }

public override void SendPublicMessage(string message) { Say(message, false); }
public override void SendStaffMessage(string message) { Say(message, true); }
public bool Enabled { get { return Server.Config.UseIRC && connection != null && connection.Connected; } }

void InitConnectionState() {
if (!Server.Config.UseIRC || connection != null) return;
Expand All @@ -129,9 +118,9 @@ public sealed class IRCBot : RelayBot {
}

void UpdateState() {
channels = Server.Config.IRCChannels.SplitComma();
opchannels = Server.Config.IRCOpChannels.SplitComma();
nick = Server.Config.IRCNick.Replace(" ", "");
Channels = Server.Config.IRCChannels.SplitComma();
OpChannels = Server.Config.IRCOpChannels.SplitComma();
nick = Server.Config.IRCNick.Replace(" ", "");
server = Server.Config.IRCServer;

args = new ConnectionArgs(nick, server);
Expand Down Expand Up @@ -215,14 +204,6 @@ public sealed class IRCBot : RelayBot {
return CheckIRCCommand(user.Nick, cmdName, out error);
}

public override void MessageChannel(string channel, string message) {
Message(channel, message);
}

public override void MessageUser(RelayUser user, string message) {
Pm(user.Nick, message);
}

// TODO remove
public void HandlePublic(string nick, string channel, string message, bool opchat) {
RelayUser user = new RelayUser();
Expand Down Expand Up @@ -313,12 +294,12 @@ public sealed class IRCBot : RelayBot {

void DoJoinLeaveMessage(string nick, string verb, string channel) {
Logger.Log(LogType.RelayActivity, "{0} {1} channel {2}", nick, verb, channel);
string which = opchannels.CaselessContains(channel) ? " operator" : "";
string which = OpChannels.CaselessContains(channel) ? " operator" : "";
MessageInGame(nick, string.Format("&I(IRC) {0} {1} the{2} channel", nick, verb, which));
}

void Listener_OnQuit(UserInfo user, string reason) {
// Old bot was disconnected, try to reclaim it.
// Old bot was disconnected, try to reclaim it
if (user.Nick == nick) connection.Sender.Nick(nick);
nicks.OnLeft(user);

Expand All @@ -339,7 +320,7 @@ public sealed class IRCBot : RelayBot {
void Listener_OnPublic(UserInfo user, string channel, string message) {
message = message.TrimEnd();
if (message.Length == 0) return;
bool opchat = opchannels.CaselessContains(channel);
bool opchat = OpChannels.CaselessContains(channel);

message = IRCBot.ParseMessage(message);
HandlePublic(user.Nick, channel, message, opchat);
Expand All @@ -350,10 +331,10 @@ public sealed class IRCBot : RelayBot {
if (!Server.ircControllers.Contains(nick)) return false;

bool foundAtAll = false;
foreach (string chan in channels) {
foreach (string chan in Channels) {
if (nicks.VerifyNick(chan, nick, ref error, ref foundAtAll)) return true;
}
foreach (string chan in opchannels) {
foreach (string chan in OpChannels) {
if (nicks.VerifyNick(chan, nick, ref error, ref foundAtAll)) return true;
}

Expand All @@ -378,8 +359,8 @@ public sealed class IRCBot : RelayBot {

void JoinChannels() {
Logger.Log(LogType.RelayActivity, "Joining IRC channels...");
foreach (string chan in channels) { Join(chan); }
foreach (string chan in opchannels) { Join(chan); }
foreach (string chan in Channels) { Join(chan); }
foreach (string chan in OpChannels) { Join(chan); }
}

void Listener_OnPrivateNotice(UserInfo user, string notice) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Relay/IRC/IRCNickList.cs
Expand Up @@ -143,7 +143,7 @@ public sealed class IRCNickList {
}
return true;
} else {
foreach (string chan in bot.opchannels) {
foreach (string chan in bot.OpChannels) {
chanNicks = GetNicks(chan);
if (chanNicks.Count == 0) continue;

Expand Down
25 changes: 20 additions & 5 deletions MCGalaxy/Modules/RelayBot.cs
Expand Up @@ -24,18 +24,25 @@

namespace MCGalaxy.Modules.Relay {

public class RelayUser { public string Nick, UserID; }
public class RelayUser { public string Nick, UserID; }
/// <summary> Manages a connection to an external communication service </summary>
public abstract class RelayBot {

/// <summary> List of commands that cannot be used by relay bot controllers. </summary>
public List<string> BannedCommands;

/// <summary> List of channels to send public chat messages to </summary>
public string[] Channels;

/// <summary> List of channels to send staff only messages to </summary>
public string[] OpChannels;

readonly Player fakeGuest = new Player("RelayBot");
readonly Player fakeStaff = new Player("RelayBot");
DateTime lastWho, lastOpWho;


/// <summary> The name of service this relay bot communicates with </summary>
/// <remarks> IRC, Discord, etc </remarks>
public abstract string RelayName { get; }
Expand All @@ -47,10 +54,18 @@ public abstract class RelayBot {
public abstract void MessageUser(RelayUser user, string message);

/// <summary> Sends a message to all channels setup for general public chat </summary>
public abstract void SendPublicMessage(string message);
public void SendPublicMessage(string message) {
foreach (string chan in Channels) {
MessageChannel(chan, message);
}
}

/// <summary> Sends a message to all channels setup for staff chat only </summary>
public abstract void SendStaffMessage(string message);
/// <summary> Sends a message to all channels setup for staff chat only </summary>
public void SendStaffMessage(string message) {
foreach (string chan in OpChannels) {
MessageChannel(chan, message);
}
}


protected void SetDefaultBannedCommands() {
Expand Down

0 comments on commit 1ee8bcc

Please sign in to comment.