[英]Discord.py | A simple command to give a specific user a specific role?
I'm working on a bot that can give roles to users, but I'm not very well-versed in the discord.py syntax.我正在开发一个可以为用户分配角色的机器人,但我不太精通 discord.py 语法。
I've tried adapting code like this:我尝试过像这样调整代码:
if message.content.startswith('!!role'):
member = message.author
var = discord.utils.get(message.guild.roles, name = "Role A")
member.add_role(var)
But this code doesn't fit my purpose, I'm trying to get a function more like this: roleGive(member,role)
so then I could have a command in discord like !role @JohnSmith#0001 moderator
, and it would give the user JohnSmith the 'moderator' role.但是这段代码不符合我的目的,我试图让 function 更像这样: roleGive(member,role)
所以我可以在 discord 中有一个命令,比如!role @JohnSmith#0001 moderator
,它会给用户 JohnSmith 担任“主持人”角色。 Also, this function is going to be used elsewhere in the bot, so I'm trying to make the function reusable, so I could do something like (this is more-or-less pseudocode, I'm still learning):此外,这个 function 将在机器人的其他地方使用,所以我试图让 function 可重复使用,所以我可以做类似的事情(这或多或少是伪代码,我还在学习):
user = "JohnSmith#0001"
if 20 > user.messagecount() >= 10:
roleGive(user,"10 Messages Gang")
if 30 > user.messagecount() >= 20:
roleGive(user,"20 Messages Gang")
I also do want to stress that I have looked at a lot of similar questions on this site, and either the code is outdated and no longer works, or it does not fit my purpose.我还想强调一下,我在这个网站上查看了很多类似的问题,或者代码已经过时并且不再有效,或者它不符合我的目的。 Thanks for any help!谢谢你的帮助!
Try this for the function!试试这个功能!
#import discord
async def roleGive(member: discord.Member, role: discord.Role):
await member.add_roles(role)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.