当命令位于DM中时,Python Discord.py bot将角色分配给服务器中的用户

2024-09-20 17:44:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在服务器中获得一个bot,当它被dm'ed时,它会给用户一个特定的角色。现在我只在服务器中使用命令时才让它工作。有没有办法让它进入dm频道

@client.command(brief="Sets favorite programming language", description="Set a role as either Python, C++, C#, or Java",
                aliases=["lang"])
async def language(ctx, lang=""):
    if isinstance(ctx.channel, discord.DMChannel):
        if lang.lower() not in ["python", 'c++', 'java', 'c#']:
            await ctx.send("The available languages are Python, C++, C#, and Java.")
        else:
            await ctx.send("Your role is now " + lang.lower().capitalize() + ".")
            await ctx.message.add_reaction("👌")
            role = get(client.get_guild(guild_id).roles, name=lang.lower().capitalize())
            print(ctx.message.author.guild(guild_id).roles)
            roles = [role for role in ctx.message.author.roles if role.name in ['C++', "Java", 'Python', 'C#']]
            await ctx.message.author.remove_roles(*roles)
            await ctx.message.author.add_roles(role)

Tags: in服务器clientmessagelangifdmjava
1条回答
网友
1楼 · 发布于 2024-09-20 17:44:04

您需要找到要将规则授予该公会中的用户的公会

如果您的bot是一台服务器的私有bot,您可以获取服务器id并使用client.get_guild(id)并将公会用作参考,或者如果您的bot是一个公共bot,将在多台服务器中使用,您应该添加另一个参数作为公会id,由用户提供

如果您要制作一个私人机器人,那么以下是您编辑的代码:

@client.command(brief="Sets favorite programming language", description="Set a role as either Python, C++, C#, or Java",
                aliases=["lang"])
async def language(ctx, lang=""):
    guild_id = 123456789   # Replace it with yours
    if lang.lower() not in ["python", 'c++', 'java', 'c#']:
        await ctx.send("The available languages are Python, C++, C#, and Java.")
    else:
        await ctx.send("Your role is now "+lang.lower().capitalize()+".")
        await ctx.message.add_reaction("👌")
        role = get(client.get_guild(guild_id).roles, name=lang.lower().capitalize())
        roles = [role for role in ctx.message.author.roles if role.name in ['C++', "Java", 'Python', 'C#']]
        await ctx.message.author.remove_roles(*roles)
        await ctx.message.author.add_roles(role)

如果你是一个公共机器人

@client.command(brief="Sets favorite programming language", description="Set a role as either Python, C++, C#, or Java",
                aliases=["lang"])
async def language(ctx, *args):
    lang = args[0]
    guild = client.get_guild(int(args[1])) if isinstance(ctx.channel, discord.DMChannel) else ctx.guild
    if lang.lower() not in ["python", 'c++', 'java', 'c#']:
        await ctx.send("The available languages are Python, C++, C#, and Java.")
    else:
        await ctx.send("Your role is now "+lang.lower().capitalize()+".")
        await ctx.message.add_reaction("👌")
        role = get(ctx.message.guild.roles, name=lang.lower().capitalize())
        roles = [role for role in guild.get_member(ctx.author.id).roles if role.name in ['C++', "Java", 'Python', 'C#']]
        await ctx.message.author.remove_roles(*roles)
        await ctx.message.author.add_roles(role)

相关问题 更多 >

    热门问题