Python 如何在discord.py中调用函数
我是discord.py的新手,我一直被这个错误困扰着。我试图在代码的后面调用removole命令。Python 如何在discord.py中调用函数,python,discord,discord.py,discord.py-rewrite,Python,Discord,Discord.py,Discord.py Rewrite,我是discord.py的新手,我一直被这个错误困扰着。我试图在代码的后面调用removole命令。 第一部分很好,我可以输入discord chat: .removole管理算法 这是可行的,但我在第二部分中发现了一个错误: “str”对象没有属性“remove\u roles” import discord from discord.ext import commands client = commands.Bot(command_prefix = '.') @client.command
第一部分很好,我可以输入discord chat:
.removole管理算法
这是可行的,但我在第二部分中发现了一个错误:
“str”对象没有属性“remove\u roles”
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
@client.command()
async def removerole(ctx, role: discord.Role, member: discord.Member):
await member.remove_roles(role)
await ctx.send(f'Successfully removed {role.mention} from {member.mention}')
我想在聊天中键入.removeRythm,机器人将从Rythm中删除管理员角色
@client.command()
async def removeRythm():
await removerole(".remove", "admin", "Rythm")
如果可能的话,有人知道怎么做吗?感谢MB您正在传递到RemoveOLE方法3字符串中<代码>“.remove”、“admin”、“Rythm”<代码>“Rythm”不是不协调对象,而是字符串。Python中的注释没有任何意义,编译器完全忽略它们。因此,突出显示
member
属于discord类型。member
实际上不会改变任何东西,只会让您感到困惑
相反,您应该将一个不协调的用户对象传入该方法。我做了这样的更改,现在它可以工作了
@client.command()
async def removeRythm():
await removerole(ctx, ctx.message.guild.get_role(779716603448787004), ctx.message.guild.get_member(235088799074484224))
谢谢你的帮助。这是错误的。Discord.py使用类型暗示作为转换器,例如标记某人()将其转换为相关Discord.Member实例,角色、频道等也是如此。请查看API文档: