async def on_message(message): # don't respond to ourselves if message.author.id == self.bot.info.id: return msg = self.convert_message(message) try: logging.info( '[%s] %s@%s [%s] sent [%s] %s' % (msg.sender.id, msg.sender.first_name, msg.conversation.title, msg.conversation.id, msg.type, msg.content)) except AttributeError: logging.info( '[%s] %s@%s [%s] sent [%s] %s' % (msg.sender.id, msg.sender.title, msg.conversation.title, msg.conversation.id, msg.type, msg.content)) try: if msg.content.startswith('/') or msg.content.startswith(self.bot.config.prefix): if int(msg.conversation.id) > 0: chat = self.client.get_user(msg.conversation.id) else: chat = self.client.get_channel(positive(msg.conversation.id)) await chat.trigger_typing() self.bot.on_message_receive(msg) while self.bot.outbox.qsize() > 0: msg = self.bot.outbox.get() logging.info(' [%s] %s@%s [%s] sent [%s] %s' % (msg.sender.id, msg.sender.first_name, msg.conversation.title, msg.conversation.id, msg.type, msg.content)) await self.send_message(msg) except KeyboardInterrupt: pass except Exception as e: logging.error(e) if self.bot.started: catch_exception(e, self.bot)
async def send_message(self, message): try: if int(message.conversation.id) > 0: chat = self.client.get_user(message.conversation.id) else: chat = self.client.get_channel(positive(message.conversation.id)) await chat.trigger_typing() if message.type == 'text': content = self.add_discord_mentions(chat, message.content) if message.extra: if 'format' in message.extra and message.extra['format'] == 'HTML': content = html_to_discord_markdown(content) if 'preview' in message.extra and not message.extra['preview']: content = re.sub(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', r'<\g<0>>', content, flags=re.MULTILINE) if len(content) > 2000: texts = split_large_message(content, 2000) for text in texts: await chat.send(text) else: await chat.send(content) elif message.type == 'photo' or message.type == 'document' or message.type == 'video' or message.type == 'voice': send_content = True embed = Embed() if message.extra and 'caption' in message.extra and message.extra['caption']: lines = message.extra['caption'].split('\n') embed.title = lines[0] embed.description = '\n'.join(lines[1:]) send_content = False if send_content: if message.content.startswith('/'): await chat.send(file=discord.File(message.content, filename=message.type + get_extension(message.content))) else: await chat.send(message.content) else: if message.content.startswith('/'): await chat.send(file=discord.File(message.content, filename=message.type + get_extension(message.content)), embed=embed) elif message.content.startswith('http'): if message.type == 'photo': embed.set_image(url=message.content) elif message.type == 'video': embed.set_video(url=message.content) else: embed.url = message.content await chat.send(embed=embed) except KeyboardInterrupt: pass except Exception as e: logging.error(e) if self.bot.started: catch_exception(e, self.bot)
def kick_conversation_member(self, conversation_id, user_id): if int(conversation_id) > 0: return False channel = self.client.get_channel(positive(conversation_id)) user = self.client.get_user(user_id) try: self.discord_loop.create_task(channel.guild.kick(user)) return True except: return False
def get_chat_administrators(self, conversation_id): if int(conversation_id) > 0: return False channel = self.client.get_channel(positive(conversation_id)) admins = [] for member in channel.members: perms = channel.permissions_for(member) if perms.administrator: admins.append(User(member.id, member.name, '#' + member.discriminator, member.name + '#' + member.discriminator, member.bot)) logging.info(admins) return admins
def create_webhook_request(avatar): channel = self.client.get_channel(positive(channel_id)) if channel and channel.guild: webhook = self.discord_loop.create_task(channel.create_webhook(name=name,avatar=avatar)) else: logging.info('invalid channel')
def get_message(self, chat_id, message_id): channel = self.client.get_channel(positive(chat_id)) message = self.discord_loop.create_task(channel.fetch_message(message_id)) return self.convert_message(message)