async def remove(self, ctx, key): """Admin: Remove a server from eggbot. Use the server name as displayed in !s command. usage !remove <servername>""" if key is not None: pattern = re.compile("<@!*[0-9]*>") if pattern.match( key ): #to convert discord id for server name if user call command with @<server> user = get(self.client.get_all_members(), id=int(re.sub(r'[<@!>]', '', key))) key = user.name.lower() else: key = key.replace("@", "").lower() await ctx.send(key + " will be removed from the list, confirm with YES ") msg = await self.client.wait_for('message') if msg.content == "YES" and ctx.message.author == msg.author: try: del DATA["server_list"][key] save_data() await ctx.send(key + " as been removed!") logger.warning(key + " deleted from config") except KeyError: await ctx.send(key + " is not in the config.") else: await ctx.send("removal cancelled!")
async def get_fry_meta(self, name, serverObj): headers = {'Authorization': 'Bearer ' + serverObj['token']} try: async with aiohttp.ClientSession() as session: async with session.get(serverObj["endpoint"] + '/v1/meta', headers=headers) as resp: if resp.status >= 400: logger.info('getting new token for ' + name) serverObj['token'] = DATA["server_list"][name][ "token"] = await self.get_token(serverObj) save_data() headers = { 'Authorization': 'Bearer ' + serverObj['token'] } async with aiohttp.ClientSession() as session: async with session.get(serverObj["endpoint"] + '/v1/meta', headers=headers) as resp: result = await resp.json() return result["data"] else: result = await resp.json() return result["data"] except: return False
async def on_message(self, message): if self.user.id != message.author.id and message.author.name in DATA[ "server_list"].keys(): if "has joined the server" in message.content: info = await self.get_fry_meta( message.author.name, DATA["server_list"][message.author.name]) DATA["server_list"][ message.author.name]["players"] = info["players_online"] save_data() else: await self.process_commands(message)
async def add(self, ctx, host, user, password): """Admin: Setup a server. The server name will be whatever you setup in the fry config. usage !add <host> <user> <password> The name meta value of the FryBot has to be set first. !setmeta for more info.""" if host is not None and user is not None and password is not None: try: #tries to connect to the api site of the frybot. host = host.strip('/') data = {"endpoint": host, "id": user, "password": password} token = await self.client.get_token(data) await ctx.send("Token acquired!") except Exception as inst: logger.exception(inst) await ctx.send( "something went wrong, check the url, username and password" ) return try: #uses the token to get the name from the meta data. headers = {'Authorization': 'Bearer ' + token} async with aiohttp.ClientSession() as session: async with session.get(host + '/v1/meta', headers=headers) as response: result = await response.json() name = result['data']['name'] name = name.replace(" ", "").lower() await ctx.send(name + " Found!") except Exception as inst: logger.exception(inst) await ctx.send( "Failed to get server name, Please set Fry's name in the meta data with the Fry commands. Ex. @FryBot !meta set name <FryBot> " ) return try: DATA["server_list"][name] = { "endpoint": host, "id": user, "password": password, 'token': token } except KeyError: DATA["server_list"] = {} DATA["server_list"][name] = { "endpoint": host, "id": user, "password": password, 'token': token } save_data() await ctx.send("Added successfully !") else: await ctx.send( " !set argument is not valid. usage !add <servername> <host> <user> <password>. Ex: !add https://localhost:4321 admin 12345abc" )
async def store_online_users(self): methods = [] for name, server in DATA["server_list"].items(): methods.append(self.get_fry_meta(name, server)) data = await asyncio.gather(*methods) data.sort(key=lambda s: "" if not s else s.get("name")) for info in data: if info: server = info["name"].strip().lower() if info["players_online"].__len__() > 0: DATA["server_list"][server]["players"] = info[ "players_online"] logger.info("Storing online players...") save_data()
async def remove(self, ctx, key): """Admin: Remove a server from eggbot. Use the server name as displayed in !s command. usage !remove <servername>""" if key is not None: await ctx.send(key + " will be removed from the list, confirm with YES ") msg = await self.client.wait_for('message') if msg.content == "YES" and ctx.message.author == msg.author: try: del DATA["server_list"][key] save_data() await ctx.send(key + " as been removed!") logger.warning(key + " deleted from config") except KeyError: await ctx.send(key + " is not in the config.") else: await ctx.send("removal cancelled!")
async def set(self, ctx, arg, *, text): """Admin: Change the apply url or the Welcome message. usage !set <applyUrl|joinMessage> <text>. For the Join message you have two parameter {member} {applyUrl} to work into your message. """ set_command_dict = { "applyurl": "applyUrl", "joinmessage": "joinMessage" } try: arg = set_command_dict.get(arg.lower(), None) if arg is not None and text != "": DATA[arg] = text.strip() save_data() await ctx.send("Saved!") else: await ctx.send( " !set argument is not valid. usage !set <applyUrl|connectUrl|joinMessage> <text>." ) except Exception as inst: logger.exception(inst) await ctx.send("you are missing something, try again!") return
def login(self): if not self.is_email_registered(): return False try: user = {'email': self.email, 'password': self.password} res = self.s.post(config['base_url'] + '/auth/login', data=user, timeout=5) except TimeoutError: print(timeout_error_message[lang]) return False except Exception: print(connection_error_message[lang]) return False if (res.status_code == 200): self.id = res.json()['id'] jwt_res = self.s.get(config['base_url'] + '/user/jwt') jwt = jwt_res.json() self.s.headers.update({'access_token': jwt['token']}) save_data(jwt, self.id) self.jwt = jwt return res.json() return False