所以我的命令有问题。它似乎没有显示任何问题,但是当我使用该命令时,我从我的机器人那里得到了它。
"错误:输入文本的参数太少。">
我不知道我是否应该使用IUserMessage或SocketMessage,我已经尝试了两者,通常得到相同的错误。
我不知道我哪里做错了?我已经尝试解决此问题一段时间了,但我在试图解决这个问题时遇到了障碍。任何帮助将不胜感激。
[Command("purge")]
[Summary("deletes set amount of text")]
[RequireUserPermission(GuildPermission.Administrator)]
[RequireBotPermission(GuildPermission.Administrator)]
public async Task PurgeChat(IUserMessage msg, int amount)
{
int messagesDeleted = 0;
var purgeMessage = await msg.Channel.SendMessageAsync("_purging in progress, give me a minute.._");
var lastMessageID = purgeMessage.Id;
try
{
var stop = false;
while (amount > messagesDeleted)
{
var messages = await msg.Channel.GetMessagesAsync(lastMessageID, Direction.Before, 15).OfType<IUserMessage>().ToList();
foreach (IUserMessage message in messages)
{
await message.DeleteAsync();
messagesToDelete++;
if (messagesDeleted >= amount || messages.Count == 0)
{
stop = true;
break;
}
lastMessageID = messages.Last().Id;
}
if (stop || messages.Count == 0)
{
break;
}
}
}
catch (Exception ex)
{
if (ex.Message != "The server responded with error 404 (NotFound): "Unknown Message"")
{
await Context.Channel.SendMessageAsync("Something fucked up while trying to delete messages, you are going to have to manually Delete them for now. or try again");
}
}
finally
{
await purgeMessage.ModifyAsync(x => x.Content = "_Purge Completed, this message will self destruct in 5 seconds_");
await Task.Factory.StartNew(async () =>
{
await Task.Delay(5000);
await purgeMessage.DeleteAsync();
});
}
}
[Command("purge", RunMode = RunMode.Async)]
[Summary("Deletes the specified amount of messages.")]
[RequireUserPermission(GuildPermission.Administrator)]
[RequireBotPermission(ChannelPermission.ManageMessages)]
public async Task PurgeChat(uint amount)
{
var messages = await this.Context.Channel.GetMessagesAsync((int)amount + 1).Flatten();
await this.Context.Channel.DeleteMessagesAsync(messages);
const int delay = 5000;
var m = await this.ReplyAsync($"Purge completed. _This message will be deleted in {delay / 1000} seconds._");
await Task.Delay(delay);
await m.DeleteAsync();
}
以下是未来读者受益的 3 个要点:
1(在循环中单独删除许多消息将导致非常令人不快的速率限制异常。
var messages = await msg.Channel.GetMessagesAsync();
foreach (IUserMessage message in messages)
{
await message.DeleteAsync(); //rate limit exception if too many messages!!!
}
2(批量删除:你想使用BBell在他的答案中给出的DeleteMessagesAsync((方法来删除批量消息。 然而,它最近进入了ITextChannel。下面是现在如何使用它的示例:
var messages = await msg.Channel.GetMessagesAsync(100).FlattenAsync(); //defualt is 100
await (Context.Channel as SocketTextChannel).DeleteMessagesAsync(messages);
这是另一个在没有强制转换的情况下访问 ITextChannel 的示例。注意到区别了吗?
Context.Guild.GetTextChannel(id); //yes, because it implements ITextChannel!
Context.Guild.GetChannel(id); //no, cant delete messages.
这愚弄了我一段时间。
3( Discord 不会让您使用 DeleteMessagesAsync(( 删除超过 2 周的消息。 您必须使用消息手动完成每个操作。删除异步((。
我真诚地希望它有所帮助!
我看到了问题。你采用了一个名为 msg 的 IUserMessage 作为参数,而不仅仅是稍后声明它。这期望命令具有额外的输入,这对于清除命令没有实际意义。这是我所拥有的:
[Command("purge")]
[Name("purge <amount>")]
[Summary("Deletes a specified amount of messages")]
[RequireBotPermission(GuildPermission.ManageMessages)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task DelMesAsync(int delnum)
{
var items = await Context.Channel.GetMessagesAsync(delnum + 1).Flatten();
await Context.Channel.DeleteMessagesAsync(items);
}
我删除了所有消息传递部分,但这应该可以解决您的参数问题。
我的代码在不阻塞线程的情况下删除最大消息数。
好吧,在那里你只需要获取一个消息数组并开始删除每个IMessage
元素的消息。
[Command("clean")]
public async Task Clean(int max)
{
var messages = Context.Channel.GetMessagesAsync(max).Flatten();
foreach (var h in await messages.ToArrayAsync())
{
await this.Context.Channel.DeleteMessageAsync(h);
}
}