我已经为我的Discord Bot制作了此命令,该命令清除了频道中的一定数量的消息。它目前可以完成工作并做得很好,但是当它在最后显示消息时,它显示了他们想清除的消息数。但是,假设该频道只包含20条消息,但它们确实清除了50,我希望它显示其实际清除的消息数,所以它将显示20个。
。public class Clear : ModuleBase<SocketCommandContext>
{
[Command("clear"),RequireUserPermission(GuildPermission.ManageMessages)]
public async Task ClearMessages(int numToClear)
{
EmbedBuilder builder = new EmbedBuilder();
numToClear = numToClear + 1;
var messages = await Context.Channel.GetMessagesAsync(numToClear).Flatten();
await Context.Channel.DeleteMessagesAsync(messages);
builder.WithTitle("MESSAGES HAVE BEEN CLEARED!")
.WithDescription($"A total of `{numToClear} messages` were cleared from this chat!")
.WithColor(Color.DarkBlue);
var clearedMessage = await ReplyAsync("", false, builder.Build());
await Task.Delay(3000);
await clearedMessage.DeleteAsync();
}
}
messages
变量具有Count
属性:
var count = messages.Count();
或者您可以使用numToClear
参数:
var count = numToClear;