Skip to content

Commit

Permalink
fix: critical bug in interaction methods
Browse files Browse the repository at this point in the history
We shouldn't always send payload_json via multipart
Also, mentions was wrong
  • Loading branch information
Lulalaby committed Jan 4, 2024
1 parent 5348b81 commit 00f9780
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 39 deletions.
16 changes: 10 additions & 6 deletions DisCatSharp/Entities/Interaction/DiscordFollowupMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public string Content
/// <summary>
/// Mentions to send on this followup message.
/// </summary>
public IReadOnlyList<IMention> Mentions => this._mentions;

private readonly List<IMention> _mentions = [];
public List<IMention>? Mentions { get; private set; }

/// <summary>
/// Appends a collection of components to the message.
Expand Down Expand Up @@ -279,7 +277,10 @@ public DiscordFollowupMessageBuilder AddFiles(Dictionary<string, Stream> files,
/// <returns>The builder to chain calls with.</returns>
public DiscordFollowupMessageBuilder AddMention(IMention mention)
{
this._mentions.Add(mention);
if (this.Mentions != null)
this.Mentions.Add(mention);
else
this.Mentions = [mention];
return this;
}

Expand All @@ -290,7 +291,10 @@ public DiscordFollowupMessageBuilder AddMention(IMention mention)
/// <returns>The builder to chain calls with.</returns>
public DiscordFollowupMessageBuilder AddMentions(IEnumerable<IMention> mentions)
{
this._mentions.AddRange(mentions);
if (this.Mentions != null)
this.Mentions.AddRange(mentions);
else
this.Mentions = mentions.ToList();
return this;
}

Expand Down Expand Up @@ -338,7 +342,7 @@ public void Clear()
this.Content = "";
this._embeds.Clear();
this.IsTts = false;
this._mentions.Clear();
this.Mentions = null;
this._files.Clear();
this.IsEphemeral = false;
this._components.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public string Content
/// <summary>
/// Mentions to send on this interaction response.
/// </summary>
public IReadOnlyList<IMention> Mentions => this._mentions;

private readonly List<IMention> _mentions = [];
public List<IMention>? Mentions { get; private set; }

/// <summary>
/// The hints to send on this interaction response.
Expand All @@ -138,7 +136,7 @@ public DiscordInteractionResponseBuilder()
public DiscordInteractionResponseBuilder(DiscordMessageBuilder builder)
{
this._content = builder.Content;
this._mentions = builder.Mentions;
this.Mentions = builder.Mentions;
this._embeds.AddRange(builder.Embeds);
this._components.AddRange(builder.Components);
}
Expand Down Expand Up @@ -366,7 +364,10 @@ public DiscordInteractionResponseBuilder AddFiles(Dictionary<string, Stream> fil
/// <returns>The current builder to chain calls with.</returns>
public DiscordInteractionResponseBuilder AddMention(IMention mention)
{
this._mentions.Add(mention);
if (this.Mentions != null)
this.Mentions.Add(mention);
else
this.Mentions = [mention];
return this;
}

Expand All @@ -377,7 +378,10 @@ public DiscordInteractionResponseBuilder AddMention(IMention mention)
/// <returns>The current builder to chain calls with.</returns>
public DiscordInteractionResponseBuilder AddMentions(IEnumerable<IMention> mentions)
{
this._mentions.AddRange(mentions);
if (this.Mentions != null)
this.Mentions.AddRange(mentions);
else
this.Mentions = mentions.ToList();
return this;
}

Expand Down Expand Up @@ -426,7 +430,7 @@ public void Clear()
this._embeds.Clear();
this.IsTts = false;
this.IsEphemeral = false;
this._mentions.Clear();
this.Mentions = null;
this._components.Clear();
this._choices.Clear();
this._files.Clear();
Expand Down
2 changes: 1 addition & 1 deletion DisCatSharp/Entities/Message/DiscordMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public DiscordEmbed Embed
/// <summary>
/// Gets the Allowed Mentions for the message to be sent.
/// </summary>
public List<IMention> Mentions { get; private set; }
public List<IMention>? Mentions { get; private set; }

/// <summary>
/// Gets the Files to be sent in the Message.
Expand Down
18 changes: 11 additions & 7 deletions DisCatSharp/Entities/Webhook/DiscordWebhookBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public string Content
/// Name of the new thread.
/// Only works if the webhook is send in a <see cref="ChannelType.Forum"/>.
/// </summary>
public string ThreadName { get; set; }
public string? ThreadName { get; set; }

/// <summary>
/// Whether to keep previous attachments.
Expand All @@ -88,9 +88,7 @@ public string Content
/// <summary>
/// Mentions to send on this webhook request.
/// </summary>
public IReadOnlyList<IMention> Mentions => this._mentions;

private readonly List<IMention> _mentions = [];
public List<IMention>? Mentions { get; private set; }

/// <summary>
/// Gets the components.
Expand Down Expand Up @@ -361,7 +359,10 @@ public DiscordWebhookBuilder KeepAttachments(bool keep)
/// <param name="mention">Mention to add.</param>
public DiscordWebhookBuilder AddMention(IMention mention)
{
this._mentions.Add(mention);
if (this.Mentions != null)
this.Mentions.Add(mention);
else
this.Mentions = [mention];
return this;
}

Expand All @@ -371,7 +372,10 @@ public DiscordWebhookBuilder AddMention(IMention mention)
/// <param name="mentions">Mentions to add.</param>
public DiscordWebhookBuilder AddMentions(IEnumerable<IMention> mentions)
{
this._mentions.AddRange(mentions);
if (this.Mentions != null)
this.Mentions.AddRange(mentions);
else
this.Mentions = mentions.ToList();
return this;
}

Expand Down Expand Up @@ -448,7 +452,7 @@ public void Clear()
this.Content = "";
this._embeds.Clear();
this.IsTts = false;
this._mentions.Clear();
this.Mentions = null;
this._files.Clear();
this.AttachmentsInternal.Clear();
this._components.Clear();
Expand Down
46 changes: 28 additions & 18 deletions DisCatSharp/Net/Rest/DiscordApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6428,7 +6428,7 @@ internal async Task CreateInteractionResponseAsync(ulong interactionId, string i
}, out var path);

var url = Utilities.GetApiUriBuilderFor(path, this.Discord.Configuration).AddParameter("wait", "false").Build();
if (builder != null)
if (builder != null && values.Count is not 0)
{
await this.DoMultipartAsync(this.Discord, bucket, url, RestRequestMethod.POST, route, values: values, files: builder.Files).ConfigureAwait(false);

Expand Down Expand Up @@ -6547,6 +6547,8 @@ internal Task DeleteOriginalInteractionResponseAsync(ulong applicationId, string
/// <param name="builder">The builder.</param>
internal async Task<DiscordMessage> CreateFollowupMessageAsync(ulong applicationId, string interactionToken, DiscordFollowupMessageBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

builder.Validate();

if (builder.Embeds != null)
Expand All @@ -6555,15 +6557,12 @@ internal async Task<DiscordMessage> CreateFollowupMessageAsync(ulong application
embed.Timestamp = embed.Timestamp.Value.ToUniversalTime();

MessageFlags? flags = builder is { FlagsChanged: true } ? MessageFlags.None : null;
if (builder != null)
{
if (builder.IsEphemeral)
flags |= MessageFlags.Ephemeral;
if (builder.EmbedsSuppressed)
flags |= MessageFlags.SuppressedEmbeds;
if (builder.NotificationsSuppressed)
flags |= MessageFlags.SuppressNotifications;
}
if (builder.IsEphemeral)
flags |= MessageFlags.Ephemeral;
if (builder.EmbedsSuppressed)
flags |= MessageFlags.SuppressedEmbeds;
if (builder.NotificationsSuppressed)
flags |= MessageFlags.SuppressNotifications;

var values = new Dictionary<string, string>();
var pld = new RestFollowupMessageCreatePayload
Expand Down Expand Up @@ -6610,17 +6609,28 @@ internal async Task<DiscordMessage> CreateFollowupMessageAsync(ulong application
}, out var path);

var url = Utilities.GetApiUriBuilderFor(path, this.Discord.Configuration).AddParameter("wait", "true").Build();
var res = await this.DoMultipartAsync(this.Discord, bucket, url, RestRequestMethod.POST, route, values: values, files: builder.Files).ConfigureAwait(false);
var ret = DiscordJson.DeserializeObject<DiscordMessage>(res.Response, this.Discord);
RestResponse res;
if (values.Count is not 0)
{
res = await this.DoMultipartAsync(this.Discord, bucket, url, RestRequestMethod.POST, route, values: values, files: builder.Files).ConfigureAwait(false);
var ret = DiscordJson.DeserializeObject<DiscordMessage>(res.Response, this.Discord);

foreach (var att in ret.AttachmentsInternal)
att.Discord = this.Discord;
foreach (var att in ret.AttachmentsInternal)
att.Discord = this.Discord;

foreach (var file in builder.Files.Where(x => x.ResetPositionTo.HasValue))
file.Stream.Position = file.ResetPositionTo.Value;
foreach (var file in builder.Files.Where(x => x.ResetPositionTo.HasValue))
file.Stream.Position = file.ResetPositionTo.Value;
ret.Discord = this.Discord;
return ret;
}
else
{
res = await this.DoRequestAsync(this.Discord, bucket, url, RestRequestMethod.POST, route, payload: DiscordJson.SerializeObject(pld)).ConfigureAwait(false);
var ret = DiscordJson.DeserializeObject<DiscordMessage>(res.Response, this.Discord);

ret.Discord = this.Discord;
return ret;
ret.Discord = this.Discord;
return ret;
}
}

/// <summary>
Expand Down

0 comments on commit 00f9780

Please sign in to comment.