简体   繁体   English

Discord.py - 给某人一个角色,但在 PM 和服务器 ID 中

[英]Discord.py - Give Someone a Role, but in the PM's and with the Server ID

I want to do something like that: If someone writes the command,GiveRole to the Bot in PM's, the next thing should be the Server ID, after that the Role, would something like that be possible, because i know that it is not hard to give someone a role with the bot?我想做这样的事情:如果有人在 PM 中写命令,GiveRole 给 Bot,接下来应该是服务器 ID,然后是角色,这样的事情是可能的,因为我知道这并不难让某人在机器人中扮演角色? but is it possible to do it with the PM's and Server ID.但是是否可以使用 PM 和服务器 ID 来做到这一点。 Thanks.谢谢。

Try this, but I'm not sure if it's gonna work.试试这个,但我不确定它是否会起作用。

@bot.command()
async def giverole(ctx, guild, role):
    if isinstance(ctx.channel, discord.DMChannel) == False: # Checking if this is a DM Channel
        return
    guild = bot.get_guild(int(guild)) # Getting guild
    if guild is None: # Checking if the guild exists
        await ctx.send("Invalid guild ID!")
        return
    role = guild.get_role(int(role)) # Getting role
    if role is None: # Checking if the role exists
       await ctx.send("Invalid role ID!")
       return
    await guild.get_member(ctx.guild.id).add_roles(role) # Give role to member
    await ctx.send(f"You received **{role.name}** role!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM