Force enumeration of global collections before using them (#106)

This PR closes #105 

This PR fixes exceptions caused by changing a collection's contents
while it is being enumerated. This can often happen with Guild- and
MemberDatas. By using `ToArray()` on these global collections and using
it in the `foreach` loop, we create a new copy of the collection,
preventing any modification to it.

While this does introduce a lot of memory allocations, there is no
fixing that. Usually, the fix to these exceptions would be to convert
the `foreach` to a reverse-`for`. However, because indices cannot be
used on these collections, that is not possible.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-09-12 18:28:46 +05:00 committed by GitHub
parent 81595e58d3
commit 438ecfb41b
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View file

@ -85,7 +85,7 @@ public class RemindCommandGroup : CommandGroup
}
var builder = new StringBuilder();
for (var i = 0; i < data.Reminders.Count; i++)
for (var i = data.Reminders.Count - 1; i >= 0; i--)
{
var reminder = data.Reminders[i];
builder.AppendLine(
@ -168,8 +168,7 @@ public class RemindCommandGroup : CommandGroup
[RequireContext(ChannelContext.Guild)]
[UsedImplicitly]
public async Task<Result> ExecuteDeleteReminderAsync(
[Description("Index of reminder to delete")]
[MinValue(0)]
[Description("Index of reminder to delete")] [MinValue(0)]
int index)
{
if (!_context.TryGetContextIDs(out var guildId, out _, out var userId))