diff --git a/src/Tgstation.Server.Api/Models/DiscordConnectionStringBuilder.cs b/src/Tgstation.Server.Api/Models/DiscordConnectionStringBuilder.cs index 2fe677ea02..57fba95013 100644 --- a/src/Tgstation.Server.Api/Models/DiscordConnectionStringBuilder.cs +++ b/src/Tgstation.Server.Api/Models/DiscordConnectionStringBuilder.cs @@ -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); /// - /// The Discord bot token + /// The Discord bot token. /// /// See https://discordapp.com/developers/docs/topics/oauth2#bots public string? BotToken { get; set; } + /// + /// The . + /// + public DiscordDMOutputDisplayType DMOutputDisplay { get; set; } + /// /// Construct a /// @@ -28,10 +34,17 @@ namespace Tgstation.Server.Api.Models /// The connection string 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(splits[1], out var dMOutputDisplayType)) + dMOutputDisplayType = DiscordDMOutputDisplayType.Always; + DMOutputDisplay = dMOutputDisplayType; } /// - public override string ToString() => BotToken ?? "(null)"; + public override string ToString() => $"{BotToken};{(int)DMOutputDisplay}"; } -} \ No newline at end of file +} diff --git a/src/Tgstation.Server.Api/Models/DiscordDMOutputDisplayType.cs b/src/Tgstation.Server.Api/Models/DiscordDMOutputDisplayType.cs new file mode 100644 index 0000000000..3af08a5daa --- /dev/null +++ b/src/Tgstation.Server.Api/Models/DiscordDMOutputDisplayType.cs @@ -0,0 +1,23 @@ +namespace Tgstation.Server.Api.Models +{ + /// + /// When the DM output section of Discord deployment embeds should be shown. + /// + public enum DiscordDMOutputDisplayType + { + /// + /// Always show. + /// + Always, + + /// + /// Only show if DM failed. + /// + OnError, + + /// + /// Never show. + /// + Never + } +} diff --git a/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs b/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs index e4b6aef02f..4488b7319a 100644 --- a/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs +++ b/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs @@ -32,11 +32,6 @@ namespace Tgstation.Server.Host.Components.Chat.Providers } } - /// - /// Gets the Discord bot token. - /// - string BotToken => ChatBot.ConnectionString; - /// /// The for the . /// @@ -52,6 +47,16 @@ namespace Tgstation.Server.Host.Components.Chat.Providers /// readonly List mappedChannels; + /// + /// The Discord bot token. + /// + readonly string botToken; + + /// + /// The . + /// + readonly DiscordDMOutputDisplayType outputDisplayType; + /// /// Normalize a discord mention string /// @@ -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(); @@ -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", diff --git a/tests/Tgstation.Server.Tests/Instance/ChatTest.cs b/tests/Tgstation.Server.Tests/Instance/ChatTest.cs index 57b9470e23..f41c1016d5 100644 --- a/tests/Tgstation.Server.Tests/Instance/ChatTest.cs +++ b/tests/Tgstation.Server.Tests/Instance/ChatTest.cs @@ -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,