我想制作一个阅读消息的不和谐黑名单机器人,但我不能让他阅读消息

I want to make a discord blacklister bot that reads messages but i can't make him read the messages

我想制作一个 discord 机器人来将某些单词列入黑名单,为此我试图首先阅读用户发送的消息并检查该消息中的某个词是否被列入黑名单,然后删除该消息,但我不知道如何让机器人阅读消息

要阅读消息,您需要将回调函数分配给 Discord.net 为您提供的 MessageRecieved 事件。

public class Program {

    public static DiscordClient client;

    static void Main(string[] args) {

        // Create a discord client
        client = new DiscordClient(x => {

                x.AppName = "Your App Name";

                x.AppUrl = "";

                x.MessageCacheSize = 0;

                x.UsePermissionsCache = true;

                x.EnablePreUpdateEvents = true;

                x.LogLevel = LogSeverity.Debug;

                x.LogHandler = OnLogMessage;

                });

        // Assign a callback to the MessageRecieved event on client
        client.MessageReceived += OnMessageRecieved;

    }

    // Our MessageRecieved callback
    public static void OnMessageRecieved(object sender, MessageEventArgs args) {
        // Your code to handle messages here, the message string can be accessed by args.Message.Text
    }

}

好的,我做了一些事情,现在我有了这个

 public static async void OnMessageReceived(object sender, MessageEventArgs e)
    {
        if (e.Message.Text.Contains(toBlacklist.ToString()))
        {
            Message[] blacklistedMessagesToDelete;
            blacklistedMessagesToDelete = await e.Channel.DownloadMessages(1);

            await e.Channel.DeleteMessages(blacklistedMessagesToDelete);
        }
    }

但是 toBlacklist 是一个包含更多单词的数组,如果我不传入带引号的字符串而不是它,它不会删除消息

一些非常简单和草率的代码,但它应该可以工作

public static async void OnMessageReceived(object sender, MessageEventArgs e)
{
    bool found = false; //temporary bool, most likely unneeded
    foreach (var word in toBlacklist)
    {
        if (e.Message.RawText.ToLower().Contains(word.ToLower()))
        {
            found = true;
        }
    }
    if (found)
    {
        await e.Message.Delete();
    }
}