Exemplo n.º 1
0
    async def ban_filter(
        self, guild: discord.Guild, mod: UserLike, target: discord.abc.Snowflake
    ) -> bool:
        """
        Determines if the specified user can ban another specified user in a guild

        Parameters
        ----------
        guild: discord.Guild
        mod: discord.User
        target: discord.User

        Returns
        -------
        bool
        """
        # TODO: rewrite more maintainibly.
        is_owner: bool = await self.bot.is_owner(mod)

        mod_member = guild.get_member(mod.id)
        if mod_member is None and not is_owner:
            return False

        # noted below lines contain a superflous condition covered above to help mypy

        can_ban: bool = guild.me.guild_permissions.ban_members
        if not is_owner and mod_member is not None:  # note
            can_ban &= mod_member.guild_permissions.ban_members

        target_m = guild.get_member(target.id)
        if target_m is not None and mod_member is not None:  # note
            can_ban &= guild.me.top_role > target_m.top_role or guild.me == guild.owner
            can_ban &= target_m != guild.owner
            if not is_owner:
                can_ban &= (
                    mod_member.top_role > target_m.top_role or mod_member == guild.owner
                )
        return can_ban
Exemplo n.º 2
0
    async def assign_moderator(self, guild: discord.Guild, ticket: Dict,
                               moderator: discord.Member):
        channel = guild.get_channel(ticket["channel"])
        author = guild.get_member(ticket["user"])
        if channel:
            await channel.send(
                f"{moderator.mention} has been assigned as the Moderator for this ticket."
            )

        async with self.config.guild(guild).created() as created:
            for index, i_ticket in enumerate(created[str(author.id)]):
                if i_ticket["channel"] == ticket["channel"]:
                    created[str(author.id)][index]["assigned"] = moderator.id
                    break
Exemplo n.º 3
0
async def revoke_moderation(guild: Guild, moderation: Moderation):
    if moderation.expiration_date > datetime.utcnow():
        await utils.sleep_until(moderation.expiration_date)

    map_moderations = {ModerationType.MUTE: unmute, ModerationType.BAN: unban}

    member = guild.get_member(moderation.user_id)
    func = map_moderations.get(moderation.type)

    if func:
        await func(guild, member, moderation.user_id)
        db = session_factory()
        crud.revoke_moderation(moderation, db)
        db.close()
Exemplo n.º 4
0
async def get_members(user_ids: Iterable[int],
                      guild: discord.Guild) -> List[discord.Member]:
    unfound_ids = []
    users = []
    for _uid in user_ids:
        uid = int(_uid)
        u = guild.get_member(uid)
        if u is not None:
            users.append(u)
        else:
            unfound_ids.append(uid)
    if unfound_ids != []:
        users += await guild.query_members(limit=None, user_ids=unfound_ids)
    return users
Exemplo n.º 5
0
def format_user_keys(server: discord.Guild, keys: dict):
    key_strings = []
    for user_id in keys:
        if not keys[user_id]:
            continue

        user_name = server.get_member(user_id).display_name
        user_header = f'{user_name}\'s keystones:\n'
        user_keys = '\n'.join([
            f'   {character}: '
            f'{dungeon_utils.get_dungeon_name(dungeon)} {level}'
            for character, dungeon, level in keys[user_id]
        ])
        key_strings.append(user_header + user_keys)

    if key_strings:
        return '\n'.join(key_strings)
    elif len(keys) == 1:
        user_id = next(iter(keys))
        user_name = server.get_member(user_id).display_name
        return f'There are no keystones for {user_name}.'
    else:
        return 'There are no keystones for the mentioned users.'
    def _generate_event_embed(guild: Guild, disciplined_user: Union[User,
                                                                    Member],
                              event: dict):
        """
        Generates the embed object that lists the details of the given event.

        :param guild: the guild of the user event
        :param disciplined_user: the user that was disciplined
        :param event: the discipline event to resolve to an embed
        :return: the resultant embed that details the given event
        """
        discipline_type = event['discipline_type']
        output_embed = Embed(title='Event {} Details'.format(event['id']),
                             description='{} for user {}'.format(
                                 discipline_type['discipline_name'],
                                 str(disciplined_user)))
        output_embed.add_field(name='Disciplined User:'******'{}({})'.format(discipline_type['discipline_name'],
                                         discipline_type['id'])
        if event['discipline_content'] is not None and len(
                event['discipline_content']) > 0:
            discipline_str += ' [{}]'.format(event['discipline_content'])
        output_embed.add_field(name='Discipline Type',
                               value=discipline_str,
                               inline=False)
        moderator_user = guild.get_member(event['moderator_user_snowflake'])
        output_embed.add_field(name='Moderator',
                               value=str(moderator_user),
                               inline=False)
        output_embed.add_field(name='Reason',
                               value=event['reason_for_discipline'],
                               inline=False)
        output_embed.add_field(name='Start Time',
                               value=event['discipline_start_date_time'],
                               inline=False)
        if 'discipline_end_date_time' in event and event[
                'discipline_end_date_time'] is not None:
            output_embed.add_field(name='End Time',
                                   value=event['discipline_end_date_time'],
                                   inline=False)
        output_embed.add_field(name='Is Terminated?',
                               value='Yes' if event['is_terminated'] else 'No',
                               inline=False)
        output_embed.add_field(name='Is Pardoned?',
                               value='Yes' if event['is_pardoned'] else 'No',
                               inline=False)
        return output_embed
async def if_time_remove_role(member_set: typing.Set[typing.Tuple[int,
                                                                  datetime]],
                              guild: discord.Guild, role: discord.Role):
    looping_set = member_set.copy()
    for member_datetime in looping_set:
        member = guild.get_member(member_datetime[0])
        if member:
            if member_datetime[1] <= datetime.utcnow():
                await member.remove_roles(role, reason="Time expired.")
                member_set.remove(member_datetime)
                logger.debug("Removed user {} from set.".format(
                    member.display_name))
        else:
            member_set.remove(member_datetime)
            logger.debug("Removed invalid user from set.")
Exemplo n.º 8
0
 async def info_command(self, ctx):
     await ctx.send(
         embed=Embed(
             title=':card_box: Informacje',
             color=0x66ffcc,
         ).add_field(
             name='**Nazwa**', value=self.bot.user.name, inline=False).
         add_field(name='**Wersja**', value=self.bot.version,
                   inline=False).add_field(name='**Oprogramowanie**',
                                           value=f'Discord.py v{version}',
                                           inline=False).
         add_field(name='**Autor bota**',
                   value=Guild.get_member(ctx.guild, 309270832683679745),
                   inline=False))
     return
Exemplo n.º 9
0
    async def alert(self, ctx, title="Titulo", description="Descrição"):
        await ctx.message.delete()

        await ctx.send("Hey @everyone!")

        user = Guild.get_member(ctx.message.author.guild,
                                user_id=ctx.message.author.id)

        embed = Embed()
        embed.title = title
        embed.description = description
        embed.colour = Colour.gold()
        embed.set_author(name=ctx.message.author.name,
                         icon_url=user.avatar_url)
        await ctx.send(embed=embed)
Exemplo n.º 10
0
    def get_user_and_intensity(self, guild: discord.Guild, target: str):
        target = target.strip()
        user = None
        intensity = 1
        # mentions can be <@! or <@
        user_ment = mention.match(target)
        user_ment_b = mention_bang.match(target)

        # try with no intensity specified and not a mention
        if not user_ment or not user_ment_b:
            user = guild.get_member_named(target)

        # has intensity, could be a mention/text
        if not user:
            try:
                args = target.split()
                intensity = int(args[-1])
                name = " ".join(args[:-1])
                # not a mention
                user = guild.get_member_named(name)
                # parse mention
                if not user:
                    user_ment = mention.match(name)
                    user_ment_b = mention_bang.match(name)
            except:
                pass

        if not user:
            if user_ment:
                user = guild.get_member(int(user_ment.group(1)))
            elif user_ment_b:
                user = guild.get_member(int(user_ment_b.group(1)))
            else:
                user = None

        return user, intensity
Exemplo n.º 11
0
    async def _get_diff(guild: Guild) -> _Diff:
        """Return the difference of users between the cache of `guild` and the database."""
        log.trace("Getting the diff for users.")

        users_to_create = []
        users_to_update = []
        seen_guild_users = set()

        async for db_user in UserSyncer._get_users():
            # Store user fields which are to be updated.
            updated_fields = {}

            def maybe_update(db_field: str, guild_value: t.Union[str,
                                                                 int]) -> None:
                # Equalize DB user and guild user attributes.
                if db_user[db_field] != guild_value:
                    updated_fields[db_field] = guild_value

            if guild_user := guild.get_member(db_user["id"]):
                seen_guild_users.add(guild_user.id)

                maybe_update("name", guild_user.name)
                maybe_update("discriminator", int(guild_user.discriminator))
                maybe_update("in_guild", True)

                guild_roles = [role.id for role in guild_user.roles]
                if set(db_user["roles"]) != set(guild_roles):
                    updated_fields["roles"] = guild_roles

            elif db_user["in_guild"]:
                # The user is known in the DB but not the guild, and the
                # DB currently specifies that the user is a member of the guild.
                # This means that the user has left since the last sync.
                # Update the `in_guild` attribute of the user on the site
                # to signify that the user left.
                updated_fields["in_guild"] = False
Exemplo n.º 12
0
async def _get_guild_timezones(db: Database,
                               guild: discord.Guild) -> set[TimezoneType]:
    """
    Get all user timezones from the given guild.

    :param db: the Database to get user data from
    :param guild: the guild to limit the search to
    :return: a set of user timezones in this guild
    """
    all_timezones = await db.get_all_timezones()
    return {
        # Filter out timezones of users outside this guild
        tz
        for user_id, tz in all_timezones if guild.get_member(user_id)
    }
Exemplo n.º 13
0
    async def get_leaderboard(self,
                              positions: int = None,
                              guild: discord.Guild = None) -> List[tuple]:
        """Gets the Adventure's leaderboard.

        Parameters
        ----------
        positions : `int`
            The number of positions to get
        guild : discord.Guild
            The guild to get the leaderboard of. If this
            is provided, get only guild members on the leaderboard

        Returns
        -------
        `list` of `tuple`
            The sorted leaderboard in the form of :code:`(user_id, raw_account)`
        """
        raw_accounts = await self.config.all_users()
        if guild is not None:
            tmp = raw_accounts.copy()
            for acc in tmp:
                if not guild.get_member(acc):
                    del raw_accounts[acc]
        raw_accounts_new = {}
        async for (k, v) in AsyncIter(raw_accounts.items(), steps=100):
            user_data = {}
            for item in ["lvl", "rebirths", "set_items"]:
                if item not in v:
                    v.update({item: 0})
            for (vk, vi) in v.items():
                if vk in ["lvl", "rebirths", "set_items"]:
                    user_data.update({vk: vi})

            if user_data:
                user_data = {k: user_data}
            raw_accounts_new.update(user_data)
        sorted_acc = sorted(
            raw_accounts_new.items(),
            key=lambda x:
            (x[1].get("rebirths", 0), x[1].get("lvl", 1), x[1].get(
                "set_items", 0)),
            reverse=True,
        )
        if positions is None:
            return sorted_acc
        else:
            return sorted_acc[:positions]
Exemplo n.º 14
0
    async def buildCache(self, guild: discord.Guild, limit=None, startup=False):
        if limit is None:
            limit = 500 if startup else 50
        GearbotLogging.info(f"Populating modlog with missed messages during downtime for {guild.name} ({guild.id}).")
        newCount = 0
        editCount = 0
        count = 0
        no_access = 0
        fetch_times = []
        processing_times = []
        for channel in guild.text_channels:
            permissions = channel.permissions_for(guild.get_member(self.bot.user.id))
            if permissions.read_messages and permissions.read_message_history:

                async for message in channel.history(limit=limit, oldest_first=False,
                                                     before=self.cache_message if startup else None):
                    processing = time.perf_counter()
                    if not self.running:
                        GearbotLogging.info("Cog unloaded while still building cache, aborting.")
                        return
                    fetch = time.perf_counter()
                    logged = LoggedMessage.get_or_none(messageid=message.id)
                    fetch_times.append(time.perf_counter() - fetch)
                    if logged is None:
                        await MessageUtils.insert_message(self.bot, message, redis=False)
                        newCount = newCount + 1
                    elif message.edited_at is not None:
                        if logged.content != message.content:
                            logged.content = message.content
                            logged.save()
                            editCount = editCount + 1
                    count = count + 1
                    processing_times.append(time.perf_counter() - processing)
                    if count % min(75, int(limit / 2)) is 0:
                        await asyncio.sleep(0)

                await asyncio.sleep(0)
            else:
                no_access += 1
        GearbotLogging.info(
            f"Discovered {newCount} new messages and {editCount} edited in {guild.name} (checked {count})")
        total_fetch_time = sum(fetch_times)
        avg_fetch_time = (total_fetch_time / len(fetch_times)) * 1000
        total_processing = (sum(processing_times)) * 1000
        avg_processing = total_processing / len(processing_times)
        GearbotLogging.info(f"Average fetch time: {avg_fetch_time} (total fetch time: {total_fetch_time})")
        GearbotLogging.info(f"Average processing time: {avg_processing} (total of {total_processing})")
        GearbotLogging.info(f"Was unable to read messages from {no_access} channels")
Exemplo n.º 15
0
    async def send_report(self, ctx: commands.Context, msg: discord.Message,
                          guild: discord.Guild):

        author = guild.get_member(msg.author.id)
        report = msg.clean_content

        channel_id = await self.config.guild(guild).output_channel()
        channel = guild.get_channel(channel_id)
        if channel is None:
            return None

        files: List[discord.File] = await Tunnel.files_from_attach(msg)

        ticket_number = await self.config.guild(guild).next_ticket()
        await self.config.guild(guild).next_ticket.set(ticket_number + 1)

        if await self.bot.embed_requested(channel, author):
            em = discord.Embed(description=report,
                               colour=await ctx.embed_colour())
            em.set_author(
                name=_("Report from {author}{maybe_nick}").format(
                    author=author,
                    maybe_nick=(f" ({author.nick})" if author.nick else "")),
                icon_url=author.avatar_url,
            )
            em.set_footer(text=_("Report #{}").format(ticket_number))
            send_content = None
        else:
            em = None
            send_content = _(
                "Report from {author.mention} (Ticket #{number})").format(
                    author=author, number=ticket_number)
            send_content += "\n" + report

        try:
            await Tunnel.message_forwarder(destination=channel,
                                           content=send_content,
                                           embed=em,
                                           files=files)
        except (discord.Forbidden, discord.HTTPException):
            return None

        await self.config.custom("REPORT", guild.id,
                                 ticket_number).report.set({
                                     "user_id": author.id,
                                     "report": report
                                 })
        return ticket_number
Exemplo n.º 16
0
    async def get_member_pairs(
            self, guild: discord.Guild,
            users) -> AsyncIterator[Tuple[discord.Member, User]]:
        """
        yields:
            discord.Member, User

        Parameters
        ----------
        users: dict
            discord_id -> inat_id mapping
        """

        known_users = []
        uncached_known_user_ids = []
        for discord_id in users:
            user_json = None
            inat_user = None

            discord_member = guild.get_member(discord_id)
            if discord_member and (guild.id
                                   in users[discord_id].get("known_in")
                                   or users[discord_id].get("known_all")):
                inat_user_id = users[discord_id].get("inat_user_id")
                if inat_user_id:
                    if inat_user_id not in self.cog.api.users_cache:
                        uncached_known_user_ids.append(inat_user_id)
                    known_users.append([discord_member, inat_user_id])

        if uncached_known_user_ids:
            try:
                # cache all the remaining known users in one call
                await self.cog.api.get_observers_from_projects(
                    user_ids=uncached_known_user_ids)
            except LookupError:
                pass

        for (discord_member, inat_user_id) in known_users:
            try:
                user_json = await self.cog.api.get_users(inat_user_id)
            except LookupError:
                continue
            if user_json:
                results = user_json["results"]
                if results:
                    inat_user = User.from_dict(results[0])
            if inat_user:
                yield (discord_member, inat_user)
Exemplo n.º 17
0
async def create_ping_embed_from_id(
		id: int,
		guild: discord.Guild,
		cursor: asqlite.Cursor,
		*,
		title_prefix: str = None
	) -> discord.Embed | None:
	"""Creates a "ping info" embed using the ID of a ping.
	"title_prefix" is an optional argument that adds a "prefix" to the title of the resulting embed."""
	# Get the name of the ping
	pingName = await ping_get_name(id, cursor)
	if pingName is None:
		return None
	# Get user names
	pingUserIDs = await ping_get_user_ids_by_id(id, cursor)
	pingUserNames = []
	for user in pingUserIDs:
		member = guild.get_member(user)
		if member is not None:
			pingUserNames.append(member.display_name)
	# Get aliases for the ping
	pingAliases = await ping_get_alias_names(id, cursor)

	# Convert pingUsers and pingAliases to the format we need for the embed
	# This put backticks around each entry in the array
	#pingUserTexts = list(map(lambda n: f"`{n}`", pingUserNames))
	#pingAliasTexts = list(map(lambda n: f"`{n}`", pingAliases))

	if title_prefix is not None:
		# Prefix present
		embedTitle = f"{title_prefix} Ping: {pingName} | Users: {len(pingUserNames)}"
	else:
		embedTitle = f"Ping: {pingName} | Users: {len(pingUserNames)}"

	# Now that we have all of our info, start creating our embed
	embed = discord.Embed(
		colour = PING_EMBED_COLOUR,
		title = embedTitle,
		description = f"```{', '.join(pingUserNames)}```"
	)
	if len(pingAliases) > 0:
		embed.add_field(
			name = "Aliases",
			value = f"```{', '.join(pingAliases)}```",
			inline = True
		)
	# Return the generated embed
	return embed
Exemplo n.º 18
0
 def _resolveUserParam(self, guild: discord.Guild, input: str):
     """
     Given user input, attempts to find the corresponding Member.
     Currently only accepts pings and explicit user IDs.
     """
     UserMention = re.compile(r"<@\!?(\d+)>")
     match = UserMention.match(input)
     if match is not None:
         idcheck = match.group(1)
     else:
         idcheck = input
     try:
         idcheck = int(idcheck)
     except ValueError:
         return None
     return guild.get_member(idcheck)
Exemplo n.º 19
0
async def add_voice(context, guild: discord.Guild):
    member = guild.get_member(context.channel.recipient.id)

    if member is None:
        await context.channel.send(NOT_A_MEMBER)
        return

    if member.joined_at > datetime.now() - timedelta(weeks=2):
        await context.channel.send(ACCESS_DENIED)
        return
    if has_role(member, VOICE):
        await context.channel.send(ACCESS_ALREADY_GRANTED)
        return

    await member.add_roles(get(guild.roles, name=VOICE))
    await context.channel.send(ACCESS_GRANTED)
Exemplo n.º 20
0
    async def __send_match_dm(
        self, member: BirdMember, message: Message, guild: Guild
    ) -> None:
        """Private - send DM message to match. To be replaced with actions queue"""
        try:
            target: Member = guild.get_member(int(member.member_id))
        except ValueError:
            self.logger.error("Invalid member_id to int: '%s'", member.member_id)
            return

        msg = (
            f"ShoulderBird notification, **{message.author.display_name}** "
            f"mentioned you in **{message.channel.name}** saying:\n"
            f"`{message.clean_content}`\n{message.jump_url}"
        )
        await self.__send_dm(target, msg)
Exemplo n.º 21
0
 async def on_guild_join(self, guild: discord.Guild):
     """
     On joining a guild, go through all the channels and locate one that we can talk in, and declare
     that to be my log channel for that guild. Remind guild admins that the log channel can be changed.
     """
     channels = guild.text_channels
     myself = guild.get_member(self.bot.user.id)
     for channel in channels:
         if channel.permissions_for(myself).send_messages:
             print("found new log channel :)")
             await channel.send(f"Hello! I'll use this channel as my log channel. "
                                f"To change this, run `{self.bot.command_prefix}config logs setchannel #channel`.")
             c = self.conn.cursor()
             c.execute("INSERT INTO log_channels(guild_id,log_channel) VALUES(?,?)", (guild.id, channel.id))
             self.conn.commit()
             break  # stop looping through channels, we found one
Exemplo n.º 22
0
    def __init__(self, gdoc_line: List[str], *, guild: discord.Guild):
        self.advenced_info_fetched = False
        try:
            self.discord_id = int(gdoc_line[0])
        except:
            raise InvalidSheet(
                f"discord_id must be a int but it was \"{gdoc_line[0]}\"")
        self.name = gdoc_line[1]
        self.image_url = gdoc_line[2]
        self.age = gdoc_line[3]
        try:
            self.level = int(gdoc_line[4])
        except:
            raise InvalidSheet(
                f"level must be a int but it was \"{gdoc_line[4]}\"")
        try:
            self.xp = int(gdoc_line[5])
        except:
            raise InvalidSheet(
                f"xp must be a int but it was \"{gdoc_line[5]}\"")
        try:
            self.gold = int(gdoc_line[6])
        except:
            raise InvalidSheet(
                f"credits must be a int but it was \"{gdoc_line[6]}\"")
        self.origin = gdoc_line[7]
        self.job = gdoc_line[8]
        try:
            self.force = int(gdoc_line[9])
            self.resis = int(gdoc_line[10])
            self.dexteriry = int(gdoc_line[11])
            self.agility = int(gdoc_line[12])
            self.social = int(gdoc_line[13])
            self.knowledge = int(gdoc_line[14])
            self.magic = int(gdoc_line[15])
        except:
            raise InvalidSheet(f"Stats must be a int")
        self.desc = gdoc_line[16]
        self.note = gdoc_line[17]
        self.gdoc_url = gdoc_line[18]

        self.member = guild.get_member(self.discord_id)  # type: discord.Member
        if not self.member:
            raise NotFound(
                f"member with id {self.discord_id} was not found in the server"
            )
Exemplo n.º 23
0
async def get_member(guild: discord.Guild, argument: str) -> discord.Member:
    match = re.match(r'([0-9]{15,21})$', argument) or re.match(
        r'<@!?([0-9]+)>$', argument)
    if match is None:
        # not a mention...
        if guild:
            result = guild.get_member_named(argument)
        else:
            result = _get_from_guilds(bot, 'get_member_named', argument)
    else:
        user_id = int(match.group(1))
        if guild:
            result = guild.get_member(user_id)
        else:
            result = _get_from_guilds(bot, 'get_member', user_id)

    return result
Exemplo n.º 24
0
    def from_dict(cls, cog: commands.Cog, guild: discord.Guild, name: str,
                  data: dict):
        self = cls.__new__(cls)
        self.cog = cog
        self.config: Config = cog.config
        self.bot: Red = cog.bot
        self.guild: discord.Guild = guild
        self.name: str = name

        self.tagscript = data["tag"]
        self.author_id = author_id = data.get("author_id", data.get("author"))
        self.author = guild.get_member(author_id) if isinstance(
            guild, discord.Guild) else None
        self.uses = data.get("uses", 0)
        self._real_tag: bool = True

        return self
Exemplo n.º 25
0
def find_member(guild: discord.Guild, name, steps=3, mention=True):
    """ Find any member by their name or a formatted mention.
    Steps define the depth at which to search. More steps equal
    less accurate checks.

    +--------+------------------+
    |  step  |     function     |
    +--------+------------------+
    |    0   | perform no check |
    |    1   |   name is equal  |
    |    2   | name starts with |
    |    3   |    name is in    |
    +--------+------------------+

    :param guild: discord.Guild to look through for members.
    :param name: display_name as a string or mention to find.
    :param steps: int from 0-3 to specify search depth.
    :param mention: bool, check for mentions.
    :return: discord.Member
    """
    member = None

    # Return a member from mention
    found_mention = member_mention_pattern.search(name)
    if found_mention and mention:
        member = guild.get_member(int(found_mention.group("id")))
        return member

    name = name.lower()

    # Steps to check, higher values equal more fuzzy checks
    checks = [
        lambda m: m.name.lower() == name or m.display_name.lower() == name,
        lambda m: m.name.lower().startswith(name) or m.display_name.lower(
        ).startswith(name),
        lambda m: name in m.display_name.lower() or name in m.name.lower()
    ]

    for i in range(steps if steps <= len(checks) else len(checks)):
        member = discord.utils.find(checks[i], guild.members)

        if member:
            break

    # Return the found member or None
    return member
Exemplo n.º 26
0
Arquivo: coins.py Projeto: lkmnds/jose
    async def ranks(self, user_id: int, guild: discord.Guild) -> tuple:
        """Get ranking data about a user."""
        all_accounts = await self.all_accounts()

        all_ids = [account['id'] for account in all_accounts]

        guild_ids = [account['id'] for account in all_accounts if
                     guild.get_member(account['id']) is not None]

        try:
            guildrank = guild_ids.index(user_id) + 1
        except ValueError:
            guildrank = -20

        globalrank = all_ids.index(user_id) + 1

        return guildrank, globalrank, len(guild_ids), len(all_ids)
Exemplo n.º 27
0
    async def user(self, ctx: CommandContext, user: User, guild: Guild = None):
        if guild is None and ctx.guild is not None:
            guild: Guild = ctx.guild
        profile: Profile = await user.profile()
        description = ""
        if profile.nitro:
            description += f"i can haz animated emojis since {profile.premium_since}\n"
        if profile.hypesquad:
            description += f"they got some hype\n"
        if profile.partner:
            description += "insrt BLU INFINITY SYMBOL her\n"
        if profile.staff:
            description += "staff. if this is b1nzy, then F**K him for banning selfbots\n"
        mutual: List[Guild] = profile.mutual_guilds

        mutual_text = '\n'.join(guild.name for guild in mutual)
        if len(mutual_text) > 512:
            mutual_text = f"Together in {len(mutual)} guilds. [Truncated]"

        em = Embed(
            title=str(user),
            description=description,
        )
        if guild:
            member: Member = guild.get_member(user.id)
            if member:
                if guild.owner_id == member.id:
                    em.add_field(name="Owner", value="Yeah", inline=True)
                em.colour = member.color
                em.add_field(name="Joined Guild",
                             value=member.joined_at,
                             inline=True)
                em.add_field(name="Permissions",
                             value=dump_perms(member.guild_permissions),
                             inline=True)
        em.set_author(name=user.display_name, icon_url=user.avatar_url)
        em.add_field(name="Mutual guilds", value=mutual_text, inline=True)
        em.add_field(name="Joined Discord", value=user.created_at, inline=True)
        for connection in profile.connected_accounts:
            em.add_field(name=connection['type'],
                         value=('☑' if connection['verified'] else '') +
                         connection['name'],
                         inline=True)
        em.set_thumbnail(url=user.avatar_url)
        await ctx.send(embed=em)
        await ctx.react('✅')
Exemplo n.º 28
0
    def return_member_or_role(
            guild: discord.Guild,
            id: int) -> typing.Union[discord.Member, discord.Role, None]:
        """メンバーか役職オブジェクトを返す関数

        Args:
            guild (discord.guild): discordのguildオブジェクト
            id (int): 役職かメンバーのID

        Returns:
            typing.Union[discord.Member, discord.Role]: discord.Memberかdiscord.Role
        """
        user_or_role = guild.get_role(id)
        if user_or_role is None:
            user_or_role = guild.get_member(id)

        return user_or_role
Exemplo n.º 29
0
 async def give_roles(self,invite:discord.Invite,guild:discord.Guild):
     """Give a role to admins of partners"""
     if isinstance(invite.guild,discord.Guild):
         if guild.id == 356067272730607628 and self.bot.beta:
             return
         roles = await self.bot.cogs['ServerCog'].find_staff(guild.id,'partner_role')
         roles = [x for x in [guild.get_role(int(x)) for x in roles.split(';') if len(x)>0 and x.isnumeric()] if x!=None]
         admins = [x for x in invite.guild.members if x.guild_permissions.administrator]
         for admin in admins:
             if admin in guild.members:
                 member = guild.get_member(admin.id)
                 for role in roles:
                     if role not in member.roles:
                         try:
                             await member.add_roles(role)
                         except:
                             pass
Exemplo n.º 30
0
    async def buildCache(self,
                         guild: discord.Guild,
                         limit=None,
                         startup=False):
        if limit is None:
            limit = 500 if startup else 50
        GearbotLogging.info(
            f"Populating modlog with missed messages during downtime for {guild.name} ({guild.id})."
        )
        newCount = 0
        editCount = 0
        count = 0
        no_access = 0
        for channel in guild.text_channels:
            permissions = channel.permissions_for(
                guild.get_member(self.bot.user.id))
            if permissions.read_messages and permissions.read_message_history:

                async for message in channel.history(
                        limit=limit,
                        oldest_first=False,
                        before=self.cache_message if startup else None):
                    if not self.running:
                        GearbotLogging.info(
                            "Cog unloaded while still building cache, aborting."
                        )
                        return
                    logged = await LoggedMessage.get_or_none(
                        messageid=message.id)
                    if logged is None:
                        await MessageUtils.insert_message(self.bot,
                                                          message,
                                                          redis=False)
                        newCount = newCount + 1
                    elif message.edited_at is not None:
                        if logged.content != message.content:
                            logged.content = message.content
                            await logged.save()
                            editCount = editCount + 1
                    count = count + 1
            else:
                no_access += 1
        GearbotLogging.info(
            f"Discovered {newCount} new messages and {editCount} edited in {guild.name} (checked {count})"
        )
Exemplo n.º 31
0
    async def init_game_channels(
            self, guild: discord.Guild) -> discord.CategoryChannel:
        """ Create the game CategoryChannel and move players into it. Return the CategoryChannel."""
        category = await guild.create_category('The Chameleon')
        await guild.create_text_channel('chameleon-game', category=category)
        gamevc = await guild.create_voice_channel('The Chameleon',
                                                  category=category)

        for player in self.lobby:
            # player is a User. Get it as a member.
            member: discord.Member = guild.get_member(player.id)
            try:
                await member.move_to(gamevc)
            except discord.errors.HTTPException:
                # This means the user was not connected to voice. No worries, just continue.
                pass

        return category
Exemplo n.º 32
0
    async def send_report(self, msg: discord.Message, guild: discord.Guild):

        author = guild.get_member(msg.author.id)
        report = msg.clean_content

        channel_id = await self.config.guild(guild).output_channel()
        channel = guild.get_channel(channel_id)
        if channel is None:
            return None

        files: List[discord.File] = await Tunnel.files_from_attatch(msg)

        ticket_number = await self.config.guild(guild).next_ticket()
        await self.config.guild(guild).next_ticket.set(ticket_number + 1)

        if await self.bot.embed_requested(channel, author):
            em = discord.Embed(description=report)
            em.set_author(
                name=_("Report from {author}{maybe_nick}").format(
                    author=author, maybe_nick=(f" ({author.nick})" if author.nick else "")
                ),
                icon_url=author.avatar_url,
            )
            em.set_footer(text=_("Report #{}").format(ticket_number))
            send_content = None
        else:
            em = None
            send_content = _("Report from {author.mention} (Ticket #{number})").format(
                author=author, number=ticket_number
            )
            send_content += "\n" + report

        try:
            await Tunnel.message_forwarder(
                destination=channel, content=send_content, embed=em, files=files
            )
        except (discord.Forbidden, discord.HTTPException):
            return None

        await self.config.custom("REPORT", guild.id, ticket_number).report.set(
            {"user_id": author.id, "report": report}
        )
        return ticket_number