Skip to content

Commit

Permalink
Merge pull request #54 from winglessraven/dev
Browse files Browse the repository at this point in the history
Batch console output together into codeblocks
  • Loading branch information
winglessraven authored Jun 17, 2023
2 parents cc3c0b9 + dc94165 commit e531416
Showing 1 changed file with 84 additions and 13 deletions.
97 changes: 84 additions & 13 deletions DiscordBotPlugin/PluginMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class PluginMain : AMPPlugin
private readonly IConfigSerializer _config;
private DiscordSocketClient _client;
private List<PlayerPlayTime> playerPlayTimes = new List<PlayerPlayTime>();
private List<String> consoleOutput = new List<String>();

public PluginMain(ILogger log, IConfigSerializer config, IPlatformInfo platform,
IRunningTasksManager taskManager, IApplicationWrapper application, IAMPInstanceInfo AMPInstanceInfo)
Expand Down Expand Up @@ -71,7 +72,7 @@ private void Log_MessageLogged(object sender, LogEventArgs e)
{
// Clean the message to avoid code blocks and send it to Discord
string clean = e.Message.Replace("`", "'");
_ = ConsoleOutputSend(clean);
consoleOutput.Add(clean);
}
}

Expand Down Expand Up @@ -198,6 +199,9 @@ public async Task ConnectDiscordAsync(string BotToken)
await _client.LoginAsync(TokenType.Bot, BotToken);
await _client.StartAsync();

//console output
await ConsoleOutputSend();

// Set the bot's status
await SetStatus();

Expand Down Expand Up @@ -750,7 +754,7 @@ private async Task ChatMessageSend(string Message)
{
// Find the text channel with the specified name
var guildID = guild.Id;
var eventChannel = GetEventChannel(guildID, _settings.MainSettings.ConsoleToDiscordChannel);
var eventChannel = GetEventChannel(guildID, _settings.MainSettings.ChatToDiscordChannel);

if (eventChannel != null)
{
Expand All @@ -765,26 +769,93 @@ private async Task ChatMessageSend(string Message)
/// </summary>
/// <param name="message">The message to send.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task ConsoleOutputSend(string Message)
private async Task ConsoleOutputSend()
{
// Get all guilds the bot is a member of
var guilds = _client.Guilds;
while (_settings.MainSettings.SendConsoleToDiscord && _settings.MainSettings.BotActive)
{
if (consoleOutput.Count > 0)
{
try
{
// Create a duplicate list of console output messages
List<string> messages = new List<string>(consoleOutput);
consoleOutput.Clear();

// Iterate over each guild
foreach (SocketGuild guild in guilds)
// Split the output into multiple strings, each presented within a code block
List<string> outputStrings = SplitOutputIntoCodeBlocks(messages);

// Get all guilds the bot is a member of
var guilds = _client.Guilds;

// Iterate over each output string
foreach (string output in outputStrings)
{
// Iterate over each guild
foreach (SocketGuild guild in guilds)
{
// Find the text channel with the specified name
var guildID = guild.Id;
var eventChannel = GetEventChannel(guildID, _settings.MainSettings.ConsoleToDiscordChannel);

if (eventChannel != null)
{
// Send the message to the channel
await _client.GetGuild(guildID).GetTextChannel(eventChannel.Id).SendMessageAsync(output);
}
}
}

// Clear the duplicate list
messages.Clear();
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}

// Delay the execution for 10 seconds
await Task.Delay(10000);
}
}

private List<string> SplitOutputIntoCodeBlocks(List<string> messages)
{
const int MaxCodeBlockLength = 2000; // Maximum length of a code block in Discord

List<string> outputStrings = new List<string>(); // List to store the split output strings

string currentString = ""; // Current string being built
foreach (string message in messages)
{
// Find the text channel with the specified name
var guildID = guild.Id;
var eventChannel = GetEventChannel(guildID, _settings.MainSettings.ConsoleToDiscordChannel);
// Check if adding the next message will exceed the maximum code block length
if (currentString.Length + Environment.NewLine.Length + message.Length + 6 > MaxCodeBlockLength)
{
// Add the current string to the list of output strings
outputStrings.Add($"```{currentString}```");

if (eventChannel != null)
// Reset the current string to start building a new one
currentString = "";
}

// Add the message to the current string, separated by a newline character
if (!string.IsNullOrEmpty(currentString))
{
// Send the message to the channel
await _client.GetGuild(guildID).GetTextChannel(eventChannel.Id).SendMessageAsync("`" + Message + "`");
currentString += Environment.NewLine;
}
currentString += message;
}

// Add the last current string to the list of output strings
if (!string.IsNullOrEmpty(currentString))
{
outputStrings.Add($"```{currentString}```");
}

return outputStrings;
}


/// <summary>
/// Handles button response and logs the command if enabled in settings.
/// </summary>
Expand Down

0 comments on commit e531416

Please sign in to comment.