async def on_message(cls, message): from henrique.main.singleton.khala.henrique_khala import HenriquePacket from khala.singleton.messenger.discord.internal.packet_discord import PacketDiscord from khala.singleton.messenger.discord.internal.chatroom_discord import ChatroomDiscord from henrique.main.singleton.khala.henrique_khala import HenriqueCommand logger = KhalaLogger.func_level2logger(cls.on_message, logging.DEBUG) client = cls.client() text_in = message.content logger.debug({"message": message, }) if DiscordTool.user_message2is_author(client.user, message): return if not HenriqueCommand.text2is_query(text_in): return Chatroom.chatrooms2upsert([ChatroomDiscord.message2chatroom(message)]) ChannelUser.channel_users2upsert([ChannelUserDiscord.message2channel_user(message)]) packet = PacketDiscord.message2packet(message) logger.debug({"packet": packet, }) text_out = HenriquePacket.packet2response(packet) await message.channel.send(text_out)
def codenames2chatrooms(cls, codenames): logger = KhalaLogger.func_level2logger(cls.codenames2chatrooms, logging.DEBUG) collection = ChatroomCollection.collection() if codenames is not None: cursor = collection.find({cls.Field.CODENAME: {"$in": codenames}}) else: cursor = collection.find() # raise Exception({"collection":collection, "codenames":codenames, # '{cls.Field.CODENAME: {"$in": codenames}}':{cls.Field.CODENAME: {"$in": codenames}} # }) h_codename2doc = merge_dicts([{ Chatroom.chatroom2codename(doc): doc } for doc in map(MongoDBTool.bson2json, cursor)], vwrite=vwrite_no_duplicate_key) doc_list = lmap(h_codename2doc.get, codenames) logger.debug({#"h_codename2doc":h_codename2doc, #"codenames":codenames, "doc_list":doc_list, }) assert_equal(len(codenames), len(doc_list)) return doc_list
def _rootname_list(cls): return luniq( chain( FoxylibLogger.rootname_list(), KhalaLogger.rootname_list(), [cls.ROOTNAME], ))
def docs2upsert(cls, docs): logger = KhalaLogger.func_level2logger(cls.docs2upsert, logging.DEBUG) def doc2pair(doc): doc_filter = DictTool.keys2filtered(doc, [ChannelUser.Field.CODENAME]) return doc_filter, doc pair_list = lmap(doc2pair, docs) collection = ChannelUserCollection.collection() mongo_result = MongoDBTool.j_pair_list2upsert(collection, pair_list) logger.debug({ "mongo_result.bulk_api_result": mongo_result.bulk_api_result, }) # raise Exception({"docs": docs, # "pair_list": pair_list , # "mongo_result.bulk_api_result":mongo_result.bulk_api_result, # }) cls._docs2cache(docs) return mongo_result # class KakaotalkUWOChannelUserDoc: # @classmethod # def username2doc(cls, username): # WARMER.warmup()
async def on_ready(cls): logger = KhalaLogger.func_level2logger(cls.on_ready, logging.DEBUG) client = cls.client() logger.info({"client.user.name": client.user.name, "client.user.id": client.user.id, "client": client, })
def codename2chatroom(cls, codename): logger = KhalaLogger.func_level2logger(cls.codename2chatroom, logging.DEBUG) # logger.debug({"codename": codename}) chatrooms = cls.codenames2chatrooms([codename]) logger.debug({"chatrooms": chatrooms}) return l_singleton2obj(chatrooms)
def chatroom(cls, ): logger = KhalaLogger.func_level2logger(cls.chatroom, logging.DEBUG) chatroom = {Chatroom.Field.CHANNEL: Channel.Codename.KAKAOTALK_UWO, Chatroom.Field.CODENAME: cls.codename(), # Chatroom.Field.EXTRA: ChatroomDiscord.Extra.message2extra(message), Chatroom.Field.LOCALE: cls.Constant.LOCALE, } logger.debug({"chatroom": chatroom, }) return chatroom
def start_discord(): from henrique.main.singleton.env.henrique_env import HenriqueEnv from henrique.main.singleton.logger.henrique_logger import KhalaLogger logger = KhalaLogger.func_level2logger(start_discord, logging.DEBUG) logger.debug({"HenriqueEnv.env()": HenriqueEnv.env()}) HenriqueWarmer.warmup_all() # maybe update? # https://stackoverflow.com/a/50981577 client = DiscordClient.client() discord_token = HenriqueEnv.key2value("DISCORD_TOKEN") logger.debug({"discord_token": discord_token}) assert_true(discord_token) client.run(discord_token)
def message2channel_user(cls, message): logger = KhalaLogger.func_level2logger(cls.message2channel_user, logging.DEBUG) codename = cls.message2codename(message) alias = PacketDiscord.message2sender_name(message) doc = {ChannelUser.Field.CHANNEL: Channel.Codename.DISCORD, ChannelUser.Field.CODENAME: codename, ChannelUser.Field.ALIAS: alias } logger.debug({"message": message, "doc": doc, }) return doc
def sender_name2channel_user(cls, sender_name): logger = KhalaLogger.func_level2logger(cls.sender_name2channel_user, logging.DEBUG) codename = cls.sender_name2codename(sender_name) doc = { ChannelUser.Field.CHANNEL: Channel.Codename.KAKAOTALK_UWO, ChannelUser.Field.CODENAME: codename, ChannelUser.Field.ALIAS: sender_name } logger.debug({ "sender_name": sender_name, "doc": doc, }) return doc
def message2chatroom(cls, message): logger = KhalaLogger.func_level2logger(cls.message2chatroom, logging.DEBUG) codename = cls.message2codename(message) chatroom = { Chatroom.Field.CHANNEL: Channel.Codename.DISCORD, Chatroom.Field.CODENAME: codename, # Chatroom.Field.EXTRA: ChatroomDiscord.Extra.message2extra(message), Chatroom.Field.LOCALE: Khala.Default.LOCALE, } logger.debug({ "message": message, "chatroom": chatroom, }) return chatroom
def _row_iter(cls): logger = KhalaLogger.func_level2logger(cls._row_iter, logging.DEBUG) # h_id2culture = CultureTable.dict_id2codename() # logger.debug({"h_id2culture":h_id2culture}) h = {} with HenriquePostgres.cursor() as cursor: sql = SQL("SELECT * FROM {} ORDER BY id ASC").format( Identifier(cls.NAME), ) cursor.execute(sql) for t in PostgresTool.fetch_iter(cursor): program, key4program, name, uuid_str = t[1:5] uiud_hex = UUIDTool.x2hex(uuid_str) yield { cls.Field.PROGRAM: program, cls.Field.KEY4PROGRAM: key4program, cls.Field.NAME: name, cls.Field.UUID: uiud_hex, }
def _docs2cache(cls, docs): logger = KhalaLogger.func_level2logger(cls._docs2cache, logging.DEBUG) for doc in docs: codename = ChannelUser.channel_user2codename(doc) CacheManager.add2cache(cls.codenames2docs, doc, args=[codename])