Skip to content

Commit 4edb9b9

Browse files
committed
Fix Starboard's incorrectly showing messages.
1 parent 7dae016 commit 4edb9b9

File tree

8 files changed

+81
-39
lines changed

8 files changed

+81
-39
lines changed

global.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "5.0",
4+
"rollForward": "latestMajor",
5+
"allowPrerelease": false
6+
}
7+
}

src/Commands/Modules/StarboardModule.cs

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System.Collections.Generic;
12
using System.Threading.Tasks;
23
using Discord;
34
using Discord.WebSocket;
5+
using Gommon;
46
using Qmmands;
57
using Volte.Core.Entities;
68
using Volte.Services;
@@ -18,39 +20,57 @@ public sealed class StarboardModule : VolteModule
1820

1921
[Command("Channel", "Ch")]
2022
[Description("Sets the channel to be used by starboard when a message is starred.")]
21-
public Task<ActionResult> ChannelAsync(SocketTextChannel channel)
23+
public Task<ActionResult> ChannelAsync(
24+
[Description("The channel to be used by Starboard.")] SocketTextChannel channel)
2225
{
23-
Context.Modify(data =>
24-
{
25-
data.Configuration.Starboard.StarboardChannel = channel.Id;
26-
});
26+
Context.Modify(data => data.Configuration.Starboard.StarboardChannel = channel.Id);
2727
return Ok($"Successfully set the starboard channel to {MentionUtils.MentionChannel(channel.Id)}.");
2828
}
2929

3030
[Command("Amount", "Count")]
3131
[Description("Sets the amount of stars required on a message for it to be posted to the Starboard.")]
32-
public Task<ActionResult> AmountAsync(int amount)
32+
public Task<ActionResult> AmountAsync(
33+
[Description("The desired star count threshold before posting it in the starboard channel.")] int amount)
3334
{
3435
if (amount < 1)
35-
{
3636
return BadRequest("Amount must be larger than zero.");
37-
}
37+
38+
39+
Context.Modify(data => data.Configuration.Starboard.StarsRequiredToPost = amount);
40+
41+
return Ok($"Set the amount of stars required to be posted as a starboard message to **{amount}**.");
42+
}
3843

44+
[Command("Setup")]
45+
[Description("A one-off command that creates a channel for Starboard, with read-only permissions for everyone, and enables the starboard.")]
46+
public async Task<ActionResult> SetupAsync(
47+
[Description("The name for the Starboard channel that will be created."), Remainder] string channelName = "starboard")
48+
{
49+
var channel = await Context.Guild.CreateTextChannelAsync(channelName.Replace(" ", "-"), props =>
50+
{
51+
props.CategoryId = Context.Channel.CategoryId;
52+
props.PermissionOverwrites = new List<Overwrite>
53+
{
54+
new Overwrite(Context.Guild.EveryoneRole.Id, PermissionTarget.Role,
55+
new OverwritePermissions(viewChannel: PermValue.Allow, sendMessages: PermValue.Deny))
56+
};
57+
});
58+
3959
Context.Modify(data =>
4060
{
41-
data.Configuration.Starboard.StarsRequiredToPost = amount;
61+
data.Configuration.Starboard.Enabled = true;
62+
data.Configuration.Starboard.StarboardChannel = channel.Id;
4263
});
43-
return Ok($"Set the amount of stars required to be posted as a starboard message to **{amount}**.");
64+
65+
return Ok($"Successfully configured the Starboard functionality, and any starred messages will go to {channel.Mention}.");
4466
}
4567

4668
[Command("Enable")]
4769
[Description("Enable or disable the Starboard in this guild.")]
48-
public Task<ActionResult> EnableAsync(bool enabled)
70+
public Task<ActionResult> EnableAsync(
71+
[Description("Whether or not to enable or disable the Starboard.")] bool enabled)
4972
{
50-
Context.Modify(data =>
51-
{
52-
data.Configuration.Starboard.Enabled = enabled;
53-
});
73+
Context.Modify(data => data.Configuration.Starboard.Enabled = enabled);
5474
return Ok(
5575
enabled ? "Enabled the Starboard in this Guild." : "Disabled the Starboard in this Guild.");
5676
}

src/Commands/VolteContext.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Volte.Core;
1111
using Volte.Core.Entities;
1212
using Volte.Core.Helpers;
13-
using Volte.Interactive;
1413
using Volte.Services;
1514

1615
namespace Volte.Commands
@@ -23,15 +22,13 @@ public static VolteContext Create(SocketMessage msg, IServiceProvider provider)
2322
// ReSharper disable once SuggestBaseTypeForParameter
2423
private VolteContext(SocketMessage msg, IServiceProvider provider) : base(provider)
2524
{
26-
if (provider.TryGet<DiscordShardedClient>(out var client))
27-
Client = client;
25+
Client = provider.Get<DiscordShardedClient>();
2826
Guild = msg.Channel.Cast<SocketTextChannel>()?.Guild;
2927
Interactive = provider.Get<InteractiveService>();
3028
Channel = msg.Channel.Cast<SocketTextChannel>();
3129
User = msg.Author.Cast<SocketGuildUser>();
3230
Message = msg.Cast<SocketUserMessage>();
33-
if (provider.TryGet<DatabaseService>(out var db))
34-
GuildData = db.GetData(Guild);
31+
GuildData = provider.Get<DatabaseService>().GetData(Guild);
3532
Now = DateTime.Now;
3633
}
3734

@@ -44,7 +41,12 @@ private VolteContext(SocketMessage msg, IServiceProvider provider) : base(provid
4441
public SocketUserMessage Message { get; }
4542
public GuildData GuildData { get; }
4643
public DateTime Now { get; }
44+
45+
public Embed CreateEmbed(StringBuilder content) => CreateEmbed(content.ToString());
4746

47+
public Embed CreateEmbed(Action<EmbedBuilder> action)
48+
=> CreateEmbedBuilder().Apply(action).Build();
49+
4850
public Embed CreateEmbed(string content) => CreateEmbedBuilder(content).Build();
4951

5052
public EmbedBuilder CreateEmbedBuilder(string content = null) => new EmbedBuilder()

src/Core/Helpers/AsyncDuplicateLock.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Diagnostics.CodeAnalysis;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using Gommon;
76

87
namespace Volte.Core.Helpers
98
{

src/Core/Helpers/DiscordHelper.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public static void RegisterVolteEventHandlers(this DiscordShardedClient client,
150150
var mod = provider.Get<ModerationService>();
151151
var starboard = provider.Get<StarboardService>();
152152

153-
client.Log += m => Task.Run(() => Logger.HandleLogEvent(new LogEventArgs(m)));
153+
client.Log += async m =>
154+
{
155+
if (!(m.Message.ContainsIgnoreCase("unknown dispatch") &&
156+
m.Message.ContainsIgnoreCase("application_command")))
157+
await Task.Run(() => Logger.HandleLogEvent(new LogEventArgs(m)));
158+
};
154159

155160
if (provider.TryGet<GuildService>(out var guild))
156161
{

src/Services/PingChecksService.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public async Task CheckMessageAsync(MessageReceivedEventArgs args)
1313
{
1414
Logger.Debug(LogSource.Service,
1515
"Received a message to check for ping threshold violations.");
16-
if (args.Message.MentionedEveryone ||
17-
args.Message.MentionedUsers.Count > 10)
16+
if (args.Message.MentionedEveryone || args.Message.MentionedUsers.Count > 10)
1817
{
1918
await args.Message.DeleteAsync();
2019
Logger.Debug(LogSource.Service,

src/Services/QuoteService.cs

+17-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using Discord.WebSocket;
55
using Discord;
6+
using Discord.Rest;
67
using Gommon;
78
using Volte.Commands;
89
using Volte.Core.Entities;
@@ -32,17 +33,9 @@ public async Task<bool> CheckMessageAsync(MessageReceivedEventArgs args)
3233
var match = JumpUrlPattern.Match(args.Message.Content);
3334
if (!match.Success) return false;
3435

35-
if (!ulong.TryParse(match.Groups["GuildId"].Value, out var guildId) ||
36-
!ulong.TryParse(match.Groups["ChannelId"].Value, out var channelId) ||
37-
!ulong.TryParse(match.Groups["MessageId"].Value, out var messageId)) return false;
38-
39-
var g = await _client.Rest.GetGuildAsync(guildId);
40-
if (g is null) return false;
41-
var c = await g.GetTextChannelAsync(channelId);
42-
if (c is null) return false;
43-
44-
var m = await c.GetMessageAsync(messageId);
36+
var m = await GetMatchMessageAsync(match);
4537
if (m is null) return false;
38+
4639
if (m.Content.IsNullOrWhitespace() && !m.Embeds.IsEmpty()) return false;
4740

4841
await GenerateQuoteEmbed(m, args.Context).SendToAsync(args.Context.Channel)
@@ -54,6 +47,20 @@ await GenerateQuoteEmbed(m, args.Context).SendToAsync(args.Context.Channel)
5447
return true;
5548
}
5649

50+
private async Task<RestMessage> GetMatchMessageAsync(Match match)
51+
{
52+
if (!ulong.TryParse(match.Groups["GuildId"].Value, out var guildId) ||
53+
!ulong.TryParse(match.Groups["ChannelId"].Value, out var channelId) ||
54+
!ulong.TryParse(match.Groups["MessageId"].Value, out var messageId)) return null;
55+
56+
var g = await _client.Rest.GetGuildAsync(guildId);
57+
if (g is null) return null;
58+
var c = await g.GetTextChannelAsync(channelId);
59+
if (c is null) return null;
60+
61+
return await c.GetMessageAsync(messageId);
62+
}
63+
5764
private Embed GenerateQuoteEmbed(IMessage message, VolteContext ctx)
5865
{
5966
var e = ctx.CreateEmbedBuilder()

src/Services/StarboardService.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,16 @@ public EmbedBuilder GetStarboardEmbed(IMessage message)
270270
.WithAuthor(message.Author)
271271
.AddField("Posted", Format.Bold(Format.Url($"#{message.Channel.Name}", message.GetJumpUrl())));
272272

273-
if (Uri.IsWellFormedUriString(message.Content, UriKind.RelativeOrAbsolute))
274-
e.WithImageUrl(message.Content);
275-
else if (!message.Attachments.IsEmpty())
273+
if (!message.Attachments.IsEmpty() && !message.Content.IsNullOrEmpty())
274+
e.WithDescription(message.Content).WithImageUrl(message.Attachments.First().Url);
275+
if (message.Attachments.IsEmpty() && !message.Content.IsNullOrEmpty())
276+
e.WithDescription(message.Content);
277+
if (!message.Attachments.IsEmpty() && message.Content.IsNullOrEmpty())
276278
e.WithImageUrl(message.Attachments.First().Url);
277279

278-
if (message.Attachments.IsEmpty())
279-
e.WithDescription(message.Content);
280+
if (message.Attachments.Count > 1)
281+
e.WithFooter($"This message has {message.Attachments.Count - 1} more attachments. See original message.");
282+
280283
return e;
281284
}
282285

0 commit comments

Comments
 (0)