def parse_channel_mention(mention: str, server: discord.Server) -> discord.Channel: match = re.fullmatch("<#(\\d+)>", mention) if match: return server.get_channel(match.group(1)) try: return server.get_channel(str(int(mention))) except ValueError: return None
def get_notify_channels(server: discord.Server, data_type: str): """ Find the notifying channel or return the server. """ if server.id not in osu_config.data["server"]: return None if data_type + "-channels" not in osu_config.data["server"][server.id]: return None return [server.get_channel(s) for s in osu_config.data["server"][server.id][data_type + "-channels"] if server.get_channel(s)]
def get_notify_channels(server: discord.Server, data_type: str): """ Find the notifying channel or return the server. """ if server.id not in osu_config.data["server"]: return None if data_type + "-channels" not in osu_config.data["server"][server.id]: return None return [ server.get_channel(s) for s in osu_config.data["server"][server.id][data_type + "-channels"] if server.get_channel(s) ]
def _get_welcome_channels(self, server: discord.Server): settings = self._get_settings(server) for channel_id in settings["CHANNELS"]: if settings["CHANNELS"][channel_id]["ENABLED"]: channel = server.get_channel(channel_id) if channel is not None: yield channel
async def new_case(self, server: discord.Server, *, action, mod=None, user, reason=None, until=None, channel=None, force_create=False): actionsSet = self.get_server(server) action = action.upper() isEnabled = actionsSet[action] if not force_create and not isEnabled: return False mod_channel = server.get_channel( self._owner_settings[server.id]["mod-log"]) if mod_channel is None: return None case_n = len(self.cases[server.id]) + 1 case = { "case": case_n, "created": datetime.utcnow().timestamp(), "modified": None, "action": actionsSet[action]['Repr'] + actionsSet[action]['Deco'], "channel": channel.id if channel else None, "channel_mention": channel.mention if channel else None, "user": str(user) if user is not None else None, "user_id": user.id if user is not None else None, "reason": reason, "moderator": str(mod) if mod is not None else None, "moderator_id": mod.id if mod is not None else None, "amended_by": None, "amended_id": None, "message": None, "until": until.timestamp() if until else None, } case_msg = self.format_case_msg(case) try: msg = await self.bot.send_message(mod_channel, case_msg) case["message"] = msg.id except: pass self.cases[server.id][str(case_n)] = case if mod: self.last_case[server.id][mod.id] = case_n dataIO.save_json(self._cases_path, self.cases) return case_n
def convert_disc_channel(message, author, irc_client, discord_server: Server): try: return CHANNEL_RE.sub( lambda match: "#{}".format( discord_server.get_channel(match.group(1)).name), message) except: return message
async def send_welcome(self, server: discord.Server, member: discord.Member, invite: discord.Invite = discord.Invite()): message = self.config[server.id]['joinmessage'] channel = self.config[server.id]["Channel"] if self.config[server.id]["Embed"]: data = discord.Embed( title="ID: {}".format(member.id), description=message.format(member, invite, server), colour=discord.Colour(value=randint(0, 0xFFFFFF))) data.set_thumbnail(url=member.avatar_url) await self.bot.send_message(server.get_channel(channel), embed=data) else: await self.bot.send_message(server.get_channel(channel), message.format(member, invite, server))
def find_channel(server: discord.Server, name, steps=3, mention=True): """ Find any channel by its 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 server: discord.Server to look through for channels. :param name: name as a string or mention to find. :param steps: int from 0-3 to specify search depth. :param mention: check for mentions. """ channel = None # Return a member from mention found_mention = re.search(r"<#([0-9]+)>", name) if found_mention and mention: channel = server.get_channel(found_mention.group(1)) if not channel: # Steps to check, higher values equal more fuzzy checks checks = [ lambda c: c.name.lower() == name.lower(), lambda c: c.name.lower().startswith(name.lower()), lambda c: name.lower() in c.name.lower() ] for i in range(steps if steps <= len(checks) else len(checks)): channel = discord.utils.find(checks[i], server.channels) if channel: break # Return the found channel or None return channel
def find_channel(server: discord.Server, name, steps=3, mention=True): """ Find any channel by its 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 server: discord.Server to look through for channels. :param name: name as a string or mention to find. :param steps: int from 0-3 to specify search depth. :param mention: check for mentions. """ channel = None # Return a member from mention found_mention = channel_mention_regex.search(name) if found_mention and mention: channel = server.get_channel(found_mention.group("id")) if not channel: # Steps to check, higher values equal more fuzzy checks checks = [lambda c: c.name.lower() == name.lower(), lambda c: c.name.lower().startswith(name.lower()), lambda c: name.lower() in c.name.lower()] for i in range(steps if steps <= len(checks) else len(checks)): channel = discord.utils.find(checks[i], server.channels) if channel: break # Return the found channel or None return channel
def get_welcome_channel(self, server: discord.Server): return server.get_channel(self.settings[server.id]["channel"])
def convert_disc_channel(message, author, irc_client, discord_server: Server): try: return CHANNEL_RE.sub(lambda match: "#{}".format(discord_server.get_channel(match.group(1)).name), message) except: return message
async def clear(ctx): if ctx.message.author == me: await bot.purge_from(Server.get_channel(ctx.message.server,'431806042377289739')) else: await bot.say("You are not permitted to do that")
def find_channel(server: discord.Server, name, steps=3, mention=True, channel_type="text"): """ Find any channel by its 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 server: discord.Server to look through for channels. :param name: name as a string or mention to find. :param steps: int from 0-3 to specify search depth. :param mention: check for mentions. :param channel_type: what type of channel we're looking for. Can be str or discord.ChannelType. :returns: discord.Channel """ channel = None # We want to allow both str and discord.ChannelType, so try converting str and handle exceptions if type(channel_type) is str: try: channel_type = getattr(discord.ChannelType, channel_type) except AttributeError: raise TypeError( "channel_type (str) must be an attribute of discord.ChannelType" ) elif type(channel_type) is not discord.ChannelType: raise TypeError( "channel_type must be discord.ChannelType or a str of a discord.ChannelType attribute" ) # Return a member from mention found_mention = channel_mention_regex.search(name) if found_mention and mention and channel_type is discord.ChannelType.text: channel = server.get_channel(found_mention.group("id")) if not channel: # Steps to check, higher values equal more fuzzy checks checks = [ lambda c: c.name.lower() == name.lower() and c.type is channel_type, lambda c: c.name.lower( ).startswith(name.lower()) and c.type is channel_type, lambda c: name.lower() in c.name.lower() and c.type is channel_type ] for i in range(steps if steps <= len(checks) else len(checks)): channel = discord.utils.find(checks[i], server.channels) if channel: break # Return the found channel or None return channel
def getServerChannel(self, s:Server) -> Channel: if s.id in self.serverChannels.keys(): return s.get_channel(self.serverChannels[s.id]) else: return s.default_channel