Skip to content

Commit

Permalink
Add config options, adjust commands, console log
Browse files Browse the repository at this point in the history
- Add new config options to Enable Slash Commands
- Add and adjust commands for adding / removing channels, admins, and managers
- New Console Mirror service to print out the console window to a Discord channel
  • Loading branch information
xNarnia committed Jan 27, 2025
1 parent d812437 commit a63831d
Show file tree
Hide file tree
Showing 44 changed files with 1,738 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
using Terraria;
using TerrariaChatRelay.Clients.DiscordClient.Helpers;
using TerrariaChatRelay.Clients.DiscordClient.Services;
using TerrariaChatRelay.Clients.DiscordClient.Messaging;

namespace TerrariaChatRelay.Clients.DiscordClient
{
public class DiscordChatClient : BaseClient
public class DiscordChatClient : BaseClient
{
public const string API_URL = "https://discordapp.com/api/v6";

Expand Down Expand Up @@ -188,7 +189,8 @@ private Task Socket_Ready()
Services.Add(new GameStatusService(Socket, serviceTimer, chatParser));
Services.Add(new SlashCommandService(Socket));
Services.Add(new NoChannelGreetingService(Socket, Channel_IDs));
Services.ForEach(x => {
Services.Add(new ConsoleMirrorService(this, Endpoint.Console_Channel_IDs));
Services.ForEach(x => {
try
{
x.Start();
Expand Down Expand Up @@ -465,7 +467,7 @@ public override void SendMessageToClient(string msg, string sourceChannelId)

public async void SendMessageToClient(string msg, Embed embed, string sourceChannelId)
{
var channel = Socket.GetChannel(ulong.Parse(sourceChannelId));
var channel = Socket?.GetChannel(ulong.Parse(sourceChannelId));
if (channel is SocketTextChannel)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public class DiscordConfig : SimpleConfig<DiscordConfig>
public string HelpFormat7 { get; set; } = "%groupsuffix% = Group suffix";
[JsonProperty(Order = 233)]
public bool EmbedPlayerMessages = false;
[JsonProperty(Order = 235)]
[JsonProperty(Order = 234)]
public bool EnableSlashCommands = true;
[JsonProperty(Order = 235)]
public string TerrariaInGameDiscordPrefix = "[c/7489d8:Discord] - ";
[JsonProperty(Order = 240)]
public string PlayerChatFormat = "> **%playername%:** %message%";
Expand Down Expand Up @@ -111,6 +113,7 @@ public class Endpoint
{
public string BotToken { get; set; } = "BOT_TOKEN";
public List<ulong> Channel_IDs { get; set; } = new List<ulong>();
public List<ulong> Console_Channel_IDs { get; set; } = new List<ulong>();
public List<ulong> DenySendingMessagesToGame { get; set; } = new List<ulong>();
public List<ulong> DenyReceivingMessagesFromGame { get; set; } = new List<ulong>();
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TerrariaChatRelay.Clients.DiscordClient.Messaging
{
public class DiscordMessage
{
public string Message { get; set; }
public bool Embed { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Timers;

namespace TerrariaChatRelay.Clients.DiscordClient.Messaging
{
public class DiscordMessageQueue : Dictionary<ulong, Queue<DiscordMessage>>
{
public bool PreparingToSend { get { return preparingToSend; } }

private bool preparingToSend { get; set; }
private Timer queueTimer { get; set; }

/// <summary>
/// Initializes a new queue manager for Discord messages.
/// </summary>
/// <param name="QueueTime">Amount of time to queue messages before firing the OnReadyToSend event.</param>
public DiscordMessageQueue(double QueueTime)
{
preparingToSend = false;

queueTimer = new Timer(QueueTime);
queueTimer.Elapsed += delegate (object sender, ElapsedEventArgs e)
{
PrepareSend(false);
OnReadyToSend?.Invoke(this);
Clear();
};
}

/// <summary>
/// Queues message to add to send when queue is ready to send.
/// </summary>
/// <param name="channelToSendTo">Channel ID of target channel.</param>
/// <param name="message">Message to send.</param>
public void QueueMessage(ulong channelToSendTo, DiscordMessage message)
=> QueueMessage(new List<ulong> { channelToSendTo }, message);

/// <summary>
/// Queues messages to add to send when queue is ready to send.
/// </summary>
/// <param name="channelToSendTo">Channel IDs of target channels.</param>
/// <param name="message">Message to send.</param>
public void QueueMessage(IEnumerable<ulong> channelsToSendTo, DiscordMessage message)
{
foreach (var channelId in channelsToSendTo)
{
if (!ContainsKey(channelId))
Add(channelId, new Queue<DiscordMessage>());

this[channelId].Enqueue(message);
}

PrepareSend(true);
}

/// <summary>
/// Starts or stops the queue timer.
/// </summary>
/// <param name="Prepare"></param>
private void PrepareSend(bool Prepare = true)
{
if (Prepare && !preparingToSend)
{
preparingToSend = true;
queueTimer.Start();
}
else if (!Prepare && preparingToSend)
{
preparingToSend = false;
queueTimer.Stop();
}
}

/// <summary>
/// <para>Fires when queue timer has elapsed. The timer begins when a message is queued. </para>
/// If a message is queued while the timer is running, it will be queued.
/// When the timer elapses, it will fire this event with a queue for each channel.
/// </summary>
public event Action<Dictionary<ulong, Queue<DiscordMessage>>> OnReadyToSend;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.IO;
using System.Text;

namespace TerrariaChatRelay.Clients.DiscordClient.Services.ConsoleMirror
{
/// <summary>
/// Wraps the original Console TextWriter, intercepting output.
/// </summary>
internal class ConsoleMirrorTextReader : TextReader, IDisposable
{
public event Action<String> ConsoleInputReceived;
private TextReader previousReader { get; set; }

public ConsoleMirrorTextReader(TextReader readerToWrap)
{
previousReader = readerToWrap;
}

public override string ReadLine()
{
string line = previousReader.ReadLine();

if (line != null)
ConsoleInputReceived?.Invoke("> " + line);

return line;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
Console.SetIn(previousReader);
base.Dispose(disposing);
}

base.Dispose(disposing);
}
}
}
Loading

0 comments on commit a63831d

Please sign in to comment.