async def on_ready(self): main.running_bots[self.bot.user.id] = { 'bot': self.bot, 'token': self.bot.http.token, 'activity': None } update_cog_object = self.bot.get_cog('UpdateTask') # for instances if config.CONFIG.secret_token != self.bot.http.token: bot_instance = data.get_bot_instance_token(self.bot.http.token) # set presence if bot_instance[BOT_ACTIVITY_TEXT_KEY]: await self.bot.change_presence(status=discord.Status.online, activity=main.running_bots[self.bot.user.id]['activity']) # set bot id data.set_bot_id(self.bot.user.id, self.bot.http.token) # set bot name data.set_bot_name(bot_instance[GUILD_ID_KEY], self.bot.user.name) # for the main bot else: main.main_bot = self.bot await self.bot.run_bot_instances() update_cog_object.run_tasks.start() print("We have logged in as {0.user}".format(self.bot)) update_cog_object.update.start() if not default_avatar: await set_default_avatar() self.bot.timers.run_old()
async def on_ready(self): running_bots[self.bot.user.id] = { 'bot': self.bot, 'token': self.bot.http.token, 'activity': None } # for instances if config.CONFIG.secret_token != self.bot.http.token: bot_instance = data.get_bot_instance_token(self.bot.http.token) if bot_instance[BOT_ACTIVITY_TEXT_KEY]: await update_activity(bot_instance, bot_instance[BOT_ACTIVITY_TYPE_KEY], bot_instance[BOT_ACTIVITY_TEXT_KEY]) # set bot id data.set_bot_id(self.bot.user.id, self.bot.http.token) # set bot name data.set_bot_name(bot_instance[GUILD_ID_KEY], self.bot.user.name) # for the main bot if not running_bot_instances: global main_bot main_bot = self.bot asyncio.create_task(self.run_bot_instances()) print("We have logged in as {0.user}".format(self.bot)) self.update.start()
async def read_mapping(guildId: str): bot_instance = data.get_bot_instance(guildId) if not bot_instance: return {"guildId": guildId, "bot_name": "rallybot"} # set name in db to current bot name if name is empty if not bot_instance[BOT_NAME_KEY]: bot_object = update_cog.running_bots[bot_instance[BOT_ID_KEY]]['bot'] bot_instance[BOT_NAME_KEY] = bot_object.user.name data.set_bot_name(guildId, bot_object.user.name) return { "guildId": guildId, "bot_name": bot_instance[BOT_NAME_KEY], 'name_timeout': int(bool(bot_instance[NAME_TIMEOUT_KEY])) }
async def update_name(guild_id: int, bot_id: int, new_name: str): """ Update the name of bot @param guild_id: id of guild @param bot_id: id of bot @param new_name: new neame of the bot """ bot_object = main.running_bots[bot_id]['bot'] # name change try: if new_name != bot_object.user.name: await bot_object.user.edit(username=new_name) data.set_bot_name(guild_id, new_name) except discord.HTTPException: # user is editing name too many times, set 1h timeout timout = round(time.time() + 3600) data.set_name_timeout(guild_id, timout) except: pass
async def add_mapping(mapping: BotNameMapping, guildId: str): bot_instance = data.get_bot_instance(guildId) if not bot_instance: raise HTTPException(status_code=404, detail="Bot config not found") # name timout if bot_instance[NAME_TIMEOUT_KEY] and int( bot_instance[NAME_TIMEOUT_KEY]) <= time.time(): data.set_name_timeout(bot_instance[GUILD_ID_KEY], 0) bot_instance[NAME_TIMEOUT_KEY] = 0 bot_object = update_cog.running_bots[bot_instance[BOT_ID_KEY]]['bot'] if mapping.bot_name and not bot_instance[NAME_TIMEOUT_KEY]: data.set_bot_name(guildId, mapping.bot_name) # name change try: if mapping.bot_name != bot_object.user.name: await bot_object.user.edit(username=mapping.bot_name) data.set_bot_name(guildId, mapping.bot_name) except discord.HTTPException: # user is editing name too many times, set 1h timeout timout = round(time.time() + 3600) data.set_name_timeout(bot_instance[GUILD_ID_KEY], timout) bot_instance[NAME_TIMEOUT_KEY] = timout except: raise HTTPException(status_code=500, detail="Error changing bot name") return { "guildId": guildId, "bot_name": bot_object.user.name, 'name_timeout': int(bool(bot_instance[NAME_TIMEOUT_KEY])) }
async def set_bot_name(self, ctx, *, name=""): try: await self.bot.user.edit(username=name) data.set_bot_name(ctx.guild.id, name) except Exception as e: return await ctx.send(f'Error: {e.text.split(":")[-1]}')