【问题标题】:Discord.py: MissingRequiredArgument: self is a required argument that is missingDiscord.py:MissingRequiredArgument:self 是缺少的必需参数
【发布时间】:2021-06-02 11:20:03
【问题描述】:

我想在用户执行命令时更改状态。 我在自己的班级中有 Discord 机器人。要改变存在,我需要 self 参数。 但是当我写的时候

    @bot.command()
    async def change(self, ctx):
        await self.client.change_presence(activity=discord.Game("P-Hub"))

我得到错误:

discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

当我写的时候:

    @bot.command()
    async def change(ctx, self):
        await self.client.change_presence(activity=discord.Game("P-Hub"))

我收到此错误:

discord.ext.commands.errors.MissingRequiredArgument: self is a required argument that is missing.

整个代码是:

    class DiscordBot():
        def __init__(self, client, token):
            self.client = client
            self.token = token

        def run(self):
            self.client.run(self.token)

       @bot.command()
            async def change(ctx, self):
                await self.client.change_presence(activity=discord.Game("P-Hub"))

       @bot.event
           async def on_ready():
           print("My Ready is Body")

       @bot.listen()
           async def on_message(message):
               print(str(message.author) + ": " + message.content)

       if __name__ == '__main__':
           client = DiscordBot(bot, 'token')
           client.run()

有人有解决办法吗?

【问题讨论】:

    标签: python-3.x discord discord.py


    【解决方案1】:

    你为什么把它放在课堂上?当您像示例一样离开时,它可以正常工作。如果是关于在同一个脚本中运行 2 个机器人,那么使用这个 question 会对你有所帮助。

    @bot.event
    async def on_ready():
        print("My Ready is Body")
    
    @bot.command()
    async def change(ctx):
        await self.client.change_presence(activity=discord.Game("P-Hub"))
    
    bot.run('token')
    

    如果您需要使用多个文件,请查看cogs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2021-11-05
      • 2018-10-28
      • 2018-12-01
      • 2016-06-29
      • 2018-06-20
      相关资源
      最近更新 更多