我开发了一个随机分配角色的 discode 机器人,但它不起作用
I developed a discode bot that randomly assigns a role, but it doesn't work
我随机尝试创建一个不和谐的机器人,当我在其中发生错误时,它会为我分配哈利波特的宿舍。无论我如何努力修复它,它都无法修复。 discord.py的版本是1.3.2,目前使用的是Python3.8。我该如何修复该错误?
错误
discord.ext.commands.errors.missingrequiredargument: ctx is a required argument that is missing.
代码
app.py
...
@client.command(pass_context=True, name="add")
async def enter(self, ctx):
await ctx.send("Wait..")
await asyncio.sleep(3.0)
num = r.randint(1, 4)
if num == 1:
#Gryffindor
egg = r.randint(1, 2)
if egg == 1:
await ctx.send("Um... it's hard... It's very difficult...")
await asyncio.sleep(2.0)
await ctx.send("No Slytherin? Really?")
await asyncio.sleep(1.0)
await ctx.send("So, I can't help it.")
await asyncio.sleep(1.0)
else:
await ctx.send("Hmm...")
await ctx.send("Slytherin shouldn't go.")
await asyncio.sleep(1.0)
role = discord.utils.get(ctx.guild.roles, name="「」Gryffindor")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Gryffindor!")
if num == 2:
# Slytherin
await ctx.send("There's nothing to see!")
await asyncio.sleep(0.5)
role = discord.utils.get(ctx.guild.roles, name="「」Slytherin")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Slytherin!")
if num == 3:
# Hufflepuff
await ctx.send("Hmm...")
await asyncio.sleep(1.4)
role = discord.utils.get(ctx.guild.roles, name="「」Hufflepuff")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Hufflepuff!")
if num == 4:
#Lebenclaw
await ctx.send("Well, it would be hard for difficult ...")
await asyncio.sleep(1.2)
role = discord.utils.get(ctx.guild.roles, name="「」Lebenclaw")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Lebenclaw!")
bot = discord.Client()
这正是你的问题。
您目前使用的是什么
@client.command(pass_context=True, name="add")
是 discord.ext.commands 库,这意味着您正在使用 discord.py 的附加组件(但它包含在您的安装中)。这是它的文档:documentation
我们如何解决这个问题?
这很容易:
代替
client = discord.Client()
执行以下操作:(此代码摘自官方示例:here
from discord.ext import commands
client = commands.Bot(command_prefix='!')
始终将您的整个代码包含在问题中,或者至少包含一个可重现性最低的示例。
编辑:
看起来您正在使用旧代码片段,您不需要再使用 pass_context
。
我随机尝试创建一个不和谐的机器人,当我在其中发生错误时,它会为我分配哈利波特的宿舍。无论我如何努力修复它,它都无法修复。 discord.py的版本是1.3.2,目前使用的是Python3.8。我该如何修复该错误?
错误
discord.ext.commands.errors.missingrequiredargument: ctx is a required argument that is missing.
代码
app.py
...
@client.command(pass_context=True, name="add")
async def enter(self, ctx):
await ctx.send("Wait..")
await asyncio.sleep(3.0)
num = r.randint(1, 4)
if num == 1:
#Gryffindor
egg = r.randint(1, 2)
if egg == 1:
await ctx.send("Um... it's hard... It's very difficult...")
await asyncio.sleep(2.0)
await ctx.send("No Slytherin? Really?")
await asyncio.sleep(1.0)
await ctx.send("So, I can't help it.")
await asyncio.sleep(1.0)
else:
await ctx.send("Hmm...")
await ctx.send("Slytherin shouldn't go.")
await asyncio.sleep(1.0)
role = discord.utils.get(ctx.guild.roles, name="「」Gryffindor")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Gryffindor!")
if num == 2:
# Slytherin
await ctx.send("There's nothing to see!")
await asyncio.sleep(0.5)
role = discord.utils.get(ctx.guild.roles, name="「」Slytherin")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Slytherin!")
if num == 3:
# Hufflepuff
await ctx.send("Hmm...")
await asyncio.sleep(1.4)
role = discord.utils.get(ctx.guild.roles, name="「」Hufflepuff")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Hufflepuff!")
if num == 4:
#Lebenclaw
await ctx.send("Well, it would be hard for difficult ...")
await asyncio.sleep(1.2)
role = discord.utils.get(ctx.guild.roles, name="「」Lebenclaw")
user = ctx.message.author
await user.add_roles(role)
await ctx.send("Lebenclaw!")
bot = discord.Client()
这正是你的问题。
您目前使用的是什么
@client.command(pass_context=True, name="add")
是 discord.ext.commands 库,这意味着您正在使用 discord.py 的附加组件(但它包含在您的安装中)。这是它的文档:documentation
我们如何解决这个问题?
这很容易:
代替
client = discord.Client()
执行以下操作:(此代码摘自官方示例:here
from discord.ext import commands
client = commands.Bot(command_prefix='!')
始终将您的整个代码包含在问题中,或者至少包含一个可重现性最低的示例。
编辑:
看起来您正在使用旧代码片段,您不需要再使用 pass_context
。