async def say(ctx: discord.Message, client: discord.Client): # Checks if user is admin or bot owner if not (_checkrole.author_has_role( ctx, _mongoFunctions.get_settings(ctx.guild.id)['admin_role']) or _util.author_is_bot_owner(ctx)): replyEmbed = _embedMessage.create("Say Reply", "Invalid Permissions", "red") await ctx.channel.send(embed=replyEmbed) return global target_channel args = _util.parse_message(ctx.content) if len(args) != 4: await ctx.channel.send(embed=_embedMessage.create( "Say Reply", "Invalid Syntax! You need three arguments for this function!\nSyntax: say title content channel", "red")) return title = args[1] content = args[2] channel_mention = args[3] channels = client.get_all_channels() for channel in channels: if channel.mention == channel_mention: target_channel = channel break if target_channel is None: await ctx.channel.send(embed=_embedMessage.create( "Say Reply", "Channel not Found!", "red")) return try: await ctx.delete() except: print("Missing Manage Messages permission in {0} on server ID: {1}". format(channel.mention, str(ctx.guild.id))) if not _util.author_is_bot_owner(ctx): embed = _embedMessage.create( title, content + "\n\n This message was sent by {}".format(ctx.author.mention), "green") else: embed = _embedMessage.create(title, content, "green") await target_channel.send(embed=embed)
def update(client: discord.Client): changes: bool = False servers: list = client.guilds server_ids: list = [] collected_servers: list = crayon.DB.execute_query("SELECT serverID FROM server;") collected_server_ids: list = [] for entry in collected_servers: collected_server_ids.append(int(entry[0])) for server in servers: server_ids.append(server.id) if server.id not in collected_server_ids: changes = True crayon.DB.check_server_register(server) for entry in collected_server_ids: if entry not in server_ids: changes = True crayon.DB.execute_query( "DELETE FROM server WHERE serverID = {serverid};".format(serverid=entry)) channels = client.get_all_channels() channel_ids = [] collected_channels: list = crayon.DB.execute_query("SELECT channelID FROM channel;") collected_channel_ids: list = [] for entry in collected_channels: collected_channel_ids.append(int(entry[0])) for channel in channels: channel_ids.append(channel.id) if channel.id not in collected_channel_ids: changes = True crayon.DB.check_channel_register(channel) for entry in collected_channel_ids: if entry not in channel_ids: changes = True crayon.DB.execute_query( "DELETE FROM channel WHERE channelID = {channelid};".format(channelid=entry)) if changes: print("Database synced")
def get_channel_by_name(bot: discord.Client, channel_name: str, server: discord.Server = None, server_id: str = None, server_name: str = None) -> discord.Channel: """Finds a channel by name on all the channels visible by the bot. If server, server_id or server_name is specified, only channels in that server will be searched""" if server is None and server_id is not None: server = bot.get_server(server_id) if server is None and server_name is not None: server = get_server_by_name(bot, server_name) if server is None: channel = discord.utils.find( lambda m: m.name == channel_name and not m.type == discord. ChannelType.voice, bot.get_all_channels()) else: channel = discord.utils.find( lambda m: m.name == channel_name and not m.type == discord. ChannelType.voice, server.channels) return channel
class DiscordHandler(GenericHandler): def __init__(self): super(DiscordHandler, self).__init__() self.bot = Client() self.message_handlers = WeakSet() self.channels = WeakValueDictionary() async def setup(self, token, client_secret): await self.bot.login(token=token, bot=True) asyncio.ensure_future(self.bot.connect(reconnect=True)) await self.bot.wait_until_ready() print("Logged into Discord") self.bot.event(self.on_message) self.bot.event(self.on_guild_channel_create) self.bot.event(self.on_guild_channel_delete) self.bot.event(self.on_guild_channel_update) def get_channel(self, serialised) -> Optional[DiscordChannel]: channel_id = serialised.get("id") try: return self.channels[channel_id] except KeyError: pass try: channel = self.bot.get_channel(int(channel_id)) except ValueError: return if not channel: return rtn = DiscordChannel(channel) self.message_handlers.add(rtn.on_message_handler) self.channels[channel_id] = rtn return rtn @property def serialised_channels(self): return [{ "type": "discord", "id": f"{channel.id}", "name": channel.name, "server": { "id": f"{channel.guild.id}", "name": channel.guild.name, "icon": str(channel.guild.icon_url_as(format="png", size=256)) } } for channel in self.bot.get_all_channels() if (channel.permissions_for(channel.guild.me).manage_webhooks and isinstance(channel, TextChannel))] async def on_message(self, message): await asyncio.gather( *[handler(message) for handler in self.message_handlers]) async def on_guild_channel_create(self, channel): await self.process_channel_handlers() async def on_guild_channel_delete(self, channel): await self.process_channel_handlers() async def on_guild_channel_update(self, before, after): await self.process_channel_handlers()