Fix: NRE with rest interactions & no api call (again) (#2922)

* init

* add better error message in this case
This commit is contained in:
Quin Lynch
2024-05-11 17:21:12 -03:00
committed by GitHub
parent 3054505d4b
commit 83fdc8917e
9 changed files with 20 additions and 18 deletions

View File

@@ -27,20 +27,20 @@ namespace Discord.Rest
{
}
internal static async Task<RestCommandBaseData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall)
internal static async Task<RestCommandBaseData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, ulong? guildId, IRestMessageChannel channel, bool doApiCall)
{
var entity = new RestCommandBaseData(client, model);
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false);
await entity.UpdateAsync(client, model, guild, guildId, channel, doApiCall).ConfigureAwait(false);
return entity;
}
internal virtual Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall)
internal virtual Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, ulong? guildId, IRestMessageChannel channel, bool doApiCall)
{
Name = model.Name;
if (model.Resolved.IsSpecified && ResolvableData == null)
{
ResolvableData = new RestResolvableData<Model>();
return ResolvableData.PopulateAsync(client, guild, channel, model, doApiCall);
return ResolvableData.PopulateAsync(client, guild, guildId, channel, model, doApiCall);
}
return Task.CompletedTask;
}

View File

@@ -22,7 +22,7 @@ namespace Discord.Rest
internal readonly Dictionary<ulong, Attachment> Attachments
= new Dictionary<ulong, Attachment>();
internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model, bool doApiCall)
internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, ulong? guildId, IRestMessageChannel channel, T model, bool doApiCall)
{
var resolved = model.Resolved.Value;
@@ -67,7 +67,7 @@ namespace Discord.Rest
{
// pull the adjacent user model
member.Value.User = resolved.Users.Value.FirstOrDefault(x => x.Key == member.Key).Value;
var restMember = RestGuildUser.Create(discord, guild, member.Value);
var restMember = RestGuildUser.Create(discord, guild, member.Value, guildId);
GuildMembers.Add(ulong.Parse(member.Key), restMember);
}

View File

@@ -35,7 +35,7 @@ namespace Discord.Rest
? (DataModel)model.Data.Value
: null;
Data = await RestMessageCommandData.CreateAsync(client, dataModel, Guild, Channel, doApiCall).ConfigureAwait(false);
Data = await RestMessageCommandData.CreateAsync(client, dataModel, Guild, model.GuildId.ToNullable(), Channel, doApiCall).ConfigureAwait(false);
}
//IMessageCommandInteraction

View File

@@ -28,10 +28,10 @@ namespace Discord.Rest
internal RestMessageCommandData(DiscordRestClient client, Model model)
: base(client, model) { }
internal new static async Task<RestMessageCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall)
internal new static async Task<RestMessageCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, ulong? guildId, IRestMessageChannel channel, bool doApiCall)
{
var entity = new RestMessageCommandData(client, model);
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false);
await entity.UpdateAsync(client, model, guild, guildId, channel, doApiCall).ConfigureAwait(false);
return entity;
}

View File

@@ -38,7 +38,7 @@ namespace Discord.Rest
? (DataModel)model.Data.Value
: null;
Data = await RestUserCommandData.CreateAsync(client, dataModel, Guild, Channel, doApiCall).ConfigureAwait(false);
Data = await RestUserCommandData.CreateAsync(client, dataModel, Guild, model.GuildId.ToNullable(), Channel, doApiCall).ConfigureAwait(false);
}
//IUserCommandInteractionData

View File

@@ -26,10 +26,10 @@ namespace Discord.Rest
internal RestUserCommandData(DiscordRestClient client, Model model)
: base(client, model) { }
internal new static async Task<RestUserCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall)
internal new static async Task<RestUserCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, ulong? guildId, IRestMessageChannel channel, bool doApiCall)
{
var entity = new RestUserCommandData(client, model);
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false);
await entity.UpdateAsync(client, model, guild, guildId, channel, doApiCall).ConfigureAwait(false);
return entity;
}

View File

@@ -38,7 +38,7 @@ namespace Discord.Rest
? (DataModel)model.Data.Value
: null;
Data = await RestSlashCommandData.CreateAsync(client, dataModel, Guild, Channel, doApiCall).ConfigureAwait(false);
Data = await RestSlashCommandData.CreateAsync(client, dataModel, Guild, model.GuildId.ToNullable(), Channel, doApiCall).ConfigureAwait(false);
}
//ISlashCommandInteraction

View File

@@ -14,15 +14,15 @@ namespace Discord.Rest
internal RestSlashCommandData(DiscordRestClient client, Model model)
: base(client, model) { }
internal static new async Task<RestSlashCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall)
internal new static async Task<RestSlashCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, ulong? guildId, IRestMessageChannel channel, bool doApiCall)
{
var entity = new RestSlashCommandData(client, model);
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false);
await entity.UpdateAsync(client, model, guild, guildId, channel, doApiCall).ConfigureAwait(false);
return entity;
}
internal override async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall)
internal override async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, ulong? guildId, IRestMessageChannel channel, bool doApiCall)
{
await base.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false);
await base.UpdateAsync(client, model, guild, guildId, channel, doApiCall).ConfigureAwait(false);
Options = model.Options.IsSpecified
? model.Options.Value.Select(x => new RestSlashCommandDataOption(this, x)).ToImmutableArray()

View File

@@ -91,7 +91,9 @@ namespace Discord.Rest
{
if (guild is not null)
Guild = guild;
GuildId = guildId ?? Guild.Id;
// kind of dangerous if the callee fucks up the 'guildId' parameter.
GuildId = guildId ?? Guild?.Id ?? throw new ArgumentNullException(nameof(guild), $"Expected either {nameof(guild)} or {nameof(guildId)} to be non-null");
}
internal static RestGuildUser Create(BaseDiscordClient discord, IGuild guild, Model model, ulong? guildId = null)
{