【问题标题】:Why doesn't it work with Python and discord.py为什么它不适用于 Python 和 discord.py
【发布时间】:2020-05-07 00:47:07
【问题描述】:

我正在尝试使用 discord.py 执行简单的命令,但它不会在终端或 Discord 中给出任何错误。

代码如下:

import discord
from discord.ext import commands

class BotLibertarin(discord.Client):
    client = commands.Bot(command_prefix=".")

    @client.command()
    async def teste(ctx,*,arg):
        await ctx.channel.send(arg)

    @client.event
    async def on_message(self, message):
        print(f"message from {message.author} what he said {message.content}")


client = BotLibertarin()
client.run("")

【问题讨论】:

    标签: python bots discord discord.py discord.py-rewrite


    【解决方案1】:

    由于缩进不当,我不确定您的类的结构究竟如何,或者您为什么要分配 client 两次,但您需要子类化 commands.Bot 以使用命令扩展名,而不是 Client

    另外,您需要在 on_message 处理程序中使用 Bot.process_commands

    请参阅Why does on_message make my commands stop working? section of the FAQ in discord.py's documentation

    【讨论】:

    • 好吧,让我看看我是否明白你到底在说什么,我需要一个像 CommandHandler(commands.Bot) 这样的子类?
    • 如果您不覆盖任何方法,则不需要子类化。如果你做子类,你需要子类commands.Bot 以使用命令扩展。现在,您有两个独立的客户端,BotLibertarin 的实例,您的 Client 子类,这是您正在运行的,以及它的属性,它是 commands.Bot 的实例并且没有运行。您已经为commands.Bot 实例注册了命令和事件处理程序。事件处理程序也适用于 BotLibertarin 实例的唯一原因是您也在子类中覆盖它。
    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2016-12-16
    • 1970-01-01
    • 2018-08-05
    • 2013-05-22
    相关资源
    最近更新 更多