所以我的命令出了问题。它似乎没有显示任何问题,但当我去使用命令时,我从我的机器人那里得到了这个。
“错误:输入文本的参数太少。”
我不知道我是否应该使用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();
});
}
}
答案 0 :(得分:3)
[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();
}
答案 1 :(得分:1)
以下是未来读者受益的3个要点:
1)在循环中单独删除许多消息将导致非常不愉快的速率限制异常。
var messages = await msg.Channel.GetMessagesAsync();
foreach (IUserMessage message in messages)
{
await message.DeleteAsync(); //rate limit exception if too many messages!!!
}
2) BULK删除:您希望使用BBell在其答案中提供的DeleteMessagesAsync()方法来删除 BULK 消息。然而它最近搬进了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周的邮件。您必须使用message.DeleteAsync()手动执行每个操作。
我真诚地希望它有所帮助!
答案 2 :(得分:1)
我的代码,用于在不阻止线程的情况下删除最大消息。
好吧,您只需要获取一组消息并开始删除每个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);
}
}
答案 3 :(得分:0)
我看到了问题。您接受了一个名为 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);
}
我取出了所有的消息部分,但这应该可以解决您的参数问题。