D
D
DoubleCool2020-12-11 05:05:20
Python
DoubleCool, 2020-12-11 05:05:20

Why is the bot not working as planned?

I decided to write a code to filter the chat. The bottom line is this: there is a black list of words, and a white list of words, and they are accompanied by a white list of roles (roles that the bot should not respond to, even if they wrote something from the black list of words).

The problem itself: at the moment, two roles from the white list of roles are specified in the code, but the bot continues to delete messages, as if there is no such thing as "white_list_roles". But if one role is specified, then everything works.

Also, a special thanks will be if you tell me how to integrate the case check into this code.

@client.event
async def on_message(message):
  black_list=["нига","негр"]
  white_list=["книга","неграмотный"]
  white_list_role1=discord.utils.get(message.guild.roles, name="Модератор")
  white_list_role2=discord.utils.get(message.guild.roles, name="Администратор")
  white_list_roles=[white_list_role1,white_list_role2]
  for roles in white_list_roles:
    if roles in message.author.roles:
      pass
    else:
      if message.content in black_list:
        await message.delete()
      elif message.content in white_list:
        pass

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2020-12-11
@DoubleCool

You can simply check as the intersection of two sets

if set(white_list_roles) & set(message.author.roles):
    # я админ, могу ругаться
else:
    # я холоп, ругаться нельзя
    if any(bad_word in message.content for bad_word in black_list):
        await message.delete()

For case sensitivity, you need to use the contents of the message in lower case in the same place. message.content.lower()
In this setting, it is not entirely clear how exactly the list of allowed words should be taken into account?

S
SagePtr, 2020-12-11
@SagePtr

You must first check the fulfillment of all white conditions, and only then check the black conditions if none of the white ones worked. Otherwise, at the first failed white check, it will start checking black ones and delete the message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question