Python Discord.py-ctx是必需参数

时间:2018-07-04 06:07:42

标签: python

我正在尝试制作一个不和谐的机器人,而且我正在遵循一个简单的教程,任何我都无法使用的最简单的命令。 我在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.

7 个答案:

答案 0 :(得分:4)

Discord.py命令默认情况下不传递上下文。您可以指定要通过传递的上下文作为装饰器的参数。

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

答案 1 :(得分:0)

documents

  

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

答案 2 :(得分:0)

有一个简单的解决方法:

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

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

答案 3 :(得分:0)

您不应同时初始化 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')

答案 4 :(得分:0)

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

import time
from discord.ext import commands

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

答案 5 :(得分:-1)

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

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

答案 6 :(得分:-3)

试试这个:

@client.command() 异步定义 Konnichiwa(ctx): await ctx.send("Konnichiwa,我的友达!")