Do stuff when Discord-bot doesn't find character in a channel

661 Views Asked by At

First of all, I'm new to this stuff. I want to write a Discord-bot-function that looks for a character at the start of every message sent by users in a specific channel, and if the bot doesn't find that character two times in a row, it does stuff.

string neededCharacter = ">";

if (Dont.FindCharacterTwoTimesInARow.Equals(neededCharacter)) //If you don't find neededCharacter in sent messages two times in a row
{

   if (ChannelName.Equal("channel_name")) //look for the specific channel
   {

      //send message to the channel, stating that the character hasn't been found
      await Context.Channel.SendMessageAsync("'>' not found two times in a row");

   }

}

Note that ChannelName.Equal("channel_text") and Dont.FindCharacterTwoTimesInARow.Equals(neededCharacter) are quite obviously not real things, they just "show" what I want to do there.

Would be nice if someone could show me how something like that would be possible. Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

Since you are using Context object here. I assume that you are using a CommandModule in Discord.NET.

First, you can get a channel by Context.Channel, it returns an IMessage​Channel object.

If you want your bot to find a channel via the user tagging a channel. There is Context.Tags, which returns a list of tagged objects (as ITag). Just loop through it and get the tagged channel object.
(Docs about ITag object is here.)

For getting messages, if you meant past messages, there is GetMessagesAsync method for IMessage​Channel object.
So you can do var messages = Context.Channel.GetMessagesAsync(2);, and messages would be a list containing 2 elements of the recently sent messages.
(Docs for the method is here.)

(NOTE: The list itself would also contained the message that the user just sent to the bot. If you want the 2 messages before that, you can get 3 messages instead and ignore the first element.)

Finally, just loop through the list of messages or something, there is a String.StartWith() method on .NET to help make your life easier.
(It is in here on the .NET docs)