简体   繁体   中英

Discord.py bot.command being ignored, no error (no on_message present)

I'm working on a Discord bot. In it, I want to have a rock paper scissors function, yet it's not working. I don't get an error message, nothing shows up.

I deleted all on_messages to avoid any problems missing a await client.process_commands(message) could give me, just to make sure the code works. Still, nothing. I feel like the answer should be obvious, but I am simply blanking on it.

Here's my code:

import os
import discord
import random
from discord.ext import commands

my_secret = os.environ['TOKEN']

intents = discord.Intents().all()
bot = commands.Bot(command_prefix='!', intents=intents)

client = discord.Client()

@bot.command()
async def rps(ctx, message):
  answer = message.content.lower()
  choices = ["rock", "paper", "scissors"]
  computer_answer = random.choice(choices)
  if answer not in choices:
      await ctx.send('You have to answer with "rock" "paper" or "scissors", dummy!')
  else:
      if computer_answer == answer:
        await ctx.send('Darn, we tied... Rematch!!')
      if computer_answer == "rock":
        if answer == "paper":
          await ctx.send('Aw, no fair! You won...')
      if computer_answer == "paper":
        if answer == "rock":
          await ctx.send('Hehehe! I won~')
      if computer_answer == "scissors":
        if answer == "rock":
          await ctx.send('Arrgh!! Fine, you win.')
      if computer_answer == "scissors":
        if answer == "paper":
          await ctx.send('Hahaha! Take that!! I win~!')
      if computer_answer == "paper":
        if answer == "scissors":
          await ctx.send('Wow. Good for you /s')
      if computer_answer == "rock":
        if answer == "scissors":
          await ctx.send('Well, looks like I won ;)')
          
client.run(my_secret)

The bot works with client.event on_message just fine. Like I said, I deleted all that temporarily to make sure the code worked on its own, which it doesn't seem to despite the lack of errors. Sorry again if the answer is obvious, I've been stuck on this for hours and after many fruitless Google inquires and re-writes I am stumped.

No need for client. Discord.Client object can't process commands. return the on_message to the bot by doing :

@bot.event
async def on_message(message) :
   await message.channel.send("Hey")

And you must do the bot.process_commands(message) in the End of the on_message Event . eg :

@bot.event
async def on_message(message) :
   await message.channel.send("Hey")
   await bot.process_commands(message)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM