Joe Biden
Joe Biden

Reputation: 50

Problems with a discord.py bot

I have been trying to code a Discord bot for my own server. However, it seems like, whenever I add more commands to the code, the ban, and kick functions no longer work correctly. I've tried rewriting the code multiple times, but it did not work. I've tried rearranging the codes, and that did not work as well.

client = commands.Bot(command_prefix = '!')

@client.command()
async def kick(ctx, member : discord.Member, *, reason = None):
  await member.kick(reason = reason)

@client.command()
async def ban(ctx, member : discord.Member, *, reason = None):
  await member.ban(reason = reason)


curseWord = ['die', 'kys', 'are you dumb', 'stfu', 'fuck you', 'nobody cares', 'do i care', 'bro shut up']

def get_quote():
  response = requests.get("https://zenquotes.io/api/random")
  json_data = json.loads(response.text)
  quote = json_data[0]['q'] + " -" + json_data[0]['a']
  return(quote)

#ready
@client.event
async def on_ready():
  print('LOGGED IN as mr MF {0.user}'.format(client))

@client.event
#detect self messages
async def on_message(message):
  if message.author == client.user:
    return
  #greeting
  if message.content.startswith('hello'):
    await message.channel.send('ay whatup ma gamer')
  #help
  if message.content.startswith('!therapy'):
    quote = get_quote()
    await message.channel.send(quote)
  #toxicity begone
  msg_content = message.content.lower()

  if any(word in msg_content for word in curseWord):
    await message.delete()

    
#environment token
token = os.environ['token']

client.run(token)

Upvotes: 1

Views: 112

Answers (1)

Daste
Daste

Reputation: 305

You have overwritten the on_message event, which by default processes commands (those marked with the @client.command() decorator). You need to explicitly tell the library to process commands when you overwrite it. As noted in the discord.py documentation, you should add this line at the end of the on_message event:

await client.process_commands(message)

Upvotes: 6

Related Questions