【问题标题】:Discord Bot Command_not_workDiscord Bot Command_not_work
【发布时间】:2022-10-21 01:29:07
【问题描述】:

我的控制台没有显示任何错误,斜杠命令有效,但是当我输入“!dog”时,它应该返回“dog”,但没有任何反应。

'''

import discord
import random
from discord import app_commands
from discord.ext import commands

id_do_servidor = my_server_id  



class client(discord.Client):
  bot = commands.Bot(command_prefix='!',intents=discord.Intents.all()) 

  def __init__(self):
    super().__init__(intents=discord.Intents.default())

    self.synced = False  

  async def on_ready(self):
    await self.wait_until_ready()
    if not self.synced:  
      await tree.sync(
        guild=discord.Object(id=id_do_servidor)
      ) 
      self.synced = True
    print(f"{self.user.name} is online.")


aclient = client()
tree = app_commands.CommandTree(aclient)
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.command(name='dog')
async def sapo(ctx):
    await ctx.send('dog')


@tree.command(guild=discord.Object(id=id_do_servidor),
              name='test',
              description='test')  
async def slash2(interaction: discord.Interaction):
  await interaction.response.send_message(f"Working!",
                                          ephemeral=True)
  
  
aclient.run(
  'my_bot_token')

'''

如果有人知道解决方案,请帮助我,感谢您的关注。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您的代码中有一个不和谐客户端 (aclient) 和机器人对象 (bot),但您只在 aclient 上运行机器人(并且命令已注册到 bot 对象,所以因此不工作)。您不能同时使用客户端和机器人对象。在您的实例中,将 class client(discord.Client): 更改为 class client(commands.Bot): 并更改构造函数以使用您与机器人一起使用的参数:super().__init__((command_prefix="!", intents=discord.Intents.default())

    然后,只需使用bot 将代码中的更多引用替换为aclient,即: @bot.command(name='dog') -> @aclient.command(name='dog')

    【讨论】:

    • AttributeError:模块'discord'没有属性'Bot'
    • 修复了错误 - commands 来自导入 from discord.ext import commands
    猜你喜欢
    • 2021-06-15
    • 2021-07-03
    • 2021-01-19
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2021-06-28
    • 2019-07-04
    相关资源
    最近更新 更多