0

Hello I was creating a bot with a method in which it reads a list from a text file and if a user messages that word the bot would just ban or kick said user without needing a command to be typed out. It works with deletion so I made it similar with the ban or kick methods and there seems to be no error but the event doesn’t run, here is what I have and yes I put the read txt file in the code so it is reading the txt file but not kicking or banning.

@client.event  
async def kick(member : discord.member,message):
  for kickable_word in kickable_words:
    if kickable_word in message.content:
        await member.kick()

i also tried running it on the on_message(message) method but it sadly wont work

Pj4172000
  • 1
  • 3

2 Answers2

0

In order to accomplish what you want, you'll need to register a different kind of event listener. The on_message listener is probably what you'll want.

You can get an example working with this:

from discord import Message
from discord.client import Client

c = Client()  # instantiate the client
kickable_words = ...  # some iterable


@c.event
def on_message(message: Message):  # register the on_message listener
    if message.author != c.user:  # check if the message was sent by the bot, if not then check if it should be kicked.
        for word in kickable_words:
            if word in message.content:
                await message.author.kick()  # get the author of the message and kick them.

Note that this method is not the recommended one, and simply available for reference since it seemed to be the intent from the question being asked.

The recommended method is to use a Cog and attach a listener to a Bot instead. See this reference for specifics: https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html#

Stephen
  • 1,498
  • 17
  • 26
-1

if kickable_word in message.content: is the problem. message.content is just a string and it won't find needed words in it. You could try going for if kickable_word in message.content.split(" "): or using re for example:
if re.search(kickable_word, message.content)

Maxsc
  • 229
  • 1
  • 5
  • 1
    This is untrue, try entering something like: `'word' in 'there is a word in this sentence'` into IDLE or a different python interpreter instance. You will see that it returns `True` as `'word'` lies within the string. you can also reference the following thread for more information: https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method – Stephen Jul 27 '20 at 21:50
  • Well, the whole this should just be run through `on_message` otherwise you can't do that without typing in a command. As far as my knowledge goes. – Maxsc Jul 27 '20 at 21:57