Skip to content
This repository was archived by the owner on Oct 31, 2018. It is now read-only.

Commit 64773a1

Browse files
committed
Fixed deleterange with two message IDs
1 parent 7477640 commit 64773a1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ContactsBot/Modules/Moderation.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,20 @@ public async Task Delete([Summary("The most recent message ID to start deleting
111111
if (Moderation.IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" })) // todo: replace this array with something better
112112
{
113113
var messageList = (await Context.Channel.GetMessagesAsync(500).Flatten()).ToList();
114-
int startIndex = messageList.IndexOf(messageList.FirstOrDefault(m => m.Id == startMessage)) + 1;
115-
int endIndex = messageList.IndexOf(messageList.FirstOrDefault(m => m.Id == endMessage), startIndex);
116-
await Context.Channel.DeleteMessagesAsync(messageList.GetRange(startIndex, endIndex - startIndex));
114+
int startIndex = messageList.FindIndex(m => m.Id == startMessage);
115+
int endIndex = messageList.FindIndex(m => m.Id == endMessage);
116+
if(startIndex == -1 || endIndex == -1)
117+
{
118+
await ReplyAsync("Couldn't delete messages: The start or end message ID couldn't be found");
119+
return;
120+
}
121+
var messageRange = (startIndex > endIndex) ? messageList.GetRange(endIndex - 1, (startIndex - endIndex) + 1) : messageList.GetRange(startIndex, (endIndex - startIndex) + 1);
122+
await Context.Channel.DeleteMessagesAsync(messageRange);
117123

118-
await ReplyAsync($"Deleted {endIndex - startIndex} messages");
124+
await ReplyAsync($"Deleted {Math.Abs(endIndex - startIndex) + 1} messages");
119125
}
120126
else
121-
await ReplyAsync("Couldn't delete message: Insufficient role");
127+
await ReplyAsync("Couldn't delete messages: Insufficient role");
122128
}
123129
}
124130
}

0 commit comments

Comments
 (0)