Skip to content

Commit

Permalink
Discord/IRC: Allow ignoring users
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 19, 2021
1 parent b902cd5 commit 46a54c2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
6 changes: 6 additions & 0 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Expand Up @@ -97,6 +97,12 @@ public sealed class DiscordBot : RelayBot {
}
}

protected override void UpdateConfig() {
Channels = Config.Channels.SplitComma();
OpChannels = Config.OpChannels.SplitComma();
IgnoredUsers = Config.IgnoredUsers.SplitComma();
}


string GetNick(JsonObject data) {
if (!Config.UseNicks) return null;
Expand Down
2 changes: 2 additions & 0 deletions MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs
Expand Up @@ -35,6 +35,8 @@ public sealed class DiscordConfig {
public string Channels = "";
[ConfigString("op-channel-ids", null, "", true)]
public string OpChannels = "";
[ConfigString("ignored-user-ids", null, "", true)]
public string IgnoredUsers = "";

const string file = "properties/discordbot.properties";
static ConfigElement[] cfg;
Expand Down
13 changes: 5 additions & 8 deletions MCGalaxy/Modules/Relay/IRC/IRCBot.cs
Expand Up @@ -42,7 +42,6 @@ public sealed class IRCBot : RelayBot {
public IRCBot() {
nicks = new IRCNickList();
nicks.bot = this;
UpdateState();
}


Expand All @@ -64,7 +63,6 @@ public sealed class IRCBot : RelayBot {
protected override void DoConnect() {
if (connection == null) connection = new Connection(new UTF8Encoding(false));
Hook();
UpdateState();

connection.Hostname = Server.Config.IRCServer;
connection.Port = Server.Config.IRCPort;
Expand All @@ -84,12 +82,12 @@ public sealed class IRCBot : RelayBot {
Unhook();
nicks.Clear();
connection.Disconnect(reason);
}

}

void UpdateState() {
Channels = Server.Config.IRCChannels.SplitComma();
OpChannels = Server.Config.IRCOpChannels.SplitComma();
protected override void UpdateConfig() {
Channels = Server.Config.IRCChannels.SplitComma();
OpChannels = Server.Config.IRCOpChannels.SplitComma();
IgnoredUsers = Server.Config.IRCIgnored.SplitComma();
}


Expand Down Expand Up @@ -301,7 +299,6 @@ public sealed class IRCBot : RelayBot {
void OnDisconnected() { AutoReconnect(); }

void OnNick(UserInfo user, string newNick) {
//Chat.MessageGlobal(Server.IRCColor + "(IRC) " + user.Nick + " changed nick to " + newNick);
// We have successfully reclaimed our nick, so try to sign in again.
if (newNick == nick) Authenticate();
if (newNick.Trim().Length == 0) return;
Expand Down
11 changes: 10 additions & 1 deletion MCGalaxy/Modules/Relay/RelayBot.cs
Expand Up @@ -39,6 +39,9 @@ public abstract class RelayBot {
/// <summary> List of channels to send staff only messages to </summary>
public string[] OpChannels;

/// <summary> List of user IDs that all chat from is ignored </summary>
public string[] IgnoredUsers;

readonly Player fakeGuest = new Player("RelayBot");
readonly Player fakeStaff = new Player("RelayBot");
DateTime lastWho, lastOpWho;
Expand Down Expand Up @@ -96,6 +99,7 @@ public abstract class RelayBot {
Logger.Log(LogType.RelayActivity, "Connecting to {0}...", RelayName);

try {
UpdateConfig();
LoadBannedCommands();
DoConnect();
} catch (Exception e) {
Expand Down Expand Up @@ -128,7 +132,8 @@ public abstract class RelayBot {

protected abstract void DoConnect();
protected abstract void DoDisconnect(string reason);

protected abstract void UpdateConfig();

void LoadBannedCommands() {
BannedCommands = new List<string>() { "IRCBot", "DiscordBot", "OpRules", "IRCControllers" };

Expand Down Expand Up @@ -227,6 +232,8 @@ public abstract class RelayBot {

/// <summary> Handles a direct message written by the given user </summary>
protected void HandleDirectMessage(RelayUser user, string channel, string message) {
if (IgnoredUsers.CaselessContains(user.ID)) return;

message = ParseMessage(message);
string[] parts = message.SplitSpaces(2);
string cmdName = parts[0].ToLower();
Expand All @@ -246,6 +253,8 @@ public abstract class RelayBot {

/// <summary> Handles a message written by the given user on the given channel </summary>
protected void HandleChannelMessage(RelayUser user, string channel, string message) {
if (IgnoredUsers.CaselessContains(user.ID)) return;

message = ParseMessage(message);
message = message.TrimEnd();
if (message.Length == 0) return;
Expand Down
4 changes: 3 additions & 1 deletion MCGalaxy/Server/ServerConfig.cs
Expand Up @@ -164,7 +164,9 @@ public sealed class ServerConfig : EnvConfig {
public string IRCPassword = "";
[ConfigBool("irc-ssl", "IRC bot", false)]
public bool IRCSSL = false;

[ConfigString("irc-ignored-nicks", "IRC bot", "", true)]
public string IRCIgnored = "";

[ConfigBool("UseMySQL", "Database", false)]
public bool UseMySQL = false;
[ConfigString("host", "Database", "127.0.0.1")]
Expand Down
19 changes: 0 additions & 19 deletions MCGalaxy/sharkbite.thresher/Connection.cs
Expand Up @@ -37,17 +37,6 @@ namespace Sharkbite.Irc
/// </summary>
public sealed class Connection
{
/// <summary>
/// Receive all the messages, unparsed, sent by the IRC server. This is not
/// normally needed but provided for those who are interested.
/// </summary>
public event RawMessageReceivedEventHandler OnRawMessageReceived;
/// <summary>
/// Receive all the raw messages sent to the IRC from this connection
/// </summary>
public event RawMessageSentEventHandler OnRawMessageSent;


TcpClient client;
Listener listener;
Sender sender;
Expand Down Expand Up @@ -229,10 +218,6 @@ internal void ReceiveIRCMessages()
if( IsCtcpMessage( line) ) continue;

listener.Parse( line );
if( OnRawMessageReceived != null )
{
OnRawMessageReceived( line );
}
}
catch( ThreadAbortException )
{
Expand Down Expand Up @@ -275,10 +260,6 @@ internal void SendCommand( StringBuilder command)
catch( Exception )
{
}
if( OnRawMessageSent != null )
{
OnRawMessageSent( command.ToString() );
}
command.Remove(0, command.Length );
}
/// <summary>
Expand Down

0 comments on commit 46a54c2

Please sign in to comment.