简体   繁体   English

Discord 机器人不响应命令,尽管没有错误并且机器人在线

[英]Discord bot not responding to commands, although no errors and the bot is online

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...我尝试将命令更改为 on-message 方法,我尝试完全删除前缀并在 on_message 方法上使用前缀等等,我尝试在论坛和 Stack Overflow 上阅读,但似乎没有任何效果......

You made both a Client and a Bot .您同时创建了ClientBot The Bot is the one which has commands, but you're running the Client . Bot是有命令的,但你正在运行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.摆脱你的客户并运行Bot There's never a reason to have both a Client and a Bot.没有理由同时拥有客户端和机器人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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