Option to not display DM output in Discord embed

This commit is contained in:
Jordan Brown
2020-08-17 13:18:58 -04:00
parent 78356eb68a
commit 7ffcdf2ed3
4 changed files with 73 additions and 14 deletions
@@ -1,4 +1,5 @@
using System;
using System;
using System.Linq;
using Tgstation.Server.Api.Models.Internal;
namespace Tgstation.Server.Api.Models
@@ -12,11 +13,16 @@ namespace Tgstation.Server.Api.Models
public override bool Valid => !String.IsNullOrEmpty(BotToken);
/// <summary>
/// The Discord bot token
/// The Discord bot token.
/// </summary>
/// <remarks>See https://discordapp.com/developers/docs/topics/oauth2#bots</remarks>
public string? BotToken { get; set; }
/// <summary>
/// The <see cref="DiscordDMOutputDisplayType"/>.
/// </summary>
public DiscordDMOutputDisplayType DMOutputDisplay { get; set; }
/// <summary>
/// Construct a <see cref="DiscordConnectionStringBuilder"/>
/// </summary>
@@ -28,10 +34,17 @@ namespace Tgstation.Server.Api.Models
/// <param name="connectionString">The connection string</param>
public DiscordConnectionStringBuilder(string connectionString)
{
BotToken = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
if(connectionString == null)
throw new ArgumentNullException(nameof(connectionString));
var splits = connectionString.Split(';');
BotToken = splits.First();
if (splits.Length < 2 || !Enum.TryParse<DiscordDMOutputDisplayType>(splits[1], out var dMOutputDisplayType))
dMOutputDisplayType = DiscordDMOutputDisplayType.Always;
DMOutputDisplay = dMOutputDisplayType;
}
/// <inheritdoc />
public override string ToString() => BotToken ?? "(null)";
public override string ToString() => $"{BotToken};{(int)DMOutputDisplay}";
}
}
}
@@ -0,0 +1,23 @@
namespace Tgstation.Server.Api.Models
{
/// <summary>
/// When the DM output section of Discord deployment embeds should be shown.
/// </summary>
public enum DiscordDMOutputDisplayType
{
/// <summary>
/// Always show.
/// </summary>
Always,
/// <summary>
/// Only show if DM failed.
/// </summary>
OnError,
/// <summary>
/// Never show.
/// </summary>
Never
}
}
@@ -32,11 +32,6 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
}
}
/// <summary>
/// Gets the Discord bot token.
/// </summary>
string BotToken => ChatBot.ConnectionString;
/// <summary>
/// The <see cref="IAssemblyInformationProvider"/> for the <see cref="DiscordProvider"/>.
/// </summary>
@@ -52,6 +47,16 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// </summary>
readonly List<ulong> mappedChannels;
/// <summary>
/// The Discord bot token.
/// </summary>
readonly string botToken;
/// <summary>
/// The <see cref="DiscordDMOutputDisplayType"/>.
/// </summary>
readonly DiscordDMOutputDisplayType outputDisplayType;
/// <summary>
/// Normalize a discord mention string
/// </summary>
@@ -74,6 +79,11 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
: base(jobManager, logger, chatBot)
{
this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
var csb = new DiscordConnectionStringBuilder(chatBot.ConnectionString);
botToken = csb.BotToken;
outputDisplayType = csb.DMOutputDisplay;
client = new DiscordSocketClient();
client.MessageReceived += Client_MessageReceived;
mappedChannels = new List<ulong>();
@@ -150,7 +160,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
{
try
{
await client.LoginAsync(TokenType.Bot, BotToken, true).ConfigureAwait(false);
await client.LoginAsync(TokenType.Bot, botToken, true).ConfigureAwait(false);
Logger.LogTrace("Logged in.");
cancellationToken.ThrowIfCancellationRequested();
@@ -379,7 +389,15 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
? "The deployment completed successfully and will be available at the next server reboot."
: "The deployment failed.";
if (dreamMakerOutput != null)
var showDMOutput = outputDisplayType switch
{
DiscordDMOutputDisplayType.Always => true,
DiscordDMOutputDisplayType.Never => false,
DiscordDMOutputDisplayType.OnError => errorMessage != null,
_ => throw new InvalidOperationException($"Invalid DiscordDMOutputDisplayType: {outputDisplayType}"),
};
if (showDMOutput && dreamMakerOutput != null)
builder.AddField(new EmbedFieldBuilder
{
Name = "DreamMaker Output",
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -93,7 +93,12 @@ namespace Tgstation.Server.Tests.Instance
{
var firstBot = new ChatBot
{
ConnectionString = Environment.GetEnvironmentVariable("TGS4_TEST_DISCORD_TOKEN"),
ConnectionString =
new DiscordConnectionStringBuilder
{
BotToken = Environment.GetEnvironmentVariable("TGS4_TEST_DISCORD_TOKEN"),
DMOutputDisplay = DiscordDMOutputDisplayType.OnError
}.ToString(),
Enabled = false,
Name = "r4407",
Provider = ChatProvider.Discord,