Home > Software design >  Discord bot not responding to commands, although no errors and the bot is online
Discord bot not responding to commands, although no errors and the bot is online

Time:01-10

My bot is not responding to commands, and there are no errors in the console. This is my code:

import discord
from discord.ui import Button, View
from discord.ext import commands
import database
from dotenv import load_dotenv
import asyncio
import os

load_dotenv('tokens.env')

TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents().all()
client = discord.Client(intents=intents)

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

adventure_slots = [True, True, True]
adventurers = {}

@bot.command(aliases=["c"])
async def characters(ctx):
  print(ctx)
  username = ctx.author.display_name
      
  page = 1
  if len(ctx.content.split()) > 1:
    page = int(ctx.content.split()[1])

  offset = (page - 1) * 10

  characters = database.get_characters(offset)

  embed = discord.Embed(title="Character List", color=0x0eebeb)

  character_list = ""
  total_characters = 0
  for character in characters:
    character_list  = "• " f"{character[1]} | {character[3]}\n"
    total_characters =1
  embed.add_field(name=f"{username}" " has a total of " f"{total_characters}" " characters:", value=character_list, inline=False)
  bot.process_commands(ctx) 
  footer_text = ""
  if page > 1:
    footer_text  = "<< Prev"
  if len(characters) == 10:
    footer_text  = " | Next >>"
  embed.set_footer(text=footer_text)

    # Send the character list message
  await ctx.channel.send(embed=embed)
  print (ctx)

@bot.command(aliases=["a"])
async def adventure(ctx):
  print(ctx)
  if all(not slot for slot in adventure_slots):
    await ctx.send("All adventure slots are currently full. Please try again later.")
    return

  message = await ctx.send("Please pick the character you would like to send by typing `r!'character_name'`")

  def check(m):
    return m.author == ctx.author and m.content.startswith("r!'") and len(m.content.split()) == 2
  try:
    character_message = await bot.wait_for("message", check=check, timeout=60.0)
  except asyncio.TimeoutError:
    await ctx.send("Timed out waiting for character name. Please try the `r!adventure` command again.")
    return

  character_name = character_message.content.split()[1]

  character = database.get_character(character_name)
  if not character:
    await ctx.send(f"Character `{character_name}` not found.")
    return

  for i, slot in enumerate(adventure_slots):
    if not slot:
      adventure_slots[i] = True
      break
  else:
    return

  await ctx.send(f"{character_name} has been sent on an adventure! They will return in 3 hours.")
  await asyncio.sleep(3 * 3600)  # 3 hours in seconds

  await ctx.author.send(f"{character}'s adventure is complete! You can collect your loot now.")
  bot.process_commands(ctx)

client.run(TOKEN)

I tried changing the commands to an on-message method and I tried removing the prefix entirely and using the prefix on the on_message method and more, I tried reading on forums and on Stack Overflow, but nothing seems to work...

CodePudding user response:

You made both a Client and a Bot. The Bot is the one which has commands, but you're running the Client.

TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents().all()
client = discord.Client(intents=intents)  # Not your Bot instance

bot = commands.Bot(command_prefix='?', intents=intents)  # Your Bot instance
...
client.run(TOKEN)  # Not your Bot instance

Get rid of your Client & run the Bot instead. There's never a reason to have both a Client and a Bot.

  • Related