【问题标题】:How to find users with a specific role in discord.py如何在 discord.py 中找到具有特定角色的用户
【发布时间】:2020-11-27 22:18:27
【问题描述】:

我正在尝试制作一个不和谐的机器人来查找具有特定角色的用户,以便它可以对这组用户做一些事情。我已经编写了代码来列出服务器中的所有成员,但我认为这不会有帮助。

import discord
#some code
client = discord.Client()
#some code
client.run("my token")

【问题讨论】:

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


    【解决方案1】:

    可以使用下面的,像这样使用$get_members ROLENAME

    ref for commands

    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix="$")
    
    @bot.event
    async def on_ready():
        print("Logged in as")
        print(bot.user.name)
        print("------")
    
    @bot.command()
    async def get_members(ctx,role_name):
        role = discord.utils.find(
            lambda r: r.name == role_name, ctx.guild.roles)
            
        for user in ctx.guild.members:
            if role in user.roles:
                await ctx.send(f"{user.mention} has the role {role.mention}")
    
    bot.run("TOKEN")
    

    【讨论】:

    • 机器人未定义。当我将“bot”更改为“client”时。客户端没有任何属性“命令”
    • 我建议你转而使用来自不和谐扩展的命令from discord.ext import commands 你可以查看我编辑的帖子以了解如何
    • bot 仍未定义
    • sorry my bad,用你可以这样用$get_members ROLENAME
    • 我只是想从脚本内部调用它,而不是从 bot 命令,但我不确定 ctx 是什么或如何将其传递给方法
    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2021-06-29
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多