Skip to content

Commit b8dff67

Browse files
committed
chore: update dependencies
- DSharpPlus.* 4.4.9 - Microsoft.Extensions.Hosting 8.0.0 - Serilog 4.0.0 - Serilog.Extensions.Logging 8.0.0 - Serilog.Sinks.* 6.0.0 - X10D.* 4.0.0 With the release of X10D 4.0.0, the DSharpPlus companion library was discontinued. Relevant extension methods have been introduced to the project to counter this
1 parent dad59dd commit b8dff67

4 files changed

Lines changed: 301 additions & 12 deletions

File tree

EmbedBot/Commands/InfoCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using DSharpPlus.Entities;
44
using DSharpPlus.SlashCommands;
55
using DSharpPlus.SlashCommands.Attributes;
6+
using EmbedBot.Extensions;
67
using EmbedBot.Services;
78
using Humanizer;
8-
using X10D.DSharpPlus;
99

1010
namespace EmbedBot.Commands;
1111

EmbedBot/EmbedBot.csproj

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@
2828
</PropertyGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="DSharpPlus" Version="4.4.2"/>
32-
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.2"/>
33-
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.2"/>
31+
<PackageReference Include="DSharpPlus" Version="4.4.9"/>
32+
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.9"/>
33+
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.9"/>
3434
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
35-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1"/>
36-
<PackageReference Include="Serilog" Version="3.0.1"/>
37-
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0"/>
38-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0"/>
39-
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
40-
<PackageReference Include="X10D" Version="3.3.1"/>
41-
<PackageReference Include="X10D.DSharpPlus" Version="3.3.1"/>
42-
<PackageReference Include="X10D.Hosting" Version="3.3.1"/>
35+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/>
36+
<PackageReference Include="Serilog" Version="4.0.0"/>
37+
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0"/>
38+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0"/>
39+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0"/>
40+
<PackageReference Include="X10D" Version="4.0.0"/>
41+
<PackageReference Include="X10D.Hosting" Version="4.0.0"/>
4342
</ItemGroup>
4443

4544
<ItemGroup>
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
using DSharpPlus.Entities;
2+
3+
namespace EmbedBot.Extensions;
4+
5+
/// <summary>
6+
/// Extension methods for <see cref="DiscordEmbedBuilder" />.
7+
/// </summary>
8+
internal static class DiscordEmbedBuilderExtensions
9+
{
10+
/// <summary>
11+
/// Adds a field of any value type to the embed.
12+
/// </summary>
13+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
14+
/// <param name="name">The name of the embed field.</param>
15+
/// <param name="value">The value of the embed field.</param>
16+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
17+
/// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
18+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
19+
/// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception>
20+
public static DiscordEmbedBuilder AddField<T>(
21+
this DiscordEmbedBuilder builder,
22+
string name,
23+
T? value,
24+
bool inline = false)
25+
{
26+
if (builder is null)
27+
{
28+
throw new ArgumentNullException(nameof(builder));
29+
}
30+
31+
return builder.AddField(name, value?.ToString(), inline);
32+
}
33+
34+
/// <summary>
35+
/// Conditionally adds a field to the embed.
36+
/// </summary>
37+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
38+
/// <param name="condition">The condition whose value is used to determine whether the field will be added.</param>
39+
/// <param name="name">The name of the embed field.</param>
40+
/// <param name="value">The value of the embed field.</param>
41+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
42+
/// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
43+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
44+
/// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception>
45+
public static DiscordEmbedBuilder AddFieldIf<T>(
46+
this DiscordEmbedBuilder builder,
47+
bool condition,
48+
string name,
49+
T? value,
50+
bool inline = false)
51+
{
52+
if (builder is null)
53+
{
54+
throw new ArgumentNullException(nameof(builder));
55+
}
56+
57+
if (condition)
58+
{
59+
builder.AddField(name, value?.ToString(), inline);
60+
}
61+
62+
return builder;
63+
}
64+
65+
/// <summary>
66+
/// Conditionally adds a field to the embed.
67+
/// </summary>
68+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
69+
/// <param name="predicate">The predicate whose return value is used to determine whether the field will be added.</param>
70+
/// <param name="name">The name of the embed field.</param>
71+
/// <param name="value">The value of the embed field.</param>
72+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
73+
/// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
74+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
75+
/// <exception cref="ArgumentNullException">
76+
/// <para><paramref name="builder" /> is <see langword="null" />.</para>
77+
/// -or-
78+
/// <para><paramref name="predicate" /> is <see langword="null" />.</para>
79+
/// </exception>
80+
public static DiscordEmbedBuilder AddFieldIf<T>(
81+
this DiscordEmbedBuilder builder,
82+
Func<bool> predicate,
83+
string name,
84+
T? value,
85+
bool inline = false)
86+
{
87+
if (builder is null)
88+
{
89+
throw new ArgumentNullException(nameof(builder));
90+
}
91+
92+
if (predicate is null)
93+
{
94+
throw new ArgumentNullException(nameof(predicate));
95+
}
96+
97+
if (predicate.Invoke())
98+
{
99+
builder.AddField(name, value?.ToString(), inline);
100+
}
101+
102+
return builder;
103+
}
104+
105+
/// <summary>
106+
/// Conditionally adds a field to the embed.
107+
/// </summary>
108+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
109+
/// <param name="predicate">The predicate whose return value is used to determine whether the field will be added.</param>
110+
/// <param name="name">The name of the embed field.</param>
111+
/// <param name="valueFactory">The delegate whose return value will be used as the value of the embed field.</param>
112+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
113+
/// <typeparam name="T">The return type of <paramref name="valueFactory" />.</typeparam>
114+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
115+
/// <exception cref="ArgumentNullException">
116+
/// <para><paramref name="builder" /> is <see langword="null" />.</para>
117+
/// -or-
118+
/// <para><paramref name="predicate" /> is <see langword="null" />.</para>
119+
/// -or-
120+
/// <para><paramref name="valueFactory" /> is <see langword="null" />.</para>
121+
/// </exception>
122+
public static DiscordEmbedBuilder AddFieldIf<T>(
123+
this DiscordEmbedBuilder builder,
124+
Func<bool> predicate,
125+
string name,
126+
Func<T?> valueFactory,
127+
bool inline = false)
128+
{
129+
if (builder is null)
130+
{
131+
throw new ArgumentNullException(nameof(builder));
132+
}
133+
134+
if (predicate is null)
135+
{
136+
throw new ArgumentNullException(nameof(predicate));
137+
}
138+
139+
if (valueFactory is null)
140+
{
141+
throw new ArgumentNullException(nameof(valueFactory));
142+
}
143+
144+
if (predicate.Invoke())
145+
{
146+
builder.AddField(name, valueFactory.Invoke()?.ToString(), inline);
147+
}
148+
149+
return builder;
150+
}
151+
152+
/// <summary>
153+
/// Conditionally adds a field to the embed.
154+
/// </summary>
155+
/// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param>
156+
/// <param name="condition">The condition whose value is used to determine whether the field will be added.</param>
157+
/// <param name="name">The name of the embed field.</param>
158+
/// <param name="valueFactory">The delegate whose return value will be used as the value of the embed field.</param>
159+
/// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param>
160+
/// <typeparam name="T">The return type of <paramref name="valueFactory" />.</typeparam>
161+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns>
162+
/// <exception cref="ArgumentNullException">
163+
/// <para><paramref name="builder" /> is <see langword="null" />.</para>
164+
/// -or-
165+
/// <para><paramref name="valueFactory" /> is <see langword="null" />.</para>
166+
/// </exception>
167+
public static DiscordEmbedBuilder AddFieldIf<T>(
168+
this DiscordEmbedBuilder builder,
169+
bool condition,
170+
string name,
171+
Func<T?> valueFactory,
172+
bool inline = false)
173+
{
174+
if (builder is null)
175+
{
176+
throw new ArgumentNullException(nameof(builder));
177+
}
178+
179+
if (valueFactory is null)
180+
{
181+
throw new ArgumentNullException(nameof(valueFactory));
182+
}
183+
184+
if (condition)
185+
{
186+
builder.AddField(name, valueFactory.Invoke()?.ToString(), inline);
187+
}
188+
189+
return builder;
190+
}
191+
192+
/// <summary>
193+
/// Sets the embed's author.
194+
/// </summary>
195+
/// <param name="builder">The embed builder to modify.</param>
196+
/// <param name="user">The author.</param>
197+
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />.</returns>
198+
public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder builder, DiscordUser user)
199+
{
200+
if (builder is null)
201+
{
202+
throw new ArgumentNullException(nameof(builder));
203+
}
204+
205+
if (user is null)
206+
{
207+
throw new ArgumentNullException(nameof(user));
208+
}
209+
210+
return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl);
211+
}
212+
}
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 EmbedBot.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);
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)