【问题标题】:Python Discord.py - ctx is a required argumentPython Discord.py - ctx 是必需的参数
【发布时间】:2018-12-12 11:44:48
【问题描述】:

我正在尝试制作一个不和谐的机器人,并且我正在学习一个简单的教程,但我无法使用最简单的命令来工作。 我在 python 3.6 上运行 discord.py 版本 0.16.12

    #Imports
import time
import discord
import asyncio
from discord.ext import commands

#Initialize
client = discord.Client()#Creates Client
bot = commands.Bot(command_prefix='!')#Sets prefix for commands(!Command)

#Code
@bot.command()
async def SendMessage(ctx):
    await ctx.send('Hello')

代码应该可以工作,但它给了我错误discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    默认情况下,Discord.py 命令不传递上下文。您指定您希望将上下文作为参数传递给装饰器。

    @bot.command(pass_context=True)
    async def SendMessage(ctx):
        await ctx.send('Hello')
    

    【讨论】:

    • 我试过你的修复,但现在它说“上下文”对象没有属性“发送”。
    • 听起来您正在尝试使用 v1.0 教程(有 Context.send 并且也不需要 pass_context=True)和 v0.* discord.py 版本。我建议仔细检查您的 discord.py 版本。
    【解决方案2】:

    来自documentation

    一个命令必须始终至少有一个参数 ctx,它是 Context 作为第一个。

    【讨论】:

      【解决方案3】:

      有一个简单的解决方法:

      bot = discord.Client()#Creates Client
      bot = commands.Bot(command_prefix='!')#Sets prefix for commands(!Command)
      

      只需将客户端更改为机器人即可。

      【讨论】:

        【解决方案4】:

        您不应同时初始化commands.Bot()discord.Client()。只需删除 client 变量,一切都会正常。

        # Imports
        import time
        import discord
        import asyncio
        from discord.ext import commands
        
        # Initialize
        bot = commands.Bot(command_prefix='!')
        
        # Code
        @bot.command()
        async def SendMessage(ctx):
            await ctx.send('Hello')
        

        【讨论】:

          【解决方案5】:

          试试这个:(选择你自己的前缀)

          import time
          from discord.ext import commands
          
          client = commands.Bot(command_prefix = '/')
                      
          @client.command()
          async def SendMessage(ctx):
              await ctx.send('Hello')
          

          【讨论】:

            【解决方案6】:
            discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.
            

            你得到这个的原因是因为你需要定义ctx

            【讨论】:

              【解决方案7】:

              试试这个:

              @client.command() async def Konnichiwa(ctx): await ctx.send("Konnichiwa,我的友达!")

              【讨论】:

                猜你喜欢
                • 2021-01-24
                • 2019-10-14
                • 2020-12-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-02-01
                • 1970-01-01
                • 2020-04-02
                相关资源
                最近更新 更多