【问题标题】:Python Discord.py bot assign role to user in server when command is in DM当命令在 DM 中时,Python Discord.py bot 将角色分配给服务器中的用户
【发布时间】:2020-11-18 02:21:52
【问题描述】:

我正在尝试在服务器中安装一个机器人,当它被 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)

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

    您需要在该公会中找到您想要将规则提供给用户的公会。

    如果您的机器人是一台服务器的私有机器人,您可以获取服务器 ID 并使用 client.get_guild(id) 并使用公会作为参考,或者如果您的机器人是将在多个服务器中使用的公共机器人,您应该添加另一个参数作为公会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)
    

    【讨论】:

    • 在您的第一个解决方案中,我认为 ctx.author.roles 不起作用。我得到discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'User'对象没有属性'roles'
    • @JohnHenry5 我的错 ☺ 我不想改变我刚才所做的事情
    • 这个解决方案应该在 dm 频道中工作。如果你在上面看到,我放了一个 if 语句 if isinstance(ctx.channel, discord.DMChannel): 来检查它是否在 dm 通道中。我在“私人”机器人中使用您的解决方案,但它不起作用。我需要以某种方式在我拥有的服务器 ID 中获取对同一用户的引用。
    猜你喜欢
    • 2019-08-22
    • 2021-04-09
    • 2020-10-10
    • 2022-01-09
    • 2021-12-19
    • 2021-07-17
    • 2021-04-08
    • 2022-01-05
    • 2023-01-26
    相关资源
    最近更新 更多