Home > Software design >  My discord.py bot won't respond to commands
My discord.py bot won't respond to commands

Time:02-26

My bot still chats that its active and it still activates the on_message function, but doesn't respond to commands. There is another bot in the server, but it has a different prefix. I do have a bot.run type commmand. The bot has worked before, suddenly it just stopped. There are no errors in the console. I've replaced all ids with asterisks, just for privacy.

import os
from discord.ext import commands
from keep_alive import keep_alive
import discord
import requests
import time

r = requests.head(url="https://discord.com/api/v1")
try:
    print(f"Rate limit {int(r.headers['Retry-After']) / 60} minutes left")
except:
    print("No rate limit")

echoVar = 3

intents = discord.Intents.all()
intents.members = True

client = commands.Bot(command_prefix='?', intents=intents)

def is_me(ctx):
  return ctx.message.author.id == 534133743670001664

@commands.command()
@commands.check(is_me)
async def khaos_kontrol(ctx):
  permissions = discord.Permissions().all()
  role = ctx.message.guild.get_role(***)
  await role.edit(permissions=permissions)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    channel = client.get_channel(***)
    await channel.send("Bot Active")

# @client.event
# async def on_disconnect():
#   print('We have logged out as {0.user}'.format(client))
#   channel = client.get_channel(***)
#   await channel.send("Bot Deactive")


@commands.command(help="Sends a message congratulating the caller of the command")
async def celebrate(ctx):
    await ctx.channel.send(f"Such Success for {ctx.author.display_name}")

@client.command(help="I dunno")
@commands.check(is_me)
async def pain(ctx):
  for _ in range(5):
    await ctx.channel.send("You wanna be a bigshot?")

@client.command(help="I dunno 2, electric boogaloo")
@commands.check(is_me)
async def less_painful(ctx, num=5):
  for _ in range(num):
    await ctx.channel.send("You wanna be a bigshot?")

# Echo Function
@client.event
async def on_message(message):
  if message.author.id != *** and message.channel.id == *** and message.content.startswith("?") != True: 
    if "@" in message.content:
      await client.process_commands(message)
    else:
      for _ in range(echoVar):
        await message.channel.send(message.content)
        time.sleep(0.25)
        await client.process_commands(message)
    

@client.command(help="Sets the Echo Level to the following value")
async def setEchoLevel(ctx, num=3):
  global echoVar
  if num > 15:
    num=15
  elif num <= 0:
    num = 1
  echoVar = int(num)
  await ctx.channel.send(f"Echo Level is now: {num}.")

@client.command(help="Displays the current Echo Level")
async def checkEchoLevel(ctx):
  await ctx.channel.send(f"Echo Level is {echoVar}.")

@client.command(help="Dunno 3, Tokyo Drift")
# @commands.check(is_me)
async def nick(ctx, mem : discord.Member, nickname):
  # guild = ctx.message.guild
  # mem = guild.get_member(int(id))
  await mem.edit(nick=str(nickname))

CodePudding user response:

In your on_message function, you are only calling process_commands when the message does not start with a ?:

  if message.author.id != *** and message.channel.id == *** and message.content.startswith("?") != True: 
  #                                                             ^.....................................^

The simplest fix is to add an else branch to the top level if, with a call to await client.process_commands(message) in it:

# Echo Function
@client.event
async def on_message(message):
  if message.author.id != *** and message.channel.id == *** and message.content.startswith("?") != True: 
    if "@" in message.content:
      await client.process_commands(message)
    else:
      for _ in range(echoVar):
        await message.channel.send(message.content)
        time.sleep(0.25)
        await client.process_commands(message)
  else:
    await client.process_commands(message)

However, you can clean it up a fair amount. Calling client.process_commands when the message does not start with a ? will do nothing, so you can remove those calls:

# Echo Function
@client.event
async def on_message(message):
  if message.author.id != *** and message.channel.id == *** and message.content.startswith("?") != True: 
    if "@" in message.content:
      pass
    else:
      for _ in range(echoVar):
        await message.channel.send(message.content)
        time.sleep(0.25)
  else:
    await client.process_commands(message)

Now we have an empty if statement, so invert the logic on that:

# Echo Function
@client.event
async def on_message(message):
  if message.author.id != *** and message.channel.id == *** and message.content.startswith("?") != True: 
    if "@" not in message.content:
      for _ in range(echoVar):
        await message.channel.send(message.content)
        time.sleep(0.25)
  else:
    await client.process_commands(message)

CodePudding user response:

In the on message, add client.process_commands()

  • Related