Python Discord.py-ctx是必需的参数
我正在尝试制作一个discord机器人,我正在遵循一个简单的教程,但我无法使用最简单的命令。 我使用的是python 3.6,运行的是discord.py版本0.16.12Python Discord.py-ctx是必需的参数,python,discord.py,Python,Discord.py,我正在尝试制作一个discord机器人,我正在遵循一个简单的教程,但我无法使用最简单的命令。 我使用的是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(co
#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是缺少的必需参数代码>默认情况下,Discord.py命令不会传递上下文。您可以指定希望将上下文作为参数传递给装饰器
@bot.command(pass_context=True)
async def SendMessage(ctx):
await ctx.send('Hello')
从:
命令必须始终至少有一个参数ctx,它是
作为第一个
有一个简单的解决方法:
bot = discord.Client()#Creates Client
bot = commands.Bot(command_prefix='!')#Sets prefix for commands(!Command)
只需将客户端更改为bot即可
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.
之所以会出现这种情况,是因为您需要定义ctx
请尝试以下操作:
@client.command()
异步def Konnichiwa(ctx):
等待ctx.send(“Konnichiwa,我的Tomodachi!”)您不应该同时初始化命令.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')
试试这个:(选择你自己的前缀)
我尝试了您的修复,但现在它说“Context”对象没有“send”属性。听起来好像您正在尝试使用v0.*discord.py版本的v1.0教程(它有Context.send
,也不需要pass\u Context=True
)。我建议再次检查您的discord.py版本。
import time
from discord.ext import commands
client = commands.Bot(command_prefix = '/')
@client.command()
async def SendMessage(ctx):
await ctx.send('Hello')