def boardcastMessage(self, senderInfo, messageData): self.log('Received message "{0}", boardcasting'.format( utils.messageData_to_string(messageData))) for msg in utils.messageData_to_strings(messageData): self.log(msg, ChatLogFile) for client in self.clients: if client.info != senderInfo: client.sendMessage(messageData)
async def listeningMessage(self): self.log('Message listening looping...') try: channel_chat = self.get_channel(self.config.channel_chat) while True: try: messageData = self.messages.get( block=False) # type: MessageData except queue.Empty: await asyncio.sleep(0.05) continue self.log('Message data with type {} and channel {} get'.format( messageData.type, messageData.channel)) data = messageData.data if messageData.type == MessageDataType.CHAT: # chat message assert isinstance(data, dict) self.log('Processing chat message from chatbridge: ' + utils.messageData_to_string(data)) for message in utils.messageData_to_strings(data): try: translation = translator.translate(data['message'], dest='en') dest = 'en' if translation.src != dest: message += ' | [{} -> {}] {}'.format( translation.src, dest, translation.text) except: self.log('Translate fail') await channel_chat.send( self.formatMessageToDiscord(message)) elif messageData.type == MessageDataType.EMBED: # embed assert isinstance(data, discord.Embed) self.log('Sending embed') await self.get_channel(messageData.channel).send(embed=data ) elif messageData.type == MessageDataType.TEXT: await self.get_channel(messageData.channel).send( self.formatMessageToDiscord(str(data))) else: self.log('Unkown messageData type {}'.format( messageData.data)) except: self.log(traceback.format_exc()) await self.close()
async def listeningMessage(self): channel = self.get_channel(self.config.channel) try: while True: if len(self.messages) == 0: await asyncio.sleep(0.05) continue messageData = self.messages.pop(0) if type(messageData) == dict: # chat message self.log('Processing chat message ' + utils.messageData_to_string(messageData)) for message in utils.messageData_to_strings(messageData): try: translation = translator.translate( messageData['message'], dest='en') dest = 'en' if translation.src != dest: message += ' | [{} -> {}] {}'.format( translation.src, dest, translation.text) except: self.log('Translate fail') await channel.send(self.formatMessageToDiscord(message) ) elif type(messageData) == discord.Embed: # embed self.log('Sending embed') await channel.send(embed=messageData) elif type(messageData) == str: await channel.send(self.formatMessageToDiscord(messageData) ) else: self.log('Unkown messageData type {}'.format( type(messageData))) except: s = traceback.format_exc() print(s) self.log(s) await self.close()
def sendMessage(self, messageData): if self.isOnline(): self.log('Sending message "{}" to the client'.format( utils.lengthLimit(utils.messageData_to_string(messageData)))) self.sendData(json.dumps(messageData))
def addChatMessage(self, messageData): self.log('Adding message "' + utils.messageData_to_string(messageData) + '" to Discord Bot') self.messages.append(messageData)