Skip to content

Commit 4f355c8

Browse files
Fix /clear when attempting to clear 0 or 1 messages
Closes #310
1 parent ea5d482 commit 4f355c8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Commands/ClearCmds.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,19 @@ public async Task ClearSlashCommand(SlashCommandContext ctx,
105105
}
106106

107107
// List of messages to delete, up to (not including) the one we just got.
108-
var firstMsg = (await channel.GetMessagesAfterAsync(message.Id, 1).ToListAsync())[0];
108+
var firstMsg = (await channel.GetMessagesAfterAsync(message.Id, 1).ToListAsync()).FirstOrDefault();
109+
if (firstMsg is null)
110+
{
111+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I couldn't find any messages to clear! Please try again. (Hint: `up_to` is NOT inclusive.)");
112+
return;
113+
}
109114
var firstMsgId = firstMsg.Id;
110115
messagesToClear.Add(firstMsg);
111116
while (true)
112117
{
113118
var newMessages = (await channel.GetMessagesAfterAsync(firstMsgId, 100).ToListAsync()).OrderByDescending(x => x.Id).ToList();
119+
if (newMessages.Count == 0)
120+
break;
114121
messagesToClear.AddRange(newMessages);
115122
firstMsgId = newMessages.First().Id;
116123
if (newMessages.Count() < 100)

0 commit comments

Comments
 (0)