Discord.py-传递参数角色函数 [英] Discord.py - passing an argument role functions
问题描述
我想创建一个命令,例如!iknow @user.我认为是一个普通的验证机器人.这是我的代码,我只粘贴了重要部分:
I wanted to create a command, like !iknow @user . A normal verification bot I think. Here's my code, I only pasted the important parts:
import discord
from discord.ext import commands
@client.command(pass_context=True)
async def iknow(ctx, arg):
await ctx.send(arg)
unknownrole = discord.utils.get(ctx.guild.roles, name = "Unknown")
await client.remove_roles(arg, unknownrole)
knownrole = discord.utils.get(ctx.guild.roles, name = "Verified")
await client.add_roles(arg, knownrole)
(当用户加入服务器时,将自动传递 Unknown 角色.)
(The Unknown role is automatically passed when a user joins the server.)
问题是:我在第6行出现错误(我想我也将在第9行出现).
The problem is: I get an error on line 6 (and I think I will get on line 9, too).
文件"/home/archit/.local/lib/python3.7/site-packages/discord/ext/commands/core.py",第83行,已包装
ret =等待coro(* args,** kwargs)文件"mainbot.py",第6行,在iknow中等待client.remove_roles(arg,unknownrole)AttributeError:'Bot'对象没有属性'remove_roles'
File "/home/archit/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 83, in wrapped
ret = await coro(*args, **kwargs) File "mainbot.py", line 6, in iknow await client.remove_roles(arg, unknownrole) AttributeError: 'Bot' object has no attribute 'remove_roles'
我刚刚开始学习python,所以请不要欺负我!
I just started learning python, so please don't bully me!
推荐答案
您正在寻找 Member.remove_roles
和
You're looking for Member.remove_roles
and Member.add_roles
.
您还可以指定 arg
的类型必须为 discord.Member
,这将自动将您的提及解析为正确的 Member
对象.
You can also specify that arg
must be of type discord.Member
, which will automatically resolve your mention into the proper Member
object.
旁注,在创建命令时不再需要指定 pass_context = True
.这是自动完成的, context
始终是协程命令中的第一个变量.
Side note, it's no longer needed to specify pass_context=True
when creating commands. This is done automatically, and context
is always the first variable in the command coroutine.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
@client.command()
async def iknow(ctx, arg: discord.Member):
await ctx.send(arg)
unknownrole = discord.utils.get(ctx.guild.roles, name="Unknown")
await arg.remove_roles(unknownrole)
knownrole = discord.utils.get(ctx.guild.roles, name="Verified")
await arg.add_roles(knownrole)
client.run('token')
这篇关于Discord.py-传递参数角色函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!