Python discord 模块,Role() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用discord.Role()

项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _signup_lottery(self, ctx):
        """Signs you up to track lottery stats.
        You must have the required role to sign-up for stat tracking.
        If you lose the role, or it changes your stats will still be tracked
        and the information can be viewed when you get the role again.

        By default, anyone can sign up.
        """
        author = ctx.message.author
        settings = self.check_server_settings(author.server)

        if author.id in settings["Members"]:
            return await self.bot.say("You are already signed-up to track stats.")

        role = settings["Config"]["Role"]
        if role not in [r.name for r in author.roles]:
            return await self.bot.say("You do not have the required role to track stats.")

        settings["Members"][author.id] = {"Name": author.name, "Entries": 0, "Won": 0}
        self.save_system()

        await self.bot.say("Congratulations {}, you can now start tracking your lottery stats. "
                           "To view your stats use the command "
                           "{}lottery stats.".format(author.name, ctx.prefix))
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _stats_lottery(self, ctx):
        """Shows your lottery stats
        Shows the number of times you have entered and the number
        of times you have won a lottery."""
        author = ctx.message.author
        settings = self.check_server_settings(author.server)
        if author.id not in settings["Members"]:
            return await self.bot.say("You are not a lottery member. Only members can view and "
                                      "track stats. Use [p]lottery signup to join.")
        role = settings["Config"]["Role"]
        if role not in [r.name for r in author.roles]:
            return await self.bot.say("You do not have the required role to view stats.")

        played = settings["Members"][author.id]["Entries"]
        won = settings["Members"][author.id]["Won"]

        embed = discord.Embed(description="Lottery Stat Tracker", color=0x50bdfe)
        embed.set_author(name=author.name)
        embed.set_thumbnail(url=author.avatar_url)
        embed.add_field(name="Entries", value=played, inline=True)
        embed.add_field(name="Won", value=won, inline=True)
        await self.bot.say(embed=embed)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _role_setlottery(self, ctx, role: discord.Role):
        """Sets the role required to track and view stats.
        Must be a role that exists on your server. If you delete this role
        you will need to update lottery with this command to the new role.
        Otherwise, no one will be able to view their stats, but it will still
        track if they were able to signup.

        By default this command is set to @everyone, and anyone can join.
        """
        server = ctx.message.server
        settings = self.check_server_settings(server)
        settings["Config"]["Role"] = role.name
        self.save_system()
        await self.bot.say("Changed the membership role to {}".format(role.name))

    # ================== Helper Functions ===================================
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def inventory(self, ctx):
        """Shows a list of items you have purchased"""
        user = ctx.message.author
        settings = self.check_server_settings(user.server)
        self.user_check(settings, user)
        title = "```{}```".format(self.bordered("{}'s\nI N V E N T O R Y".format(user.name)))
        if not settings["Users"][user.id]["Inventory"]:
            return await self.bot.say("Your inventory is empty.")

        column1 = ["[{}]".format(subdict["Item Name"].title())
                   if "Role" in subdict else subdict["Item Name"].title()
                   for subdict in settings["Users"][user.id]["Inventory"].values()
                   ]
        column2 = [subdict["Item Quantity"]
                   for subdict in settings["Users"][user.id]["Inventory"].values()
                   ]
        headers = ["Item Name", "Item Quantity"]
        data = sorted(list(zip(column1, column2)))
        method = settings["Config"]["Inventory Output Method"]
        msg = await self.inventory_split(user, title, headers, data, method)
        if method == "Chat":
            await self.bot.say(msg)
        else:
            await self.bot.whisper(msg)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _list_shop(self, ctx):
        """Shows a list of all the shop items. Roles are blue."""
        user = ctx.message.author
        settings = self.check_server_settings(user.server)
        shop_name = settings["Config"]["Shop Name"]
        column1 = ["[{}]".format(subdict["Item Name"].title())
                   if "Role" in subdict else subdict["Item Name"].title()
                   for subdict in settings["Shop List"].values()]
        column2 = [subdict["Quantity"] for subdict in settings["Shop List"].values()]
        column3 = [subdict["Item Cost"] for subdict in settings["Shop List"].values()]
        column4_raw = [subdict["Discount"] for subdict in settings["Shop List"].values()]
        column4 = [x + "%" if x != "0" else "None" for x in list(map(str, column4_raw))]
        if not column1:
            await self.bot.say("There are no items for sale in the shop.")
        else:
            data, header = self.table_builder(settings, column1, column2,
                                              column3, column4, shop_name)
            msg = await self.shop_table_split(user, data)
            await self.shop_list_output(settings, msg, header)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def redeem_handler(self, settings, ctx, user, itemname, confirmation):
        time_now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        item_dict = {"Name": user.name, "Confirmation Number": confirmation, "Status": "Pending",
                     "Time Stamp": time_now, "Item": itemname}
        if "Role" in settings["Users"][user.id]["Inventory"][itemname]:
            if "Buyrole" in self.bot.cogs:
                roleid = settings["Users"][user.id]["Inventory"][itemname]["Role"]
                role = [role for role in ctx.message.server.roles if roleid == role.id][0]
                await self.bot.add_roles(user, role)
                return True
            else:
                raise RuntimeError('I need the buyrole cog to process this request.')
        elif user.id in settings["Pending"]:
                if len(settings["Pending"][user.id].keys()) <= 12:
                    settings["Pending"][user.id][confirmation] = item_dict
                    dataIO.save_json(self.file_path, self.system)
                    return True
                else:
                    return False
        else:
            settings["Pending"][user.id] = {}
            settings["Pending"][user.id][confirmation] = item_dict
            dataIO.save_json(self.file_path, self.system)
            return True
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def shop_item_add(self, settings, itemname, cost, quantity, role=False):
        if role is False:
            item = itemname.title()
            settings["Shop List"][item] = {"Item Name": itemname, "Item Cost": cost,
                                           "Discount": 0, "Members Only": "No",
                                           "Buy Msg": []}
        else:
            item = str(itemname.name).title()
            settings["Shop List"][item] = {"Item Name": item, "Item Cost": cost,
                                           "Discount": 0, "Members Only": "No",
                                           "Role": itemname.id, "Buy Msg": []}
        if quantity == 0:
            settings["Shop List"][item]["Quantity"] = "?"
        else:
            settings["Shop List"][item]["Quantity"] = quantity
        dataIO.save_json(self.file_path, self.system)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def check_server_settings(self, server):
        if server.id not in self.system["Servers"]:
            self.system["Servers"][server.id] = {"Shop List": {},
                                                 "Users": {},
                                                 "Pending": {},
                                                 "Config": {"Shop Name": "Jumpman's",
                                                            "Shop Open": True,
                                                            "Shop Notify": False,
                                                            "Shop Role": None,
                                                            "Trade Cooldown": 30,
                                                            "Store Output Method": "Chat",
                                                            "Inventory Output Method": "Chat",
                                                            "Sort Method": "Alphabet",
                                                            "Member Discount": None,
                                                            "Pending Type": "Manual"}
                                                 }
            dataIO.save_json(self.file_path, self.system)
            print("Creating default Shop settings for Server: {}".format(server.name))
            path = self.system["Servers"][server.id]
            return path
        else:
            path = self.system["Servers"][server.id]
            if "Shop Role" not in path["Config"]:
                path["Config"]["Shop Role"] = None
            return path
项目:PTSCogs    作者:PlanetTeamSpeakk    | 项目源码 | 文件源码
def getoff(self, ctx, role):
        """Removes a giveme from you, by name which should be defined in [p]giveme list."""
        if ctx.message.server.id not in self.settings:
            await self.bot.say("This server has no giveme's I can remove.")
        elif role not in list(self.settings[ctx.message.server.id]['givemes'].keys()):
            await self.bot.say("That's not a valid giveme.")
        else:
            try:
                if not ctx.message.server.me.permissions_in(ctx.message.channel).manage_roles:
                    await self.bot.say("I do not have the manage roles permission here, I cannot remove roles from you untill I do.")
                else:
                    role = discord.utils.get(ctx.message.server.roles, id=self.settings[ctx.message.server.id]['givemes'][role])
                    await self.bot.remove_roles(ctx.message.author, role)
                    await self.bot.say("Role removed.")
            except Exception as e:
                await self.bot.say("An error occured while remove the role from you ({}).".format(e))
项目:Dwarf    作者:Dwarf-Community    | 项目源码 | 文件源码
def new_role(self, role, guild=None):
        """Creates a new Dwarf ?Role? object and connects it to the database.

        Parameters
        ----------
        role
            Can be a Discord ?Role? object or a role ID.
        guild : Optional
            Can be a Discord ?Server? object or a guild ID.
            Is not an optional parameter if ?role? is not a Discord ?Role? object.
        """

        if isinstance(role, discord.Role):
            return Role(id=role.id)
        else:
            if guild is None:
                raise ValueError("Either a Role object or both role ID "
                                 "and guild ID must be given as argument(s)")
            return Role(id=role)
项目:Excalibot    作者:endreman0    | 项目源码 | 文件源码
def base_command(self, ctx, *roles: discord.Role):
        """Base command for all opt-ins. With no subcommand, it functions like optin add."""
        if not roles:
            return await ctx.send('BAKA! You must specify roles!')

        settings = ctx.get(GuildOptins, id=ctx.guild.id).one_or_none()
        if settings is None or not settings.optin_roles:
            return await ctx.send('BAKA! This guild has no optins!')

        optin_ids = {optin.id for optin in settings.optin_roles}
        optin_roles = {role for role in roles if role.id in optin_ids}
        already_have = optin_roles & set(ctx.author.roles)
        given = optin_roles - already_have
        await ctx.author.add_roles(*given)

        responses = []
        if given:
            responses.append('You have opted into {}.'.format(format.list(role.name for role in given)))
        if already_have:
            responses.append('BAKA! You have already opted into {}!'.format(format.list(role.name for role in already_have)))
        if len(optin_roles) < len(roles):
            responses.append('BAKA! {}!'.format(format.list((role.name for role in set(roles) - optin_roles), ' is not an optin', ' are not optins')))
        await ctx.send('\n'.join(responses)) # One or more of the above is always true
项目:Excalibot    作者:endreman0    | 项目源码 | 文件源码
def remove(self, ctx, *roles: discord.Role):
        """Takes the named opt-ins from you."""
        if not roles:
            return await ctx.send('BAKA! You must specify roles!')

        settings = ctx.get(GuildOptins, id=ctx.guild.id).one_or_none()
        if settings is None or not settings.optin_roles:
            return await ctx.send('BAKA! This guild has no optins!')

        optin_ids = {optin.id for optin in settings.optin_roles}
        optin_roles = {role for role in roles if role.id in optin_ids}
        removed = optin_roles & set(ctx.author.roles)
        dont_have = optin_roles - removed
        await ctx.author.remove_roles(*removed)

        responses = []
        if removed:
            responses.append('You have opted out of {}.'.format(format.list(role.name for role in removed)))
        if dont_have:
            responses.append('BAKA! You have not opted into {}!'.format(format.list(role.name for role in dont_have)))
        if len(optin_roles) < len(roles):
            responses.append('BAKA! {}!'.format(format.list((role.name for role in set(roles) - optin_roles), ' is not an optin', ' are not optins')))
        await ctx.send('\n'.join(responses)) # One or more of the above is always true
项目:calebj-cogs    作者:calebj    | 项目源码 | 文件源码
def protect_common(self, obj, protect=True):
        if not isinstance(obj, (discord.Member, discord.Role)):
            raise TypeError('Can only pass member or role objects.')
        server = obj.server
        id = ('r' if type(obj) is discord.Role else '') + obj.id

        protected = self.duelists.get(server.id, {}).get("protected", [])
        if protect == (id in protected):
            return False
        elif protect:
            protected.append(id)
        else:
            protected.remove(id)

        if server.id not in self.duelists:
            self.duelists[server.id] = {}
        self.duelists[server.id]['protected'] = protected
        dataIO.save_json(JSON_PATH, self.duelists)
        return True
项目:dogbot    作者:slice    | 项目源码 | 文件源码
def pingrole(self, ctx, role: discord.Role, *, message):
        """
        Temporarily edits a role to be pingable, sends a message mentioning said role, then edits it to be
        unpingable.

        The role must be below my highest role.
        """
        if role.position >= ctx.guild.me.top_role.position:
            return await ctx.send("I can't edit that role because it's above my highest role.")

        try:
            await role.edit(mentionable=True, reason=f'Pingrole by {describe(ctx.author)}')
            await ctx.send(f'{role.mention}: {message}')
        finally:
            try:
                await role.edit(mentionable=False, reason=f'Pingrole by {describe(ctx.author)}')
            except discord.HTTPException:
                pass
项目:dogbot    作者:slice    | 项目源码 | 文件源码
def add(self, ctx: DogbotContext, type: AutoroleType, *roles: discord.Role):
        """Adds autoroles."""
        for role in roles:
            if role.position > ctx.guild.me.top_role.position:
                await ctx.send('I can\'t autorole the role \"{0.name}\". It\'s too high on the role list. Move my '
                               'role above it.'.format(role))
                return

        log.debug('Adding autorole. (type=%s, roles=%s)', type, roles)
        try:
            async with ctx.acquire() as conn:
                await conn.execute('INSERT INTO autoroles (guild_id, type, roles) VALUES ($1, $2, $3)', ctx.guild.id,
                                   type.name, list(map(lambda r: r.id, roles)))
        except asyncpg.UniqueViolationError:
            return await ctx.send('There\'s already autoroles for that type on this server.')
        await ctx.ok()
项目:dogbot    作者:slice    | 项目源码 | 文件源码
def on_member_autorole(self, member: discord.Member, roles_added: 'List[discord.Role]'):
        # make embed
        msg = (f'\N{BOOKMARK} Automatically assigned roles to {describe(member)}' if isinstance(roles_added, list) else
               f'\N{CLOSED BOOK} Failed to automatically assign roles for {describe(member)}')

        if roles_added:
            # if roles were added, add them to the message
            msg += ', added roles: ' + ', '.join(describe(role) for role in roles_added)

            # make sure to add to debounce so we don't spew out "roles updated" messages
            self.autorole_debounces.add(
                role_ids=[role.id for role in roles_added],
                member_id=member.id
            )

        await self.log(member.guild, msg)
项目:Shallus-Bot    作者:cgropp    | 项目源码 | 文件源码
def colour(self, ctx, role: discord.Role, value: discord.Colour):
        """Edits a role's colour

        Use double quotes if the role contains spaces.
        Colour must be in hexadecimal format.
        \"http://www.w3schools.com/colors/colors_picker.asp\"
        Examples:
        !editrole colour \"The Transistor\" #ff0000
        !editrole colour Test #ff9900"""
        author = ctx.message.author
        try:
            await self.bot.edit_role(ctx.message.server, role, color=value)
            logger.info("{}({}) changed the colour of role '{}'".format(
                author.name, author.id, role.name))
            await self.bot.say("Done.")
        except discord.Forbidden:
            await self.bot.say("I need permissions to manage roles first.")
        except Exception as e:
            print(e)
            await self.bot.say("Something went wrong.")
项目:Shallus-Bot    作者:cgropp    | 项目源码 | 文件源码
def edit_role_name(self, ctx, role: discord.Role, name: str):
        """Edits a role's name

        Use double quotes if the role or the name contain spaces.
        Examples:
        !editrole name \"The Transistor\" Test"""
        if name == "":
            await self.bot.say("Name cannot be empty.")
            return
        try:
            author = ctx.message.author
            old_name = role.name  # probably not necessary?
            await self.bot.edit_role(ctx.message.server, role, name=name)
            logger.info("{}({}) changed the name of role '{}' to '{}'".format(
                author.name, author.id, old_name, name))
            await self.bot.say("Done.")
        except discord.Forbidden:
            await self.bot.say("I need permissions to manage roles first.")
        except Exception as e:
            print(e)
            await self.bot.say("Something went wrong.")
项目:TnyBot-Discord    作者:00firestar00    | 项目源码 | 文件源码
def admin(self, ctx):
        """Shows the help information for creating self assigned roles
        """
        role = "role"
        plural = "s"
        if "bias" in ctx.invoked_with:
            role = "bias"
            plural = "es"

        msg = []
        msg.append("```")
        msg.append("To get started, give me permission to manage roles, and delete messages.")
        msg.append("I should have been added with these roles, so just check for a `TnyBot` role.")
        msg.append("")
        msg.append("Next, you need to create a list of {1}{2} members can add.")
        msg.append("Use: {0}set{1} Role=Alias, Role2=Alias2")
        msg.append("Example: {0}set{1} robots=robits, dogs=doge, lions, tigers, bears=beers")
        msg.append("")
        msg.append("If you want to enforce 1 {1} per user, then use {0}setmain{1}")
        msg.append("The Member will be prompted if they want to swap {1}{2}.")
        msg.append("")
        msg.append("Hint: {1}{2}, and main{1}{2} can share the same alias.")
        msg.append("To make it easier to add roles, you can allow @mentions and just mention each role you want to add")
        msg.append("```")
        await self.bot.say("\n".join(msg).format(ctx.prefix, role, plural))
项目:TnyBot-Discord    作者:00firestar00    | 项目源码 | 文件源码
def insert(self, role: Role, alias: str = None, is_primary: int = 0):
        """ Inserts a new role into the table.
            If the alias is not specified, the role name will be used instead
        """
        if not role:  # pragma: no cover
            return
        if alias is None:
            alias = role.name

        server = role.server
        if self.sql_type is SQLType.sqlite:
            return await self._insert_lite(role, server, alias, is_primary)
        else:  # pragma: no cover
            self.cursor.execute(
                self.query(
                    '''INSERT INTO roles VALUES (%(role)s, %(alias)s, %(server)s, %(primary)s)
                        ON CONFLICT(role, server_id)
                        DO UPDATE SET alias = %(alias)s'''),
                {"role": role.id, "alias": alias, "server": server.id, "primary": is_primary})
            self.connection.commit()
项目:bursting-cogs    作者:Repulser    | 项目源码 | 文件源码
def status(self, ctx):
        """Shows the servers settings for welcomer."""
        db = fileIO(self.load, "load")
        if ctx.message.server.id not in db:
            await self.bot.say(":x: **Welcomer has not been configured for this server, use `welcomer channel` first**")
            return
        server = ctx.message.server
        color = ''.join([choice('0123456789ABCDEF') for x in range(6)])
        color = int(color, 16)
        e = discord.Embed(colour=discord.Colour(value=color), description="\n\a")
        role = discord.utils.get(ctx.message.server.roles, id=db[server.id]["botrole"])
        e.set_author(name="Settings for " + server.name, icon_url=server.icon_url)
        e.add_field(name="Welcomer Channel:", value="#" + self.bot.get_channel(db[server.id]["Channel"]).name if self.bot.get_channel(db[server.id]["Channel"]) else None, inline=True)
        e.add_field(name="Join Toggle:", value=db[server.id]["join"], inline=True)
        e.add_field(name="Leave Toggle:", value=db[server.id]["leave"], inline=True)
        e.add_field(name="Bot Role:", value=role.name if role else None)
        e.add_field(name="Bot Role Toggle:", value=db[server.id]["botroletoggle"])  
        e.add_field(name="Embed", value=db[server.id]["Embed"], inline=True)
        e.add_field(name="Leave Message:", value=db[server.id]["leavemessage"], inline=False)
        e.add_field(name="Join Message:", value=db[server.id]["joinmessage"], inline=False)
        try:
            await self.bot.say(embed=e)
        except Exception as e:
            await self.bot.say(e)
项目:snake    作者:AnonymousDapper    | 项目源码 | 文件源码
def blacklist_add(self, ctx, value:str, *, obj:MultiMention):
        if isinstance(obj, discord.Server):
            kwargs = dict(server_id=int(obj.id))

        elif isinstance(obj, discord.Channel):
            kwargs = dict(channel_id=int(obj.id))

        elif isinstance(obj, discord.Role):
            kwargs = dict(role_id=int(obj.id))

        elif isinstance(obj, discord.Member):
            kwargs = dict(user_id=int(obj.id))


        with self.bot.db_scope() as session:
            blacklist_obj = session.query(sql.Blacklist).filter_by(**kwargs, data=value).first()
            if blacklist_obj is not None:
                await self.bot.say(f"{obj.__class__.__name__} **{str(obj)}** has already been blacklisted for `{value}`")
                return
            else:
                blacklist_obj = sql.Blacklist(**kwargs, data=value)
                session.add(blacklist_obj)
                await self.bot.say(f"Blacklisted {obj.__class__.__name__} **{str(obj)}** for `{value}`")
项目:snake    作者:AnonymousDapper    | 项目源码 | 文件源码
def blacklist_remove(self, ctx, value:str, *, obj:MultiMention):
        if isinstance(obj, discord.Server):
            kwargs = dict(server_id=int(obj.id))

        elif isinstance(obj, discord.Channel):
            kwargs = dict(channel_id=int(obj.id))

        elif isinstance(obj, discord.Role):
            kwargs = dict(role_id=int(obj.id))

        elif isinstance(obj, discord.Member):
            kwargs = dict(user_id=int(obj.id))


        with self.bot.db_scope() as session:
            blacklist_obj = session.query(sql.Blacklist).filter_by(**kwargs, data=value).first()
            if blacklist_obj is None:
                await self.bot.say(f"{obj.__class__.__name__} **{str(obj)}** is not blacklisted for `{value}`")
                return
            else:
                session.delete(blacklist_obj)
                await self.bot.say(f"Removed {obj.__class__.__name__} **{str(obj)}** from blacklist for `{value}`")
项目:snake    作者:AnonymousDapper    | 项目源码 | 文件源码
def blacklist_search(self, ctx, *, obj:MultiMention):
        if isinstance(obj, discord.Server):
            kwargs = dict(server_id=int(obj.id))

        elif isinstance(obj, discord.Channel):
            kwargs = dict(channel_id=int(obj.id))

        elif isinstance(obj, discord.Role):
            kwargs = dict(role_id=int(obj.id))

        elif isinstance(obj, discord.Member):
            kwargs = dict(user_id=int(obj.id))

        with self.bot.db_scope() as session:
            blacklist_objs = session.query(sql.Blacklist).filter_by(**kwargs).all()

            if len(blacklist_objs) > 0:
                result_text = f"```md\n# {obj.__class__.__name__} {str(obj)} is blacklisted for\n" + "\n".join(f"- {b_obj.data}" for b_obj in blacklist_objs) + "\n```"
            else:
                result_text = f"```md\n# {obj.__class__.__name__} {str(obj)} is not blacklisted\n```"

        await self.bot.say(result_text)
项目:LunaBot    作者:miraai    | 项目源码 | 文件源码
def colour(self, ctx, colour : discord.Colour, *, role : discord.Role):
        """Changes the colour of a role.

        The colour must be a hexadecimal value, e.g. FF2AEF. Don't prefix it
        with a pound (#) as it won't work. Colour names are also not supported.

        To use this command you must have the Manage Roles permission or
        have the Bot Admin role. The bot must also have Manage Roles permissions.

        This command cannot be used in a private message.
        """
        try:
            await self.bot.edit_role(ctx.message.server, role, colour=colour)
        except discord.Forbidden:
            await self.bot.say('The bot must have Manage Roles permissions to use this.')
        else:
            await self.bot.say('\U0001f44c')

    #changes the name of the role
    #NOTE: Currently CANNOT change default bot role name (BotName=DafaultRoleName)
项目:GAFBot    作者:DiNitride    | 项目源码 | 文件源码
def add(self, ctx, *, role: CustomRoleConverter):
        """
        Adds a role to the list that can be assigned
        """
        if not isinstance(role, discord.Role):
            await ctx.send("Could not find role! Creating blank role now :crayon: ")
            role = await ctx.guild.create_role(name=role,
                                               colour=Colour.orange(),
                                               mentionable=True,
                                               reason="Role automagically created by GAF Bot for the role list")
        if role.position >= ctx.author.top_role.position:
            await ctx.send("Unable to add role due to Hierarchy")
        else:
            guild_config = await self.bot.get_guild_config(ctx.guild.id)
            guild_config["roles"][role.id] = role.name
            await self.bot.set_guild_config(ctx.guild.id, guild_config)
            await ctx.send("Added {} to the role list".format(role.name))
项目:Maselkov-Cogs    作者:Maselkov    | 项目源码 | 文件源码
def bouncerset_roles(self, ctx, before_after: str, role: discord.Role=None):
        """For first parameter use before or after. For roles with space with them,
        use \"double quotes\"

        Before: role assigned to users when they join the server but don't accept
        the rules yet, will be stripped after accepting the rules. Can be left empty.

        After: Role assigned after accepting the rules
        """
        server = ctx.message.server
        valid_options = ["before", "after"]
        selection = before_after.lower()
        if selection not in valid_options:
            await send_cmd_help(ctx)
            return
        if selection == "before":
            await self.bot.say("Role assigned at join will be: {}".format(role))
            self.settings[server.id]["role_before"] = role.id
        elif role is not None:
            await self.bot.say("Role assigned after accepting rules will be: {}".format(role))
            self.settings[server.id]["role_after"] = role.id
        else:
            self.bot.say("After role can't be empty")
            return
        dataIO.save_json('data/bouncer/settings.json', self.settings)
项目:Addon    作者:GitAcrown    | 项目源码 | 文件源码
def set(self, ctx, role: discord.Role):
        """Change le rôle de président enregistré."""
        channel = ctx.message.channel
        author = ctx.message.author
        if self.sys["GEP_ROLE"] is None:
            self.sys["GEP_ROLE"] = role.name
            fileIO("data/extra/sys.json", "save", self.sys)
            await self.bot.say("Rôle de président enregistré.")
        else:
            await self.bot.say(
                "Le rôle {} est déja renseigné. Voulez-vous l'enlever ? (O/N)".format(self.sys["GEP_ROLE"]))
            rep = await self.bot.wait_for_message(author=author, channel=channel)
            rep = rep.content.lower()
            if rep == "o":
                await self.bot.say("Le rôle à été retiré de ma BDD.")
                self.sys["GEP_ROLE"] = None
                fileIO("data/extra/sys.json", "save", self.sys)
            elif rep == "n":
                await self.bot.say("Le rôle est conservé.")
            else:
                await self.bot.say("Réponse invalide, le rôle est conservé.")

                # BOITE A IDEES --------------------
项目:KeekoBot    作者:DavidNeon    | 项目源码 | 文件源码
def colour(self, ctx, role: discord.Role, value: discord.Colour):
        """Edits a role's colour

        Use double quotes if the role contains spaces.
        Colour must be in hexadecimal format.
        \"http://www.w3schools.com/colors/colors_picker.asp\"
        Examples:
        !editrole colour \"The Transistor\" #ff0000
        !editrole colour Test #ff9900"""
        author = ctx.message.author
        try:
            await self.bot.edit_role(ctx.message.server, role, color=value)
            logger.info("{}({}) changed the colour of role '{}'".format(
                author.name, author.id, role.name))
            await self.bot.say("Done.")
        except discord.Forbidden:
            await self.bot.say("I need permissions to manage roles first.")
        except Exception as e:
            print(e)
            await self.bot.say("Something went wrong.")
项目:KeekoBot    作者:DavidNeon    | 项目源码 | 文件源码
def edit_role_name(self, ctx, role: discord.Role, name: str):
        """Edits a role's name

        Use double quotes if the role or the name contain spaces.
        Examples:
        !editrole name \"The Transistor\" Test"""
        if name == "":
            await self.bot.say("Name cannot be empty.")
            return
        try:
            author = ctx.message.author
            old_name = role.name  # probably not necessary?
            await self.bot.edit_role(ctx.message.server, role, name=name)
            logger.info("{}({}) changed the name of role '{}' to '{}'".format(
                author.name, author.id, old_name, name))
            await self.bot.say("Done.")
        except discord.Forbidden:
            await self.bot.say("I need permissions to manage roles first.")
        except Exception as e:
            print(e)
            await self.bot.say("Something went wrong.")
项目:KeekoBot    作者:DavidNeon    | 项目源码 | 文件源码
def is_admin_or_superior(self, obj):
        if isinstance(obj, discord.Message):
            user = obj.author
        elif isinstance(obj, discord.Member):
            user = obj
        elif isinstance(obj, discord.Role):
            pass
        else:
            raise TypeError('Only messages, members or roles may be passed')

        server = obj.server
        admin_role = settings.get_server_admin(server)

        if isinstance(obj, discord.Role):
            return obj.name == admin_role

        if user.id == settings.owner:
            return True
        elif discord.utils.get(user.roles, name=admin_role):
            return True
        else:
            return False
项目:KeekoBot    作者:DavidNeon    | 项目源码 | 文件源码
def is_mod_or_superior(self, obj):
        if isinstance(obj, discord.Message):
            user = obj.author
        elif isinstance(obj, discord.Member):
            user = obj
        elif isinstance(obj, discord.Role):
            pass
        else:
            raise TypeError('Only messages, members or roles may be passed')

        server = obj.server
        admin_role = settings.get_server_admin(server)
        mod_role = settings.get_server_mod(server)

        if isinstance(obj, discord.Role):
            return obj.name in [admin_role, mod_role]

        if user.id == settings.owner:
            return True
        elif discord.utils.get(user.roles, name=admin_role):
            return True
        elif discord.utils.get(user.roles, name=mod_role):
            return True
        else:
            return False
项目:KeekoBot    作者:DavidNeon    | 项目源码 | 文件源码
def uniquegroup(self, ctx, role: discord.Role, groupid: int):
        """Set a role to a unique group ID,
        This means that a user cannot have more then one role from the same group.

        Any role sharing the same ID will be considered a group.
        GroupID 0 will not be considered unique and can share other roles."""
        server = ctx.message.server
        if role.id not in self.settings_dict[server.id]['roles']:
            await self.bot.say('This role ins\'t in the buyrole list')
        elif groupid < 0:
            await self.bot.say('The group ID cannot be negative.')
        else:
            # Set the uniquegroup ID here, logic will remain in a subfunction of buyrole
            self.settings_dict[server.id]['roles'][role.id]['uniquegroup'] = groupid
            self.save_json()
            if groupid == 0:
                await self.bot.say('Unique Group ID set. {} isn\'t considered unique.'.format(role.name))
            else:
                await self.bot.say('Unique Group ID set. {} will now be unique in group ID {}'.format(role.name, groupid))
项目:KeekoBot    作者:DavidNeon    | 项目源码 | 文件源码
def _create_list(self, server):  # A credit to calebj#7377 for helping me out here.
        """Creates the role list for a server"""
        if 'colour' not in self.settings_dict[server.id]:  # Backwards compatibility. *Sigh*
            colour = 0x72198b
        else:
            colour = self.settings_dict[server.id]['colour']
        embed = discord.Embed(description='**Role list:**', colour=colour)
        for roleid, roledata in self.settings_dict[server.id]['roles'].items():
            role = discord.utils.get(server.roles, id=roleid)
            if not role:
                continue
            if roledata['uniquegroup'] > 0:
                embed.add_field(name='%s (Unique, ID #%s)' % (role.name, roledata['uniquegroup']), value=self._price_string(roledata['price'], True))
            else:
                embed.add_field(name=role.name, value=self._price_string(roledata['price'], True))
        return embed
项目:dango.py    作者:khazhyk    | 项目源码 | 文件源码
def roleinfo(self, ctx, *, role: discord.Role):
        """Displays information about a role."""
        rd = InfoBuilder([
            ('Name', role.name),
            ('ID', role.id),
            ('Members', sum(1 for member in role.guild.members if role in member.roles)),
            ('Created', role.created_at),
            ('Managed', role.managed),
            ('Position', role.position),
            ('Permissions', role.permissions.value),
            ('Color', "#{:06x}".format(role.color.value)),
            ('Hoist', role.hoist),
            ('Mentionable', role.mentionable)
        ])

        await ctx.send(rd.code_block())
项目:Cassandra    作者:Avinch    | 项目源码 | 文件源码
def add_(self, ctx, role: Role):
        """
        Adds a role.
        Available Roles List:
            - ping
            - battlenet
        If the argument given is not a valid role in the guild, it will safely ignore it.
        If the argument is a valid role in the guild and is not in the available roles list, it will flag it as invalid.
        """
        whitelisted_roles = [
            utils.get(ctx.guild.roles, name="ping"),
            utils.get(ctx.guild.roles, name="battlenet")
        ]
        if role in whitelisted_roles:
            await ctx.author.add_roles(role, reason="Used role command")
            await ctx.send(f"Added `{role}` to {ctx.author.mention}.")
        else:
            await ctx.send(
                f"""
                `{role}` is not a valid role! 
                Do `{ctx.prefix}help role add` for more information. {ctx.author.mention}
                """
            )
项目:Cassandra    作者:Avinch    | 项目源码 | 文件源码
def remove_(self, ctx, role: Role):
        """
        Removes a role.
        Available Roles List:
            - ping
            - battlenet
        If the argument given is not a valid role in the guild, it will safely ignore it.
        If the argument is a valid role in the guild and is not in the available roles list, it will flag it as invalid.
        """
        whitelisted_roles = [
            utils.get(ctx.guild.roles, name="ping"),
            utils.get(ctx.guild.roles, name="battlenet")
        ]
        if role in whitelisted_roles:
            await ctx.author.remove_roles(role, reason="Used role command")
            await ctx.send(f"Removed `{role}` from {ctx.author.mention}.")
        else:
            await ctx.send(
                f"""
                `{role}` is not a valid role! 
                Do `{ctx.prefix}help role remove` for more information. {ctx.author.mention}
                """
            )
项目:refactored-cogs    作者:Kowlin    | 项目源码 | 文件源码
def uniquegroup(self, ctx, role: discord.Role, groupid: int):
        """Set a role to a unique group ID,
        This means that a user cannot have more then one role from the same group.

        Any role sharing the same ID will be considered a group.
        GroupID 0 will not be considered unique and can share other roles."""
        server = ctx.message.server
        if role.id not in self.settings_dict[server.id]['roles']:
            await self.bot.say('This role ins\'t in the buyrole list')
        elif groupid < 0:
            await self.bot.say('The group ID cannot be negative.')
        else:
            # Set the uniquegroup ID here, logic will remain in a subfunction of buyrole
            self.settings_dict[server.id]['roles'][role.id]['uniquegroup'] = groupid
            self.save_json()
            if groupid == 0:
                await self.bot.say('Unique Group ID set. {} isn\'t considered unique.'.format(role.name))
            else:
                await self.bot.say('Unique Group ID set. {} will now be unique in group ID {}'.format(role.name, groupid))
项目:refactored-cogs    作者:Kowlin    | 项目源码 | 文件源码
def _create_list(self, server):  # A credit to calebj#7377 for helping me out here.
        """Creates the role list for a server"""
        if 'colour' not in self.settings_dict[server.id]:  # Backwards compatibility. *Sigh*
            colour = 0x72198b
        else:
            colour = self.settings_dict[server.id]['colour']
        embed = discord.Embed(description='**Role list:**', colour=colour)
        for roleid, roledata in self.settings_dict[server.id]['roles'].items():
            role = discord.utils.get(server.roles, id=roleid)
            if not role:
                continue
            if roledata['uniquegroup'] > 0:
                embed.add_field(name='%s (Unique, ID #%s)' % (role.name, roledata['uniquegroup']), value=self._price_string(roledata['price'], True))
            else:
                embed.add_field(name=role.name, value=self._price_string(roledata['price'], True))
        return embed
项目:tmerc-cogs    作者:tmercswims    | 项目源码 | 文件源码
def _mdm(self, ctx: commands.Context,
                   role: discord.Role, *, message: str):
        """Sends a DM to all Members with the given Role.
        Allows for the following customizations:
        {0} is the member being messaged.
        {1} is the role they are being message through.
        {2} is the person sending the message.
        """

        server = ctx.message.server
        sender = ctx.message.author

        try:
            await self.bot.delete_message(ctx.message)
        except:
            pass

        dm_these = self._get_users_with_role(server, role)

        for user in dm_these:
            try:
                await self.bot.send_message(user,
                                            message.format(user, role, sender))
            except (discord.Forbidden, discord.HTTPException):
                continue
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _status_lottery(self, ctx):
        """Check if a lottery is active"""
        author = ctx.message.author
        settings = self.check_server_settings(author.server)
        if settings["Config"]["Active"]:
            ld = settings["Config"]["Current Loadout"]
            timer = settings["Loadouts"][ld]["Timer"]

            if timer == 0:
                remaining = "no time limit"
            else:
                counter = settings["Config"]["Tracker"]
                seconds = timer - (datetime.utcnow() - parser.parse(counter)).seconds
                remaining = "{} remaining".format(self.time_formatter(seconds))

            winners = settings["Loadouts"][ld]["Winners"]
            entry_limit = settings["Loadouts"][ld]["Limit"]
            dos = settings["Loadouts"][ld]["DOS"]
            role_req = settings["Loadouts"][ld]["Role"]
            prize = settings["Loadouts"][ld]["Prize"]
            footer = "There are currently {} users in the lottery.".format(len(settings["Players"]))

            if author.id in settings["Players"]:
                desc = "You are currently in the lottery."
            else:
                desc = "You have **not** entered into this lottery yet."

            embed = discord.Embed(title="Loadout {}".format(ld), description=desc, color=0x50bdfe)
            embed.set_author(name="Lottery System 3.0")
            embed.add_field(name="Prize", value=prize, inline=True)
            embed.add_field(name="Possible Winners", value=winners, inline=True)
            embed.add_field(name="Role", value=role_req, inline=True)
            embed.add_field(name="Limit", value=entry_limit, inline=True)
            embed.add_field(name="Time Remaining", value=remaining, inline=True)
            embed.add_field(name="Days on Server Required", value=dos, inline=True)
            embed.set_footer(text=footer)
            await self.bot.say(embed=embed)
        else:
            await self.bot.say("There aren't any lotteries running on this server right now.")
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _view_lottery(self, ctx, loadout: int):
        """View the parameters set for a loadout"""

        if loadout not in [0, 1, 2, 3, 4, 5]:
            return await self.bot.say("Invalid loadout. Must be 0-5.")

        server = ctx.message.server
        settings = self.check_server_settings(server)
        loadout = str(loadout)

        if not self.slot_checker(settings, loadout):
            return await self.bot.say("The selected loadout is empty.")

        timer = settings["Loadouts"][loadout]["Timer"]
        if timer == 0:
            time_fmt = "no time limit"
        else:
            time_fmt = self.time_formatter(timer)

        winners = settings["Loadouts"][loadout]["Winners"]
        entry_limit = settings["Loadouts"][loadout]["Limit"]
        dos = settings["Loadouts"][loadout]["DOS"]
        role_req = settings["Loadouts"][loadout]["Role"]
        prize = settings["Loadouts"][loadout]["Prize"]
        start_msg = settings["Loadouts"][loadout]["Start Message"]
        end_msg = settings["Loadouts"][loadout]["End Message"]

        embed = discord.Embed(title="Loadout {}".format(loadout), color=0x50bdfe)
        embed.add_field(name="Prize", value=prize, inline=True)
        embed.add_field(name="Number of Winners", value=winners, inline=True)
        embed.add_field(name="Role", value=role_req, inline=True)
        embed.add_field(name="Entry Limit", value=entry_limit, inline=True)
        embed.add_field(name="Timer", value=time_fmt, inline=True)
        embed.add_field(name="Days on Server Required", value=dos, inline=True)
        embed.add_field(name="Start Message", value=start_msg, inline=True)
        embed.add_field(name="End Message", value=end_msg, inline=True)
        await self.bot.say(embed=embed)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _addrole_shop(self, ctx, quantity: int, cost: int, role: discord.Role):
        """Add a role token to shop list. Requires buyrole from refactored cogs"""
        server = ctx.message.server
        settings = self.check_server_settings(server)
        shop_name = settings["Config"]["Shop Name"]
        if 'Buyrole' not in self.bot.cogs:
            msg = ("This feature requires the buyrole cog from the Refactored Cogs repo.\n"
                   "Load buyrole to use this function.")
        else:
            self.shop_item_add(settings, role, cost, quantity, role=True)
            item_count = len(settings["Shop List"])
            msg = ("```{} {} have been added to {} shop.\n{} item(s) available for purchase in the "
                   "store.```".format(quantity, role.name, shop_name, item_count))
        await self.bot.say(msg)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def _role_sethop(self, ctx, *, rolename: str):
        """Set the server role that will receive pending notifications"""
        server = ctx.message.server
        settings = self.check_server_settings(server)
        server_roles = [x.name for x in server.roles]
        if rolename in server_roles:
            settings["Config"]["Shop Role"] = rolename
            dataIO.save_json(self.file_path, self.system)
            msg = ("Notify role set to {}. Server users assigned this role will be notifed when "
                   "an item is redeemed.".format(rolename))
        else:
            role_output = ", ".join(server_roles).replace("@everyone,", "")
            msg = ("{} is not a role on your server. The current roles on your server are:\n"
                   "```{}```".format(rolename, role_output))
        await self.bot.say(msg)
项目:Jumper-Cogs    作者:Redjumpman    | 项目源码 | 文件源码
def user_add_item(self, settings, user, quantity, itemname):
        user_path = settings["Users"][user.id]["Inventory"]
        if itemname in settings["Users"][user.id]["Inventory"]:
            user_path[itemname]["Item Quantity"] += quantity
        else:
            user_path[itemname] = {"Item Name": itemname, "Item Quantity": quantity}
        if "Role" in settings["Shop List"][itemname]:
            user_path[itemname]["Role"] = settings["Shop List"][itemname]["Role"]
        dataIO.save_json(self.file_path, self.system)
项目:CorpBot.py    作者:corpnewt    | 项目源码 | 文件源码
def setkillrole(self, ctx, role : discord.Role = None):
        """Sets the required role ID to add/remove hacks (admin only)."""

        channel = ctx.message.channel
        author  = ctx.message.author
        server  = ctx.message.server

        # Check if we're suppressing @here and @everyone mentions
        if self.settings.getServerStat(ctx.message.server, "SuppressMentions").lower() == "yes":
            suppress = True
        else:
            suppress = False

        isAdmin = author.permissions_in(channel).administrator
        # Only allow admins to change server stats
        if not isAdmin:
            await self.bot.send_message(channel, 'You do not have sufficient privileges to access this command.')
            return

        if role == None:
            self.settings.setServerStat(server, "RequiredKillRole", "")
            msg = 'Kill/resurrect now *admin-only*.'
            await self.bot.send_message(channel, msg)
            return

        if type(role) is str:
            try:
                role = discord.utils.get(server.roles, name=role)
            except:
                print("That role does not exist")
                return

        # If we made it this far - then we can add it
        self.settings.setServerStat(server, "RequiredKillRole", role.id)

        msg = 'Role required for kill/resurrect set to **{}**.'.format(role.name)
        # Check for suppress
        if suppress:
            msg = Nullify.clean(msg)
        await self.bot.send_message(channel, msg)
项目:CorpBot.py    作者:corpnewt    | 项目源码 | 文件源码
def killrole(self, ctx):
        """Lists the required role to kill/resurrect the bot."""

        # Check if we're suppressing @here and @everyone mentions
        if self.settings.getServerStat(ctx.message.server, "SuppressMentions").lower() == "yes":
            suppress = True
        else:
            suppress = False

        role = self.settings.getServerStat(ctx.message.server, "RequiredKillRole")
        if role == None or role == "":
            msg = '**Only Admins** can kill/ressurect the bot.'.format(ctx)
            await self.bot.say(msg)
        else:
            # Role is set - let's get its name
            found = False
            for arole in ctx.message.server.roles:
                if arole.id == role:
                    found = True
                    msg = 'You need to be a/an **{}** to kill/ressurect the bot.'.format(arole.name)
            if not found:
                msg = 'There is no role that matches id: `{}` - consider updating this setting.'.format(role)
            # Check for suppress
            if suppress:
                msg = Nullify.clean(msg)
            await self.bot.send_message(ctx.message.channel, msg)
项目:PTSCogs    作者:PlanetTeamSpeakk    | 项目源码 | 文件源码
def add(self, ctx, name, *, role:discord.Role):
        """Adds a role to the list of giveme's, if the name contains spaces put it in quotes (").
        Example:
        [p]giveme add "role name" role_mention OR
        [p]giveme add "role name" name of the role"""
        if not ctx.message.server.me.permissions_in(ctx.message.channel).manage_roles:
            await self.bot.say("I do not have the manage roles permission here, I cannot assign roles to people untill I do.")
        else:
            if ctx.message.server.id not in self.settings:
                self.settings[ctx.message.server.id] = {'givemes': {}}
            self.settings[ctx.message.server.id]['givemes'][name] = role.id
            self.save_settings()
            await self.bot.say("Giveme has been added.")
项目:PTSCogs    作者:PlanetTeamSpeakk    | 项目源码 | 文件源码
def autorole(self, ctx):
        """Manage autorole settings."""
        if ctx.message.server.id not in self.settings:
            self.settings[ctx.message.server.id] = {'role': None, 'toggled': False}
            self.save_settings()
        if not ctx.invoked_subcommand:
            role = discord.utils.get(ctx.message.server.roles, id=self.settings[ctx.message.server.id]['role'])
            if role == None:
                role = "DELETED"
            else:
                role = role.name
            await self.bot.send_cmd_help(ctx)
            await self.bot.say("```Role: {}\nEnabled: {}```".format(role, self.settings[ctx.message.server.id]['toggled']))
项目:PTSCogs    作者:PlanetTeamSpeakk    | 项目源码 | 文件源码
def setrole(self, ctx, *, role:discord.Role):
        """Set the role the bot should assign on join, the highest role that the bot has should be higher than this one."""
        self.settings[ctx.message.server.id]['role'] = role.id
        self.save_settings()
        await self.bot.say("Role has been set.")