试图从一组单词中删除消息

Trying to delete messages from an array of words

我正在尝试检查用户是否发布了一条包含黑名单字词的消息,如果发现黑名单字词则删除该消息,但是我无法检查任何 way.i 将 OnMessageReceived 回调分配给 MessageReceived 函数。我对此有点陌生,所以这可能是一个简单的答案,但过去 2 天我一直在努力弄清楚。

    private async Task OnMessageReceived(SocketMessage msg)
    {
        for (int i = 0; i < blacklistedWords.Length; i++)
        {
            if (msg.Content.Contains(blacklistedWords[i]))
            {
                Console.WriteLine(msg.Author.ToString() + " did a bad");
                await msg.Channel.DeleteMessagesAsync(); //I can't find out what to pass in here to delete the message
            }
        }
    }

尝试创建全局重复数据结构并将编辑的内容分配到数据结构中。即.E

List<SocketMessageModel > mylist = new List<SocketMessageModel >();

private async Task OnMessageReceived(SocketMessage msg)
{
    SocketMessageModel msgModel = new SocketMessageModel();
    msgModel.Content = msg.Content;
    msgModel.Author = msg.Author;
    Boolean blackListCheck = false;

    for (int i = 0; i < blacklistedWords.Length; i++)
    {
            Console.WriteLine(msg.Author.ToString() + " did a bad");
            if (msg.Content.Contains(blacklistedWords[i]))
            {
                blackListCheck = true;
                break;
            }

    }

    if (blackListCheck == false)
    {
        mylist.add(msg);
    }

}
public class SocketMessageModel
{
    public string Author {get; set; }
    public string Content {get; set; }
}

使用此解决方案,所有包含黑名单单词的消息都将从全局数据结构中排除。然后,在解析每条消息后,您可以对存储在一个简单列表中的所有消息做任何您想做的事情。

好的,我明白了。我只需要得到我试图先删除的消息。这是最终代码

    private async Task OnMessageReceived(SocketMessage msg)
    {
        foreach (var word in blacklistedWords)
        {
            if (msg.Content.Contains(word))
            {
                var messagesToDelete = await msg.Channel.GetMessagesAsync(1).Flatten();
                await msg.Channel.DeleteMessagesAsync(messagesToDelete);
            }
        }
    }