我想制作一个不和谐的blacklister机器人来读取消息,但我不能让他阅读消息

时间:2017-05-29 17:58:59

标签: c# visual-studio discord blacklist discord.net

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

3 个答案:

答案 0 :(得分:0)

要阅读邮件,您需要为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
    }

}

答案 1 :(得分:0)

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

 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是一个包含更多单词的数组,如果我没有用引号代替它来传递字符串,它就不会删除该消息

答案 2 :(得分:0)

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

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();
    }
}