如何调用 discord.py 中的函数
How can you call a function in discord.py
我是 discord.py 的新手,我被这个错误困住了。我稍后会尝试在代码中调用命令 removerole。
第一部分工作正常,我可以输入 discord chat:
.removerole admin Rythm
它有效,但我在第二部分遇到错误:
'str' object has no attribute 'remove_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
您正在向 removerole 方法传递 3 个字符串。 ".remove", "admin", "Rythm"
。 "Rythm"
不是不和谐对象而是字符串。 Python 中的注释没有意义,编译器完全忽略它们。因此,突出显示 member
属于 discord.Member
类型并不会真正改变任何内容,只会让您感到困惑。
相反,您应该将 discord 用户对象传递到方法中。
我是这样改的,现在可以用了
@client.command()
async def removeRythm():
await removerole(ctx, ctx.message.guild.get_role(779716603448787004), ctx.message.guild.get_member(235088799074484224))
感谢您的帮助。
我是 discord.py 的新手,我被这个错误困住了。我稍后会尝试在代码中调用命令 removerole。
第一部分工作正常,我可以输入 discord chat:
.removerole admin Rythm
它有效,但我在第二部分遇到错误:
'str' object has no attribute 'remove_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
您正在向 removerole 方法传递 3 个字符串。 ".remove", "admin", "Rythm"
。 "Rythm"
不是不和谐对象而是字符串。 Python 中的注释没有意义,编译器完全忽略它们。因此,突出显示 member
属于 discord.Member
类型并不会真正改变任何内容,只会让您感到困惑。
相反,您应该将 discord 用户对象传递到方法中。
我是这样改的,现在可以用了
@client.command()
async def removeRythm():
await removerole(ctx, ctx.message.guild.get_role(779716603448787004), ctx.message.guild.get_member(235088799074484224))
感谢您的帮助。