Skip to content

Commit

Permalink
Relay bots are now completely responsible for converting messages to …
Browse files Browse the repository at this point in the history
…be suitable to send to an external communication service
  • Loading branch information
UnknownShadow200 committed Jan 24, 2023
1 parent e05f035 commit 28dede6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
7 changes: 5 additions & 2 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Expand Up @@ -392,13 +392,16 @@ public sealed class DiscordBot : RelayBot
}

protected override void DoSendMessage(string channel, string message) {
message = ConvertMessage(message);
ChannelSendMessage msg = new ChannelSendMessage(channel, message);
msg.Allowed = allowed;
Send(msg);
}

protected override string ConvertMessage(string message) {
message = base.ConvertMessage(message);
/// <summary> Formats a message for displaying on Discord </summary>
/// <example> Escapes markdown characters such as _ and * </example>
string ConvertMessage(string message) {
message = ConvertMessageCommon(message);
message = Colors.StripUsed(message);
message = EscapeMarkdown(message);
message = SpecialToMarkdown(message);
Expand Down
7 changes: 5 additions & 2 deletions MCGalaxy/Modules/Relay/IRC/IRCBot.cs
Expand Up @@ -50,6 +50,7 @@ public sealed class IRCBot : RelayBot
static char[] newline = { '\n' };
protected override void DoSendMessage(string channel, string message) {
if (!ready) return;
message = ConvertMessage(message);

// IRC messages can't have \r or \n in them
// https://stackoverflow.com/questions/13898584/insert-line-breaks-into-an-irc-message
Expand Down Expand Up @@ -160,11 +161,13 @@ public sealed class IRCBot : RelayBot
return sb.ToString();
}

protected override string ConvertMessage(string message) {
/// <summary> Formats a message for displaying on IRC </summary>
/// <example> Converts colors such as &amp;0 into IRC color codes </example>
string ConvertMessage(string message) {
if (String.IsNullOrEmpty(message.Trim())) message = ".";
const string resetSignal = "\x03\x0F";

message = base.ConvertMessage(message);
message = ConvertMessageCommon(message);
message = message.Replace("%S", "&f"); // TODO remove
message = message.Replace("&S", "&f");
message = message.Replace("&f", resetSignal);
Expand Down
17 changes: 8 additions & 9 deletions MCGalaxy/Modules/Relay/RelayBot.cs
Expand Up @@ -108,14 +108,16 @@ public abstract class RelayBot

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

/// <summary> Sends a message to all channels setup for staff chat only </summary>
public void SendStaffMessage(string message) {
foreach (string chan in OpChannels) {
foreach (string chan in OpChannels)
{
SendMessage(chan, message);
}
}
Expand All @@ -124,19 +126,16 @@ public abstract class RelayBot
/// <remarks> Channels can specify either group chat or direct messages </remarks>
public void SendMessage(string channel, string message) {
if (!Enabled || !Connected) return;
DoSendMessage(channel, ConvertMessage(message));
DoSendMessage(channel, message);
}

/// <summary> Formats a message for displaying on the external communication service </summary>
/// <example> IRC converts colors such as &amp;0 into IRC color codes </example>
protected virtual string ConvertMessage(string message) {
protected abstract void DoSendMessage(string channel, string message);

protected string ConvertMessageCommon(string message) {
message = EmotesHandler.Replace(message);
message = ChatTokens.ApplyCustom(message);
return message;
}
/// <summary> Sends a chat message to the given channel </summary>
/// <remarks> Assumes the message has already been formatted using ConvertMessage </remarks>
protected abstract void DoSendMessage(string channel, string message);


/// <summary> Attempts to connect to the external communication service </summary>
Expand Down

0 comments on commit 28dede6

Please sign in to comment.