繁体   English   中英

Python Discord.py - ctx 是必需参数

[英]Python Discord.py - ctx is a required argument

我正在尝试制作一个不和谐的机器人,我正在学习一个简单的教程,但我无法使用最简单的命令。 我在 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.

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

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

文档

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

对此有一个简单的解决方法:

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

只需将客户端更改为 bot 即可。

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

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

import time
from discord.ext import commands

client = commands.Bot(command_prefix = '/')
            
@client.command()
async def SendMessage(ctx):
    await ctx.send('Hello')
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

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

尝试这个:

@client.command() async def Konnichiwa(ctx): await ctx.send("Konnichiwa, my Tomodachi!")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM