Skip to content

Commit a3e4705

Browse files
committed
chore: update dependencies
* DSharpPlus.* 4.5.0 * Microsoft.EntityFrameworkCore.Sqlite 9.0.0 * Microsoft.Extensions.Hosting 9.0.0 * SmartFormat 3.5.1 * X10D.* 4.0.0 - X10D.DSharpPlus
1 parent 519468f commit a3e4705

17 files changed

Lines changed: 395 additions & 32 deletions

SuggestionBot/AutocompleteProviders/SuggestionAutocompleteProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using DSharpPlus.SlashCommands;
33
using Microsoft.Extensions.DependencyInjection;
44
using SuggestionBot.Data;
5+
using SuggestionBot.Extensions;
56
using SuggestionBot.Services;
6-
using X10D.DSharpPlus;
77

88
namespace SuggestionBot.AutocompleteProviders;
99

SuggestionBot/Commands/InfoCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using DSharpPlus.SlashCommands;
55
using DSharpPlus.SlashCommands.Attributes;
66
using Humanizer;
7+
using SuggestionBot.Extensions;
78
using SuggestionBot.Services;
8-
using X10D.DSharpPlus;
99

1010
namespace SuggestionBot.Commands;
1111

SuggestionBot/Commands/SuggestionCommand.SetStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Humanizer;
44
using SuggestionBot.AutocompleteProviders;
55
using SuggestionBot.Data;
6-
using X10D.DSharpPlus;
6+
using SuggestionBot.Extensions;
77

88
namespace SuggestionBot.Commands;
99

SuggestionBot/Commands/SuggestionCommand.Stats.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
namespace SuggestionBot.Commands;
77

8-
using MentionUtility = X10D.DSharpPlus.MentionUtility;
9-
108
internal sealed partial class SuggestionCommand
119
{
1210
[SlashCommand("stats", "View suggestion statistics.", false)]

SuggestionBot/Commands/SuggestionCommand.Top.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
namespace SuggestionBot.Commands;
77

8-
using MentionUtility = X10D.DSharpPlus.MentionUtility;
9-
108
internal sealed partial class SuggestionCommand
119
{
1210
[SlashCommand("top", "Views the top rated suggestions.", false)]

SuggestionBot/Data/EntityConfigurations/BlockedUserConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal sealed class BlockedUserConfiguration : IEntityTypeConfiguration<Blocke
1212
/// <inheritdoc />
1313
public void Configure(EntityTypeBuilder<BlockedUser> builder)
1414
{
15+
builder.ToTable("BlockedUsers");
1516
builder.HasKey(e => new { e.GuildId, e.UserId });
1617

1718
builder.Property(e => e.GuildId).IsRequired();

SuggestionBot/Data/EntityConfigurations/SuggestionConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal sealed class SuggestionConfiguration : IEntityTypeConfiguration<Suggest
1111
/// <inheritdoc />
1212
public void Configure(EntityTypeBuilder<Suggestion> builder)
1313
{
14+
builder.ToTable("Suggestions");
1415
builder.HasKey(e => new { e.GuildId, e.Id });
1516

1617
builder.Property(e => e.Id).IsRequired();
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
using DSharpPlus;
2+
using DSharpPlus.Entities;
3+
4+
namespace SuggestionBot.Extensions;
5+
6+
/// <summary>
7+
/// Extension methods for <see cref="DiscordEmbedBuilder" />.
8+
/// </summary>
9+
internal static class DiscordEmbedBuilderExtensions
10+
{
11+
/// <summary>
12+
/// Adds a notice to the end of the embed description notifying that the user should DM ModMail to discuss something with
13+
/// staff.
14+
/// </summary>
15+
/// <param name="embedBuilder">The embed builder to modify.</param>
16+
/// <param name="guild">The guild whose branding to add.</param>
17+
/// <param name="addThumbnail">
18+
/// <see langword="true" /> to show the guild's icon as the embed thumbnail; otherwise, <see langword="false" />.
19+
/// </param>
20+
/// <returns><paramref name="embedBuilder" />, to allow for method chaining.</returns>
21+
/// <exception cref="ArgumentNullException">
22+
/// <para><paramref name="embedBuilder" /> is <see langword="null" />.</para>
23+
/// -or-
24+
/// <para><paramref name="guild" /> is <see langword="null" />.</para>
25+
/// </exception>
26+
public static DiscordEmbedBuilder WithGuildInfo(
27+
this DiscordEmbedBuilder embedBuilder,
28+
DiscordGuild guild,
29+
bool addThumbnail = true
30+
)
31+
{
32+
if (embedBuilder is null) throw new ArgumentNullException(nameof(embedBuilder));
33+
if (guild is null) throw new ArgumentNullException(nameof(guild));
34+
35+
string iconUrl = guild.GetIconUrl(ImageFormat.Png);
36+
embedBuilder.WithFooter(guild.Name, iconUrl: iconUrl);
37+
if (addThumbnail) embedBuilder.WithThumbnail(iconUrl);
38+
return embedBuilder;
39+
}
40+
41+
42+
/// <summary>
43+
/// Adds a field of any value type to the embed.
44+
/// </summary>
45+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
46+
/// <param name="name">The name of the embed field.</param>
47+
/// <param name="value">The value of the embed field.</param>
48+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
49+
/// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
50+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
51+
/// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception>
52+
public static DiscordEmbedBuilder AddField<T>(
53+
this DiscordEmbedBuilder builder,
54+
string name,
55+
T? value,
56+
bool inline = false)
57+
{
58+
if (builder is null)
59+
{
60+
throw new ArgumentNullException(nameof(builder));
61+
}
62+
63+
return builder.AddField(name, value?.ToString(), inline);
64+
}
65+
66+
/// <summary>
67+
/// Conditionally adds a field to the embed.
68+
/// </summary>
69+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
70+
/// <param name="condition">The condition whose value is used to determine whether the field will be added.</param>
71+
/// <param name="name">The name of the embed field.</param>
72+
/// <param name="value">The value of the embed field.</param>
73+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
74+
/// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
75+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
76+
/// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception>
77+
public static DiscordEmbedBuilder AddFieldIf<T>(
78+
this DiscordEmbedBuilder builder,
79+
bool condition,
80+
string name,
81+
T? value,
82+
bool inline = false)
83+
{
84+
if (builder is null)
85+
{
86+
throw new ArgumentNullException(nameof(builder));
87+
}
88+
89+
if (condition)
90+
{
91+
builder.AddField(name, value?.ToString(), inline);
92+
}
93+
94+
return builder;
95+
}
96+
97+
/// <summary>
98+
/// Conditionally adds a field to the embed.
99+
/// </summary>
100+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
101+
/// <param name="predicate">The predicate whose return value is used to determine whether the field will be added.</param>
102+
/// <param name="name">The name of the embed field.</param>
103+
/// <param name="value">The value of the embed field.</param>
104+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
105+
/// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
106+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
107+
/// <exception cref="ArgumentNullException">
108+
/// <para><paramref name="builder" /> is <see langword="null" />.</para>
109+
/// -or-
110+
/// <para><paramref name="predicate" /> is <see langword="null" />.</para>
111+
/// </exception>
112+
public static DiscordEmbedBuilder AddFieldIf<T>(
113+
this DiscordEmbedBuilder builder,
114+
Func<bool> predicate,
115+
string name,
116+
T? value,
117+
bool inline = false)
118+
{
119+
if (builder is null)
120+
{
121+
throw new ArgumentNullException(nameof(builder));
122+
}
123+
124+
if (predicate is null)
125+
{
126+
throw new ArgumentNullException(nameof(predicate));
127+
}
128+
129+
if (predicate.Invoke())
130+
{
131+
builder.AddField(name, value?.ToString(), inline);
132+
}
133+
134+
return builder;
135+
}
136+
137+
/// <summary>
138+
/// Conditionally adds a field to the embed.
139+
/// </summary>
140+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
141+
/// <param name="predicate">The predicate whose return value is used to determine whether the field will be added.</param>
142+
/// <param name="name">The name of the embed field.</param>
143+
/// <param name="valueFactory">The delegate whose return value will be used as the value of the embed field.</param>
144+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
145+
/// <typeparam name="T">The return type of <paramref name="valueFactory" />.</typeparam>
146+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
147+
/// <exception cref="ArgumentNullException">
148+
/// <para><paramref name="builder" /> is <see langword="null" />.</para>
149+
/// -or-
150+
/// <para><paramref name="predicate" /> is <see langword="null" />.</para>
151+
/// -or-
152+
/// <para><paramref name="valueFactory" /> is <see langword="null" />.</para>
153+
/// </exception>
154+
public static DiscordEmbedBuilder AddFieldIf<T>(
155+
this DiscordEmbedBuilder builder,
156+
Func<bool> predicate,
157+
string name,
158+
Func<T?> valueFactory,
159+
bool inline = false)
160+
{
161+
if (builder is null)
162+
{
163+
throw new ArgumentNullException(nameof(builder));
164+
}
165+
166+
if (predicate is null)
167+
{
168+
throw new ArgumentNullException(nameof(predicate));
169+
}
170+
171+
if (valueFactory is null)
172+
{
173+
throw new ArgumentNullException(nameof(valueFactory));
174+
}
175+
176+
if (predicate.Invoke())
177+
{
178+
builder.AddField(name, valueFactory.Invoke()?.ToString(), inline);
179+
}
180+
181+
return builder;
182+
}
183+
184+
/// <summary>
185+
/// Conditionally adds a field to the embed.
186+
/// </summary>
187+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
188+
/// <param name="condition">The condition whose value is used to determine whether the field will be added.</param>
189+
/// <param name="name">The name of the embed field.</param>
190+
/// <param name="valueFactory">The delegate whose return value will be used as the value of the embed field.</param>
191+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
192+
/// <typeparam name="T">The return type of <paramref name="valueFactory" />.</typeparam>
193+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
194+
/// <exception cref="ArgumentNullException">
195+
/// <para><paramref name="builder" /> is <see langword="null" />.</para>
196+
/// -or-
197+
/// <para><paramref name="valueFactory" /> is <see langword="null" />.</para>
198+
/// </exception>
199+
public static DiscordEmbedBuilder AddFieldIf<T>(
200+
this DiscordEmbedBuilder builder,
201+
bool condition,
202+
string name,
203+
Func<T?> valueFactory,
204+
bool inline = false)
205+
{
206+
if (builder is null)
207+
{
208+
throw new ArgumentNullException(nameof(builder));
209+
}
210+
211+
if (valueFactory is null)
212+
{
213+
throw new ArgumentNullException(nameof(valueFactory));
214+
}
215+
216+
if (condition)
217+
{
218+
builder.AddField(name, valueFactory.Invoke()?.ToString(), inline);
219+
}
220+
221+
return builder;
222+
}
223+
224+
/// <summary>
225+
/// Sets the embed's author.
226+
/// </summary>
227+
/// <param name="builder">The embed builder to modify.</param>
228+
/// <param name="user">The author.</param>
229+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />.</returns>
230+
public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder builder, DiscordUser user)
231+
{
232+
if (builder is null)
233+
{
234+
throw new ArgumentNullException(nameof(builder));
235+
}
236+
237+
if (user is null)
238+
{
239+
throw new ArgumentNullException(nameof(user));
240+
}
241+
242+
return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl);
243+
}
244+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using DSharpPlus.Entities;
2+
using DSharpPlus.Exceptions;
3+
4+
namespace SuggestionBot.Extensions;
5+
6+
/// <summary>
7+
/// Extension methods for <see cref="DiscordUser" />.
8+
/// </summary>
9+
internal static class DiscordUserExtensions
10+
{
11+
/// <summary>
12+
/// Returns the current <see cref="DiscordUser" /> as a member of the specified guild.
13+
/// </summary>
14+
/// <param name="user">The user to transform.</param>
15+
/// <param name="guild">The guild whose member list to search.</param>
16+
/// <returns>
17+
/// A <see cref="DiscordMember" /> whose <see cref="DiscordMember.Guild" /> is equal to <paramref name="guild" />, or
18+
/// <see langword="null" /> if this user is not in the specified <paramref name="guild" />.
19+
/// </returns>
20+
/// <exception cref="ArgumentNullException">
21+
/// <para><paramref name="user" /> is <see langword="null" />.</para>
22+
/// -or-
23+
/// <para><paramref name="guild" /> is <see langword="null" />.</para>
24+
/// </exception>
25+
public static async Task<DiscordMember?> GetAsMemberOfAsync(this DiscordUser user, DiscordGuild guild)
26+
{
27+
if (user is null)
28+
{
29+
throw new ArgumentNullException(nameof(user));
30+
}
31+
32+
if (guild is null)
33+
{
34+
throw new ArgumentNullException(nameof(guild));
35+
}
36+
37+
if (user is DiscordMember member && member.Guild == guild)
38+
{
39+
return member;
40+
}
41+
42+
if (guild.Members.TryGetValue(user.Id, out member!))
43+
{
44+
return member;
45+
}
46+
47+
try
48+
{
49+
return await guild.GetMemberAsync(user.Id).ConfigureAwait(false);
50+
}
51+
catch (NotFoundException)
52+
{
53+
return null;
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Returns the user's username with the discriminator, in the format <c>username#discriminator</c>.
59+
/// </summary>
60+
/// <param name="user">The user whose username and discriminator to retrieve.</param>
61+
/// <returns>A string in the format <c>username#discriminator</c></returns>
62+
/// <exception cref="ArgumentNullException"><paramref name="user" /> is <see langword="null" />.</exception>
63+
public static string GetUsernameWithDiscriminator(this DiscordUser user)
64+
{
65+
if (user is null)
66+
{
67+
throw new ArgumentNullException(nameof(user));
68+
}
69+
70+
if (user.Discriminator == "0")
71+
{
72+
// user has a new username. see: https://discord.com/blog/usernames
73+
return user.Username;
74+
}
75+
76+
return $"{user.Username}#{user.Discriminator}";
77+
}
78+
}

0 commit comments

Comments
 (0)