Skip to content

Commit b41fcb4

Browse files
authored
annotate embed code for nullability (#1724)
* annotate embed code for nullability * annotate messages for nullability too * fix build errors from deprecations * handle empty collection
1 parent ef4cd11 commit b41fcb4

27 files changed

+246
-293
lines changed

DSharpPlus.CommandsNext/CommandsNextExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ public async Task DefaultHelpAsync(CommandContext ctx, [Description("Command to
924924

925925
CommandHelpMessage helpMessage = helpBuilder.Build();
926926

927-
DiscordMessageBuilder builder = new DiscordMessageBuilder().WithContent(helpMessage.Content).WithEmbed(helpMessage.Embed);
927+
DiscordMessageBuilder builder = new DiscordMessageBuilder().WithContent(helpMessage.Content).AddEmbed(helpMessage.Embed);
928928

929929
if (!ctx.Config.DmHelp || ctx.Channel is DiscordDmChannel || ctx.Guild is null || ctx.Member is null)
930930
{

DSharpPlus.Interactivity/EventHandling/Paginator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private async Task PaginateAsync(IPaginationRequest p, DiscordEmoji emoji)
262262
Page page = await p.GetPageAsync();
263263
DiscordMessageBuilder builder = new DiscordMessageBuilder()
264264
.WithContent(page.Content)
265-
.WithEmbed(page.Embed);
265+
.AddEmbed(page.Embed);
266266

267267
await builder.ModifyAsync(msg);
268268
}

DSharpPlus.Interactivity/InteractivityExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ public async Task SendPaginatedMessageAsync(
774774

775775
DiscordMessageBuilder builder = new DiscordMessageBuilder()
776776
.WithContent(pages.First().Content)
777-
.WithEmbed(pages.First().Embed)
777+
.AddEmbed(pages.First().Embed)
778778
.AddComponents(bts.ButtonArray);
779779

780780
DiscordMessage message = await builder.SendAsync(channel);
@@ -825,7 +825,7 @@ public async Task SendPaginatedMessageAsync(DiscordChannel channel, DiscordUser
825825
{
826826
DiscordMessageBuilder builder = new DiscordMessageBuilder()
827827
.WithContent(pages.First().Content)
828-
.WithEmbed(pages.First().Embed);
828+
.AddEmbed(pages.First().Embed);
829829
DiscordMessage m = await builder.SendAsync(channel);
830830

831831
TimeSpan timeout = timeoutoverride ?? this.Config.Timeout;

DSharpPlus.Rest/DiscordRestClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ public async Task<DiscordMessage> EditMessageAsync(ulong channel_id, ulong messa
879879
/// <returns></returns>
880880
public async Task<DiscordMessage> EditMessageAsync(ulong channel_id, ulong message_id, DiscordMessageBuilder builder, bool suppressEmbeds = false, IEnumerable<DiscordAttachment> attachments = default)
881881
{
882-
builder.Validate(true);
882+
builder.Validate();
883883

884884
return await this.ApiClient.EditMessageAsync(channel_id, message_id, builder.Content, new Optional<IEnumerable<DiscordEmbed>>(builder.Embeds), builder._mentions, builder.Components, builder.Files, suppressEmbeds ? MessageFlags.SuppressedEmbeds : null, attachments);
885885
}

DSharpPlus/Entities/Channel/Message/DiscordAttachment.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class DiscordAttachment : SnowflakeObject
1111
/// Gets the name of the file.
1212
/// </summary>
1313
[JsonProperty("filename", NullValueHandling = NullValueHandling.Ignore)]
14-
public string FileName { get; internal set; }
14+
public string? FileName { get; internal set; }
1515

1616
/// <summary>
1717
/// Gets the file size in bytes.
@@ -23,19 +23,19 @@ public class DiscordAttachment : SnowflakeObject
2323
/// Gets the media, or MIME, type of the file.
2424
/// </summary>
2525
[JsonProperty("content_type", NullValueHandling = NullValueHandling.Ignore)]
26-
public string MediaType { get; internal set; }
26+
public string? MediaType { get; internal set; }
2727

2828
/// <summary>
2929
/// Gets the URL of the file.
3030
/// </summary>
3131
[JsonProperty("url", NullValueHandling = NullValueHandling.Ignore)]
32-
public string Url { get; internal set; }
32+
public string? Url { get; internal set; }
3333

3434
/// <summary>
3535
/// Gets the proxied URL of the file.
3636
/// </summary>
3737
[JsonProperty("proxy_url", NullValueHandling = NullValueHandling.Ignore)]
38-
public string ProxyUrl { get; internal set; }
38+
public string? ProxyUrl { get; internal set; }
3939

4040
/// <summary>
4141
/// Gets the height. Applicable only if the attachment is an image.

DSharpPlus/Entities/Channel/Message/DiscordMentions.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ internal class DiscordMentions
2020
/// Collection roles to serialize
2121
/// </summary>
2222
[JsonProperty("roles", NullValueHandling = NullValueHandling.Ignore)]
23-
public IEnumerable<ulong> Roles { get; }
23+
public IEnumerable<ulong>? Roles { get; }
2424

2525
/// <summary>
2626
/// Collection of users to serialize
2727
/// </summary>
2828
[JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]
29-
public IEnumerable<ulong> Users { get; }
29+
public IEnumerable<ulong>? Users { get; }
3030

3131
/// <summary>
3232
/// The values to be parsed
3333
/// </summary>
3434
[JsonProperty("parse", NullValueHandling = NullValueHandling.Ignore)]
35-
public IEnumerable<string> Parse { get; }
35+
public IEnumerable<string>? Parse { get; }
3636

3737
// WHY IS THERE NO DOCSTRING HERE
3838
[JsonProperty("replied_user", NullValueHandling = NullValueHandling.Ignore)]
@@ -41,7 +41,7 @@ internal class DiscordMentions
4141
internal DiscordMentions(IEnumerable<IMention> mentions, bool mention = false, bool repliedUser = false)
4242
{
4343
//Null check just to be safe
44-
if (mentions == null)
44+
if (mentions is null)
4545
{
4646
return;
4747
}
@@ -52,21 +52,20 @@ internal DiscordMentions(IEnumerable<IMention> mentions, bool mention = false, b
5252
// Doing this allows for "no parsing"
5353
if (!mentions.Any())
5454
{
55-
this.Parse = Array.Empty<string>();
55+
this.Parse = [];
5656
return;
5757
}
5858

5959

6060
//Prepare a list of allowed IDs. We will be adding to these IDs.
61-
HashSet<ulong> roles = new HashSet<ulong>();
62-
HashSet<ulong> users = new HashSet<ulong>();
63-
HashSet<string> parse = new HashSet<string>();
61+
HashSet<ulong> roles = [];
62+
HashSet<ulong> users = [];
63+
HashSet<string> parse = [];
6464

6565
foreach (IMention m in mentions)
6666
{
6767
switch (m)
6868
{
69-
default: throw new NotSupportedException("Type not supported in mentions.");
7069
case UserMention u:
7170
if (u.Id.HasValue)
7271
{
@@ -97,6 +96,8 @@ internal DiscordMentions(IEnumerable<IMention> mentions, bool mention = false, b
9796

9897
case RepliedUserMention:
9998
break;
99+
100+
default: throw new NotSupportedException($"The type {m.GetType()} is not supported in allowed mentions.");
100101
}
101102
}
102103

0 commit comments

Comments
 (0)