Discord.py: None of my commands work, but the bot still responds to things in on_message
Question:
I have a discord bot, and none of my commands work. Things in on_message
do work, like the response you get when pinging the bot. A very strange issue, and I also tried removing the commands and re-declaring them, I tried bot.Bot
and commands.Bot
and nothing worked.
I was making a poll command, it did not display the poll command at all and the command did not work.
Only the info command and help command worked.
Code:
import discord
from discord.ext import commands
import platform
import distro
import logging
logging.basicConfig(level=logging.DEBUG)
token = open("token.txt", "r").read()
#bot constructor
activity=discord.Game(name="I want my paycheck.")
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='br!', intents=intents, status=discord.Status.dnd, activity=activity)
#Cache clearer cause discord.py
for command in bot.commands:
bot.remove_command(command.name)
@bot.command(name='poll')
@commands.has_permissions(administrator=True)
async def poll(ctx, question, *options):
print('1')
if len(options) < 2:
await ctx.send('You need to provide at least two options.')
return
print('2')
if len(options) > 9:
await ctx.send('You can provide up to nine options.')
return
print('3')
reactions = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '][:len(options)]
description = 'n'.join(f'{i+1}. {option}' for i, option in enumerate(options))
embed = discord.Embed(title=question, description=description)
message = await ctx.send(embed=embed)
print('4')
async for reaction in reactions:
await message.add_reaction(reaction)
print('5')
@bot.command(name='info')
@commands.has_permissions(administrator=True)
async def info(ctx):
embed=discord.Embed(title="Bot info", description="Bot stats", color=0x2416e9)
embed.add_field(name="Ping:", value=f"`{bot.latency}ms`", inline=True)
embed.add_field(name="Python version", value=f"{platform.python_version()}", inline=True)
embed.add_field(name="Linux distro", value=f"{distro.name(pretty=True)}", inline=True)
await ctx.send(embed=embed)
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, welcome to SteamOS!'
)
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if bot.user.mentioned_in(message):
await message.channel.send(f'***Do you mind?***')
bot.run(token)
Answers:
I believe you need to add await bot.process_commands(message)
to your on_message. I had a problem like this happen to me and that fixed it.
That would make your on_message look like this:
@bot.event
async def on_message(message):
await bot.process_commands(message)
if message.author == bot.user:
return
if bot.user.mentioned_in(message):
await message.channel.send(f'***Do you mind?***')
I have a discord bot, and none of my commands work. Things in on_message
do work, like the response you get when pinging the bot. A very strange issue, and I also tried removing the commands and re-declaring them, I tried bot.Bot
and commands.Bot
and nothing worked.
I was making a poll command, it did not display the poll command at all and the command did not work.
Only the info command and help command worked.
Code:
import discord
from discord.ext import commands
import platform
import distro
import logging
logging.basicConfig(level=logging.DEBUG)
token = open("token.txt", "r").read()
#bot constructor
activity=discord.Game(name="I want my paycheck.")
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='br!', intents=intents, status=discord.Status.dnd, activity=activity)
#Cache clearer cause discord.py
for command in bot.commands:
bot.remove_command(command.name)
@bot.command(name='poll')
@commands.has_permissions(administrator=True)
async def poll(ctx, question, *options):
print('1')
if len(options) < 2:
await ctx.send('You need to provide at least two options.')
return
print('2')
if len(options) > 9:
await ctx.send('You can provide up to nine options.')
return
print('3')
reactions = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '][:len(options)]
description = 'n'.join(f'{i+1}. {option}' for i, option in enumerate(options))
embed = discord.Embed(title=question, description=description)
message = await ctx.send(embed=embed)
print('4')
async for reaction in reactions:
await message.add_reaction(reaction)
print('5')
@bot.command(name='info')
@commands.has_permissions(administrator=True)
async def info(ctx):
embed=discord.Embed(title="Bot info", description="Bot stats", color=0x2416e9)
embed.add_field(name="Ping:", value=f"`{bot.latency}ms`", inline=True)
embed.add_field(name="Python version", value=f"{platform.python_version()}", inline=True)
embed.add_field(name="Linux distro", value=f"{distro.name(pretty=True)}", inline=True)
await ctx.send(embed=embed)
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, welcome to SteamOS!'
)
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if bot.user.mentioned_in(message):
await message.channel.send(f'***Do you mind?***')
bot.run(token)
I believe you need to add await bot.process_commands(message)
to your on_message. I had a problem like this happen to me and that fixed it.
That would make your on_message look like this:
@bot.event
async def on_message(message):
await bot.process_commands(message)
if message.author == bot.user:
return
if bot.user.mentioned_in(message):
await message.channel.send(f'***Do you mind?***')