From 4edb9b9ef3ac453cf46282de2a8a8afa6c3dfea1 Mon Sep 17 00:00:00 2001 From: GreemDev Date: Mon, 16 Aug 2021 19:41:22 -0500 Subject: [PATCH] Fix Starboard's incorrectly showing messages. --- global.json | 7 ++++ src/Commands/Modules/StarboardModule.cs | 50 +++++++++++++++++-------- src/Commands/VolteContext.cs | 12 +++--- src/Core/Helpers/AsyncDuplicateLock.cs | 1 - src/Core/Helpers/DiscordHelper.cs | 7 +++- src/Services/PingChecksService.cs | 3 +- src/Services/QuoteService.cs | 27 ++++++++----- src/Services/StarboardService.cs | 13 ++++--- 8 files changed, 81 insertions(+), 39 deletions(-) create mode 100644 global.json diff --git a/global.json b/global.json new file mode 100644 index 00000000..4db6a486 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "5.0", + "rollForward": "latestMajor", + "allowPrerelease": false + } +} \ No newline at end of file diff --git a/src/Commands/Modules/StarboardModule.cs b/src/Commands/Modules/StarboardModule.cs index 48dacf76..cefa9c26 100644 --- a/src/Commands/Modules/StarboardModule.cs +++ b/src/Commands/Modules/StarboardModule.cs @@ -1,6 +1,8 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Discord; using Discord.WebSocket; +using Gommon; using Qmmands; using Volte.Core.Entities; using Volte.Services; @@ -18,39 +20,57 @@ namespace Volte.Commands.Modules [Command("Channel", "Ch")] [Description("Sets the channel to be used by starboard when a message is starred.")] - public Task ChannelAsync(SocketTextChannel channel) + public Task ChannelAsync( + [Description("The channel to be used by Starboard.")] SocketTextChannel channel) { - Context.Modify(data => - { - data.Configuration.Starboard.StarboardChannel = channel.Id; - }); + Context.Modify(data => data.Configuration.Starboard.StarboardChannel = channel.Id); return Ok($"Successfully set the starboard channel to {MentionUtils.MentionChannel(channel.Id)}."); } [Command("Amount", "Count")] [Description("Sets the amount of stars required on a message for it to be posted to the Starboard.")] - public Task AmountAsync(int amount) + public Task AmountAsync( + [Description("The desired star count threshold before posting it in the starboard channel.")] int amount) { if (amount < 1) - { return BadRequest("Amount must be larger than zero."); - } + + Context.Modify(data => data.Configuration.Starboard.StarsRequiredToPost = amount); + + return Ok($"Set the amount of stars required to be posted as a starboard message to **{amount}**."); + } + + [Command("Setup")] + [Description("A one-off command that creates a channel for Starboard, with read-only permissions for everyone, and enables the starboard.")] + public async Task SetupAsync( + [Description("The name for the Starboard channel that will be created."), Remainder] string channelName = "starboard") + { + var channel = await Context.Guild.CreateTextChannelAsync(channelName.Replace(" ", "-"), props => + { + props.CategoryId = Context.Channel.CategoryId; + props.PermissionOverwrites = new List + { + new Overwrite(Context.Guild.EveryoneRole.Id, PermissionTarget.Role, + new OverwritePermissions(viewChannel: PermValue.Allow, sendMessages: PermValue.Deny)) + }; + }); + Context.Modify(data => { - data.Configuration.Starboard.StarsRequiredToPost = amount; + data.Configuration.Starboard.Enabled = true; + data.Configuration.Starboard.StarboardChannel = channel.Id; }); - return Ok($"Set the amount of stars required to be posted as a starboard message to **{amount}**."); + + return Ok($"Successfully configured the Starboard functionality, and any starred messages will go to {channel.Mention}."); } [Command("Enable")] [Description("Enable or disable the Starboard in this guild.")] - public Task EnableAsync(bool enabled) + public Task EnableAsync( + [Description("Whether or not to enable or disable the Starboard.")] bool enabled) { - Context.Modify(data => - { - data.Configuration.Starboard.Enabled = enabled; - }); + Context.Modify(data => data.Configuration.Starboard.Enabled = enabled); return Ok( enabled ? "Enabled the Starboard in this Guild." : "Disabled the Starboard in this Guild."); } diff --git a/src/Commands/VolteContext.cs b/src/Commands/VolteContext.cs index bcf0b2f6..9e34180b 100644 --- a/src/Commands/VolteContext.cs +++ b/src/Commands/VolteContext.cs @@ -10,7 +10,6 @@ using Humanizer; using Volte.Core; using Volte.Core.Entities; using Volte.Core.Helpers; -using Volte.Interactive; using Volte.Services; namespace Volte.Commands @@ -23,15 +22,13 @@ namespace Volte.Commands // ReSharper disable once SuggestBaseTypeForParameter private VolteContext(SocketMessage msg, IServiceProvider provider) : base(provider) { - if (provider.TryGet(out var client)) - Client = client; + Client = provider.Get(); Guild = msg.Channel.Cast()?.Guild; Interactive = provider.Get(); Channel = msg.Channel.Cast(); User = msg.Author.Cast(); Message = msg.Cast(); - if (provider.TryGet(out var db)) - GuildData = db.GetData(Guild); + GuildData = provider.Get().GetData(Guild); Now = DateTime.Now; } @@ -44,7 +41,12 @@ namespace Volte.Commands public SocketUserMessage Message { get; } public GuildData GuildData { get; } public DateTime Now { get; } + + public Embed CreateEmbed(StringBuilder content) => CreateEmbed(content.ToString()); + public Embed CreateEmbed(Action action) + => CreateEmbedBuilder().Apply(action).Build(); + public Embed CreateEmbed(string content) => CreateEmbedBuilder(content).Build(); public EmbedBuilder CreateEmbedBuilder(string content = null) => new EmbedBuilder() diff --git a/src/Core/Helpers/AsyncDuplicateLock.cs b/src/Core/Helpers/AsyncDuplicateLock.cs index ff982ab1..6d06e48c 100644 --- a/src/Core/Helpers/AsyncDuplicateLock.cs +++ b/src/Core/Helpers/AsyncDuplicateLock.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; -using Gommon; namespace Volte.Core.Helpers { diff --git a/src/Core/Helpers/DiscordHelper.cs b/src/Core/Helpers/DiscordHelper.cs index cf55e608..aa3ac254 100644 --- a/src/Core/Helpers/DiscordHelper.cs +++ b/src/Core/Helpers/DiscordHelper.cs @@ -150,7 +150,12 @@ namespace Volte.Core.Helpers var mod = provider.Get(); var starboard = provider.Get(); - client.Log += m => Task.Run(() => Logger.HandleLogEvent(new LogEventArgs(m))); + client.Log += async m => + { + if (!(m.Message.ContainsIgnoreCase("unknown dispatch") && + m.Message.ContainsIgnoreCase("application_command"))) + await Task.Run(() => Logger.HandleLogEvent(new LogEventArgs(m))); + }; if (provider.TryGet(out var guild)) { diff --git a/src/Services/PingChecksService.cs b/src/Services/PingChecksService.cs index 6acf7004..4b1cf0cb 100644 --- a/src/Services/PingChecksService.cs +++ b/src/Services/PingChecksService.cs @@ -13,8 +13,7 @@ namespace Volte.Services { Logger.Debug(LogSource.Service, "Received a message to check for ping threshold violations."); - if (args.Message.MentionedEveryone || - args.Message.MentionedUsers.Count > 10) + if (args.Message.MentionedEveryone || args.Message.MentionedUsers.Count > 10) { await args.Message.DeleteAsync(); Logger.Debug(LogSource.Service, diff --git a/src/Services/QuoteService.cs b/src/Services/QuoteService.cs index 335fd727..d165715e 100644 --- a/src/Services/QuoteService.cs +++ b/src/Services/QuoteService.cs @@ -3,6 +3,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using Discord.WebSocket; using Discord; +using Discord.Rest; using Gommon; using Volte.Commands; using Volte.Core.Entities; @@ -32,17 +33,9 @@ namespace Volte.Services var match = JumpUrlPattern.Match(args.Message.Content); if (!match.Success) return false; - if (!ulong.TryParse(match.Groups["GuildId"].Value, out var guildId) || - !ulong.TryParse(match.Groups["ChannelId"].Value, out var channelId) || - !ulong.TryParse(match.Groups["MessageId"].Value, out var messageId)) return false; - - var g = await _client.Rest.GetGuildAsync(guildId); - if (g is null) return false; - var c = await g.GetTextChannelAsync(channelId); - if (c is null) return false; - - var m = await c.GetMessageAsync(messageId); + var m = await GetMatchMessageAsync(match); if (m is null) return false; + if (m.Content.IsNullOrWhitespace() && !m.Embeds.IsEmpty()) return false; await GenerateQuoteEmbed(m, args.Context).SendToAsync(args.Context.Channel) @@ -54,6 +47,20 @@ namespace Volte.Services return true; } + private async Task GetMatchMessageAsync(Match match) + { + if (!ulong.TryParse(match.Groups["GuildId"].Value, out var guildId) || + !ulong.TryParse(match.Groups["ChannelId"].Value, out var channelId) || + !ulong.TryParse(match.Groups["MessageId"].Value, out var messageId)) return null; + + var g = await _client.Rest.GetGuildAsync(guildId); + if (g is null) return null; + var c = await g.GetTextChannelAsync(channelId); + if (c is null) return null; + + return await c.GetMessageAsync(messageId); + } + private Embed GenerateQuoteEmbed(IMessage message, VolteContext ctx) { var e = ctx.CreateEmbedBuilder() diff --git a/src/Services/StarboardService.cs b/src/Services/StarboardService.cs index 9d1b2b5c..dbf50d02 100644 --- a/src/Services/StarboardService.cs +++ b/src/Services/StarboardService.cs @@ -270,13 +270,16 @@ namespace Volte.Services .WithAuthor(message.Author) .AddField("Posted", Format.Bold(Format.Url($"#{message.Channel.Name}", message.GetJumpUrl()))); - if (Uri.IsWellFormedUriString(message.Content, UriKind.RelativeOrAbsolute)) - e.WithImageUrl(message.Content); - else if (!message.Attachments.IsEmpty()) + if (!message.Attachments.IsEmpty() && !message.Content.IsNullOrEmpty()) + e.WithDescription(message.Content).WithImageUrl(message.Attachments.First().Url); + if (message.Attachments.IsEmpty() && !message.Content.IsNullOrEmpty()) + e.WithDescription(message.Content); + if (!message.Attachments.IsEmpty() && message.Content.IsNullOrEmpty()) e.WithImageUrl(message.Attachments.First().Url); - if (message.Attachments.IsEmpty()) - e.WithDescription(message.Content); + if (message.Attachments.Count > 1) + e.WithFooter($"This message has {message.Attachments.Count - 1} more attachments. See original message."); + return e; }