async def on_guild_join(guild: discord.Guild): """Adds guild to database if not already in it""" if not await Server.get_server(guild): log.info(f"Adding {guild.name} to DB") s = Server() s.guild = guild.id s.save()
def checkServer(brand, name): try: server = Server.getServer(brand, name) return utils.check_port(server.host, server.port) except Exception: logging.debug("server not exist, check server fails") return False
def startServer(brand, name): try: server1 = Server.getServer(brand, name) command.createCommand(server1.host, server1.ssh_port, "service " + server1.type + " start", type="SSH").execute() return None except Exception as e: logging.debug(e) raise Exception("startServer: " + str(e))
def keysMem(brand, server): try: server1 = Server.getServer(brand, server) if server1: out = RedisHelper.keys(server1.host) return out else: raise Exception("None Object Server") except Exception as e: logging.debug(e) raise Exception("keysMem: " + str(e))
def flushMem(brand, server): try: servername = server + "." + brand server1 = Server.get(Server.name == servername) if server1: out = RedisHelper.flushdb(server1.host) return out else: raise Exception("None Object Host") except Exception as e: logging.debug(e) raise Exception("flushMem:" + str(e))
async def on_guild_available(guild): print('Joined guild:', guild.name) server = Server(guild.id) if server.private_voice_category is not None: category = discord.utils.get(guild.categories, id=server.private_voice_category) if category is not None and category.channels is not None: for channel in category.channels: if len( channel.members ) == 0 and channel.id != server.private_voice_creation_channel: await channel.delete()
def restartServer(brand, name): try: server1 = Server.getServer(brand, name) command.createCommand(server1.host, server1.ssh_port, "service " + server1.type + " restart", type="SSH").execute() if (env.config.getboolean("SERVER_RELOAD_SYNC")): if checkServer(brand, name): return None else: raise Exception("server not sync") else: return None except Exception as e: logging.debug(e) raise Exception("restartServer: " + str(e))
def infoApp(brand, app): try : app = App.get(App.brand == brand, App.name == app) servers = Server.raw( '''select * from t_server where id in ({0})'''.format(app.getConfig("SERVERS")) ) deploy = appmodule.createAppDeploy(app, [s for s in servers]) if deploy: app_status = deploy.getAppStatus() if len(app_status.keys()) > 0: return app_status else: return None else: raise Exception("None Object appdeploy") except Exception as e: logging.debug(e) raise Exception("infoApp: " + str(e))
def deployApp(brand, app, master_rev, config_rev): try : app_instance = App.get(App.brand == brand, App.name == app) app_instance.master_rev = master_rev app_instance.config_rev = config_rev servers = Server.raw( '''select * from t_server where id in ({0})'''.format(app_instance.getConfig("SERVERS")) ) # if prd: # profile = DBHelper().getProfile("prd") # else: # profile = DBHelper().getProfile("dev") deploy_instance = appmodule.createAppDeploy(app_instance, [s for s in servers]) if deploy_instance: deploy_instance.deploy() deploy_instance.saveHistory() return None else: raise Exception("None Object AppDeploy") except Exception as e: logging.debug(e) raise Exception("app deploy fail:" + str(e))
def checkServers(brand, app): try : app = App.get(App.brand == brand, App.name == app) servers = Server.raw( '''select * from t_server where id in ({0})'''.format(app.getConfig("SERVERS")) ) check_list = [] for server in servers: th = utils.CheckThread(server.host, server.port) check_list.append(th) th.start() for th in check_list: th.join() time.sleep(2) flag = True for th in check_list: flag = flag and th.retcode return flag except Exception as e: logging.debug(e) logging.debug("server not exist, check server fails:" + str(e)) return False
async def on_voice_state_update(member, before, after): server = Server(member.guild.id) if server.private_voice_category is None: return if after.channel is not None: if after.channel.id == server.private_voice_creation_channel: channel = await after.channel.guild.create_voice_channel( name=f'{member.name}\'s Channel', overwrites=None, category=after.channel.category, reason=None) await channel.set_permissions(member, manage_channels=True, move_members=True, view_channel=True, connect=True) await member.move_to(channel) if before.channel is not None: if before.channel.category_id is not None and before.channel.category_id == server.private_voice_category: if before.channel.id != server.private_voice_creation_channel: if len(before.channel.members) == 0: await before.channel.delete()
def on_message(message): # Stop handling messages from this bot immediately. if message.author == client.user: return import json print(json.dumps(message.raw_mentions)) print('Recived message') user_id = message.author.id server_id = message.server.id print('UserID: {0}'.format(message.author.id)) if client.user in message.mentions and 'help' in message.content: yield from client.send_typing(message.channel) yield from asyncio.sleep(4) yield from client.send_message(message.channel, """ Jag kommer att pinga folk på fikalistan när någon pingar mig med "fika". `@fikabotten register` - registrera dig på fikalistan. `@fikabotten unregister` - bli borttagen ifrån fikalistan. `@fikabotten fika` - Trigga meddelandet till alla på fikalistan. """ ) elif client.user in message.mentions and 'unregister' in message.content: print('[on_message] Removing client ID:{0} from ID:{1}'.format(user_id, server_id)) u = session.query(User).filter(User.id==user_id).one_or_none() if u is not None: s = session.query(Server).filter(Server.id==server_id).one_or_none() if s is not None: s.users.remove(u) session.commit() yield from client.send_message(message.channel, "Du är nu borttagen") else: print('[on_message] unregister - server dont exist. waaat') else: print('[on_message] unregister - user dont exist') elif client.user in message.mentions and 'GTFO' in message.content: print('[on_message] Removing client ID:{0} from everywhere') u = session.query(User).filter(User.id==user_id).one_or_none() if u is not None: session.delete(u) session.commit() elif client.user in message.mentions and 'register' in message.content: print('[on_message] Adding client ID:{0} on ID:{1}'.format(user_id, server_id)) u = session.query(User).filter(User.id==user_id).one_or_none() if u is None: u = User(id=user_id) session.add(u) print('Added user to database') s = session.query(Server).filter(Server.id==server_id).one_or_none() if s is None: s = Server(id=server_id) session.add(s) print('Added server to database') if not s in u.servers: u.servers.append(s) session.commit() print('Added client to server') yield from client.send_message(message.channel, 'Du kommer att bli' + ' pingad när det är fika på G.') else: print('But, your already registered in this server :o') yield from client.send_message(message.channel, 'Du är redan tillagd ') yield from asyncio.sleep(3) yield from client.send_typing(message.channel) yield from asyncio.sleep(1) yield from client.send_message(message.channel, message.author.mention + ' n00b') elif (client.user in message.mentions and len(re.findall('fika', message.content,flags=re.I)) > 0): print('[on_message] TIME TO GET SOME FIKA') h = time.localtime().tm_hour # Get server and user objects as u and s. u = session.query(User).filter(User.id==user_id).one_or_none() s = session.query(Server).filter(Server.id==server_id).one_or_none() # If hen isn't, GTFO if u is None: return # Do a check for odd hours of the day. if h < 8 or h > 23: yield from client.send_message(message.channel, message.author.mention + ' :middle_finger:') return elif h > 18: yield from client.send_message(message.channel, message.author.mention + ' Lite sent för fika nu....') return #elif h == 10 or h == 11 or h == 12: # yield from client.send_message(message.channel, message.author.mention + ' Fika? Det är ju lunch...') # return # BEGIN Anti-spam section # Because people are generally assholes and will probably attempt to misuse the bot. # rate_1m = ( session.query(func.count('*')) .select_from(Trigger) .filter(Trigger.user_id==user_id) .filter(Trigger.server_id==server_id) .filter(Trigger.timestamp > time.time()-(60)) # 60s .scalar() ) rate_g_5m = ( session.query(func.count('*')) .select_from(Trigger) .filter(Trigger.server_id==server_id) .filter(Trigger.timestamp > time.time()-(5*60)) # 5*60s .scalar() ) rate_5m = ( session.query(func.count('*')) .select_from(Trigger) .filter(Trigger.user_id==user_id) .filter(Trigger.server_id==server_id) .filter(Trigger.timestamp > time.time()-(5*60)) # 5*60 s .scalar() ) #rate_30m = ( # session.query(func.count('*')) # .select_from(Trigger) # .filter(Trigger.user_id==user_id) # .filter(Trigger.server_id==server_id) # .filter(Trigger.time > ()time.time()-(30*60)) # 5*60 s # .scalar() #) rate_limit_bail = False # RTL-1 if rate_g_5m >= 1: print('RTL-1 - silent') rate_limit_bail = True # RTL-2 if rate_5m == 4: print('RTL-2 - verbose') rate_limit_bail = True yield from client.send_message(message.channel, message.author.mention + ' Förhelvete...!') if rate_1m == 7: print('RTL-3 - GTFO') rate_limit_bail = True yield from client.send_message(message.channel, message.author.mention + ' :middle_finger:') session.add(Trigger( user_id=message.author.id, server_id=server_id, timestamp=int(time.time()) )) # Gotta commit those changes to the DB. session.commit() if rate_limit_bail: return # END Anti-spam section # # Okej, ready for action. Vi har serverobjektet. if s is not None: fikare_db = s.users # En list med alla användare relaterade till servern. fikare_mentions = "" for fikare in fikare_db: #loopa över listan fikare_mentions += '<@{0.id}> '.format(fikare) # Lägg till mentions till en lång sträng. yield from client.send_message(message.channel, fikare_mentions + "FIKA!!") #Skrik. ( session.query(Trigger) .filter(Trigger.timestamp < int(time.time()-30*60)) .delete() ) elif len(re.findall('fika', message.content, flags=re.I)) > 0: print('[on_message] DEBUG: fika matched, but no trigger.') print('------')