Skip to content

Commit

Permalink
Merge pull request #71 from MonzUn/staging_2.0
Browse files Browse the repository at this point in the history
2.0 Merge
  • Loading branch information
MonzUn authored Aug 26, 2020
2 parents 99c9785 + f4cc30b commit 033096c
Show file tree
Hide file tree
Showing 14 changed files with 473 additions and 257 deletions.
5 changes: 3 additions & 2 deletions ConfigurationGUI.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ See the [installation guide](Installation.md)

## Eco Status

1. Located the box called "Eco Status Discord Channels". Click on the three dots next to the box saying "(Collection)". This may be hidden until you mouse over it.
1. Locate the box called "Eco Status Discord Channels". Click on the three dots next to the box saying "(Collection)". This may be hidden until you mouse over it.

![Opening Collection Window](images/configuration/ecostatus/1.png)

Expand All @@ -42,7 +42,8 @@ See the [installation guide](Installation.md)

3. Enter the parameters for the Eco status Discord Channel. _"DiscordChannel"_ should be the Discord channel you want the status message to be posted in. For example, "eco-status". _"DiscordGuild"_ is the name of the Discord Server, for example "Eco" for the official Eco Discord. Both _DiscordChannel_ and _DiscordGuild_ accept names or IDs. Once entered, hit "OK".
**Optional**: Configure the usage flags according to your preferences.
**Note**: The Eco status message is continiously being rewritten by DiscordLink. It should be the top message in its channel and the message should not be removed.

**Note**: Only one message will be posted and that message will then be kept up to date by DiscordLink through frequent automatic edits.

![Opening Collection Window](images/configuration/ecostatus/3.png)

Expand Down
3 changes: 2 additions & 1 deletion ConfigurationNoGUI.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Once you have a bot token, copy the _"BotToken"_ field in the sample config and
2. Set _"DiscordGuild"_ field to the name or ID of your Discord Server.
3. Set the _"DiscordChannel"_ field to the name or ID of the Discord channel you want the status message to be posted in.
4. **Optional**: Configure the usage flags according to your preferences.
**Note**: The Eco status message is continiously being rewritten by DiscordLink. It should be the top message in its channel and the message should not be removed.

**Note**: Only one message will be posted and that message will then be kept up to date by DiscordLink through frequent automatic edits.

## Chat Log
To enable/disable logging of the chat, set the _"Log Chat"_ option to true/false.
Expand Down
1 change: 0 additions & 1 deletion DLLCopyList.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DiscordLink.dll
DSharpPlus.dll
DSharpPlus.CommandsNext.dll
Newtonsoft.Json.dll
99 changes: 99 additions & 0 deletions DiscordLink/ChatLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using DSharpPlus.Entities;
using Eco.Gameplay.GameActions;
using System;
using System.Globalization;
using System.IO;
using System.Threading;

namespace Eco.Plugins.DiscordLink
{
class ChatLogger
{
public bool Initialized { get { return _initialized; } }

private const int CHATLOG_FLUSH_TIMER_INTERAVAL_MS = 60000; // 1 minute interval
private bool _initialized = false;
private StreamWriter _writer;
private Timer _flushTimer = null;

public void Start()
{
if (_initialized) return;

DiscordConfig config = DiscordLink.Obj.DiscordPluginConfig;
try
{
_writer = new StreamWriter(config.ChatlogPath, append: true);
_initialized = true;
}
catch (Exception e)
{
Logger.Error("Error occurred while attempting to initialize the chat logger using path \"" + config.ChatlogPath + "\". Error message: " + e);
}

if (_initialized)
{
_flushTimer = new Timer(async innerArgs =>
{
Flush();
}, null, 0, CHATLOG_FLUSH_TIMER_INTERAVAL_MS);
}
}

public void Stop()
{
if (!_initialized) return;

SystemUtil.StopAndDestroyTimer(ref _flushTimer);
try
{
_writer.Close();
}
catch (Exception e)
{
Logger.Error("Error occurred while attempting to close the chatlog file writer. Error message: " + e);
}

_writer = null;
_initialized = false;
}

public void Restart()
{
Stop();
Start();
}

public void Write(DiscordMessage message)
{
if (!_initialized) return;

DateTime time = DateTime.Now;
int utcOffset = TimeZoneInfo.Local.GetUtcOffset(time).Hours;
_writer.WriteLine("[Discord] [" + DateTime.Now.ToString("yyyy-MM-dd : HH:mm", CultureInfo.InvariantCulture) + " UTC " + (utcOffset != 0 ? (utcOffset >= 0 ? "+" : "-") + utcOffset : "") + "] "
+ $"{DiscordLink.StripTags(message.Author.Username) + ": " + DiscordLink.StripTags(message.Content)}");
}

public void Write(ChatSent message)
{
if (!_initialized) return;

DateTime time = DateTime.Now;
int utcOffset = TimeZoneInfo.Local.GetUtcOffset(time).Hours;
_writer.WriteLine("[Eco] [" + DateTime.Now.ToString("yyyy-MM-dd : HH:mm", CultureInfo.InvariantCulture) + " UTC " + (utcOffset != 0 ? (utcOffset >= 0 ? "+" : "-") + utcOffset : "") + "] "
+ $"{DiscordLink.StripTags(message.Citizen.Name) + ": " + DiscordLink.StripTags(message.Message)}");
}

private void Flush()
{
try
{
_writer.Flush();
}
catch (Exception e)
{
Logger.Error("Error occurred while attempting to write the chatlog to file. Error message: " + e);
}
}
}
}
4 changes: 2 additions & 2 deletions DiscordLink/DiscordDiscordCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private async Task RespondToCommand(CommandContext ctx, string textContent, Disc
else
{
// Either make sure we have permission to use embeds or convert the embed to text
if (DiscordUtil.HasEmbedPermission(ctx.Channel))
if (DiscordUtil.ChannelHasPermission(ctx.Channel, Permissions.EmbedLinks))
{
await ctx.RespondAsync(textContent, false, embedContent);
}
Expand All @@ -69,7 +69,7 @@ public async Task Ping(CommandContext ctx)
[Description("Retrieves the current status of the Eco Server")]
public async Task EcoStatus(CommandContext ctx)
{
await RespondToCommand(ctx, "", MessageBuilder.GetEcoStatus(MessageBuilder.EcoStatusComponentFlag.All));
await RespondToCommand(ctx, "", MessageBuilder.GetEcoStatus(MessageBuilder.EcoStatusComponentFlag.All, isLiveMessage: false));
}

#if DEBUG
Expand Down
Loading

0 comments on commit 033096c

Please sign in to comment.