Discord.NET 1.0发送消息事件?

时间:2017-04-23 20:05:38

标签: c# events discord.net

我正在尝试在Discord.NET 1.0 中制作一个插件来审查某些单词,并且正在网上查找当有人发送短信时会触发的事件,但是不能正常一个地方。我知道如何在0.9.6中执行此操作,但在1.0中是否有办法?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

DiscordSocketClient有一个MessageReceived事件。

答案 1 :(得分:0)

首先,你要制作一个你想要审查的单词数组:

        String[] badWords = {"word1","word2", "word3"};

然后,您需要检查用户发送的消息是否包含这些消息,然后将其存储到字典中。

            for (int i = 0; i < badWords.Length; i++)
        {


            if (msg.Content.Contains(badWords[i]))
            {
                Console.WriteLine("true");
                if (wordOffences.ContainsKey(msg.Author.ToString()))
                {
                    wordOffences[msg.Author.ToString()] += 1;
                    Console.WriteLine("if");
                    Console.WriteLine(wordOffences[msg.Author.ToString()]);
                    File.WriteAllText(@"c:\Users\me\Desktop\real.json", JsonConvert.SerializeObject(wordOffences));
                    if (wordOffences[msg.Author.ToString()] >= 3)
                    {
                        Console.WriteLine("really true");
                        await m.Channel.SendMessageAsync("@Staff have been notified. Hey buddy watch your language! This is offence: " + wordOffences[msg.Author.ToString()]);

                    }
                    else
                    {
                        Console.WriteLine("y");
                        await m.Channel.SendMessageAsync("Hey buddy watch your language! This is offence: " + wordOffences[msg.Author.ToString()]);

                    }
                }
                else
                {
                    Console.WriteLine("runmf");
                    wordOffences.Add(msg.Author.ToString(), 1);
                    Console.WriteLine(wordOffences[msg.Author.ToString()]);
                    await m.Channel.SendMessageAsync("Hey buddy watch your language! This is offence: " + wordOffences[msg.Author.ToString()]);
                    File.WriteAllText(@"c:\Users\me\Desktop\real.json", JsonConvert.SerializeObject(wordOffences));

                }

                i = badWords.Length;


            }

使用此功能,您可以将用户拥有的攻击数存储到json文件中,因此它是永久性的。你应该把它放在你的: public async Task HandleCommandAsync(SocketMessage m)