1

I'm trying to check if the user posted a message with a blacklisted word in it and delete the message if the blacklisted word is found however i'm not able to check for the words in any way.i assigned the OnMessageReceived callback to the MessageReceived function. I'm kinda new to this so it might be an easy answer but i've been trying to figure it out for the past 2 days.

    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
            }
        }
    }
ComxT
  • 13
  • 4

2 Answers2

0

Try creating a global duplicate data structure and assigning the edited content into the data structure. I.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; }
}

With this solution, all messages containing blacklisted words will be excluded from the global data structure. Then, you can do whatever you want with all of the messages stored in a simple list after you parse each message.

objectively C
  • 960
  • 9
  • 25
  • Do i need a using statement for the `List mylist = new List();` because it's missing a directive or assembly reffrence, the `msg.Content` is read only and at `Convert.ToCharacter` it says that Convert doesn't have a definition for ToCharacter – ComxT Nov 28 '17 at 20:17
  • Updated to reflect your comment. Research how to declare global variables here https://stackoverflow.com/questions/35083652/how-to-declare-a-variable-for-global-use – objectively C Nov 28 '17 at 20:25
  • What i'm trying to do is delete the messages containing the blacklisted words so storing other words doesn't seem needed. I don't see where i go with this because before i updated from 0.9.6 i could do this although i had to pass in the string everytime – ComxT Nov 28 '17 at 20:43
0

Ok I figured it out. I just needed to get the message I was trying to delete first. Here's the final code

    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);
            }
        }
    }
ComxT
  • 13
  • 4