def on_message(client, userdata, message): topic = message.topic if (message.topic in config.ADMIN_CHANNELS) is True: return print() message = json.loads(str(message.payload)[2:-1]) mess = MessageHandler(message) res = mess.perform() if res is not None: print("OI OI " + str(res.payload)) send_message(topic, res) if config.DEBUG is True and mess.id is not False: print(mess)
def main(): p = argparse.ArgumentParser() p.add_argument("--config", required=True, help="Path to configuration file") args = p.parse_args() with open(args.config, "r") as f: config = json.load(f) client = discord.Client() message_handler = MessageHandler(config, client) @client.event async def on_ready(): print("Logged in as", client.user.name) @client.event async def on_message(message): await message_handler.handle(message) client.run(config["token"])
# adjusting the path import sys from settings import ROOT_PATH sys.path.append(str(ROOT_PATH / 'scripts')) sys.path.append(str(ROOT_PATH / 'scripts' / 'misc_func')) sys.path.append(str(ROOT_PATH / 'scripts' / 'handlers')) # imports from settings import TOKEN, PREFIX, OBJECTIVES from handlers.message_handler import MessageHandler import discord from misc_func import primes, factors list_command_handler = MessageHandler() @list_command_handler.create_handler('list factors <int:number>') async def prime_handler(user_msg, number): await user_msg.channel.send(' '.join(map(str, factors.get_factors(number)))) @list_command_handler.create_handler('list primes <int:number>') async def prime_handler(user_msg, number): await user_msg.channel.send(' '.join(map(str, primes.get_primes(number)))) @list_command_handler.create_handler('list objectives') async def list_objectives_handler(user_msg): await user_msg.channel.send('\n'.join(OBJECTIVES))
def main(): p = argparse.ArgumentParser() p.add_argument("--config", required=True, help="Path to configuration file") p.add_argument("--sync-roles", action="store_true") args = p.parse_args() sync_roles = args.sync_roles with open(args.config, "r") as f: config = json.load(f) client = discord.Client() message_handler = MessageHandler(config, client) @client.event async def on_ready(): print("Logged in as", client.user.name) if sync_roles: p = subprocess.Popen( config["sync_roles"]["command"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) output, stderr = p.communicate() if stderr and b"psycopg2-binary" not in stderr: # ignore psycopg2-binary warning (sigh) raise RuntimeError("Error while running command: %r" % (stderr)) accounts = json.loads(output.decode("utf-8")) discord_ids = [account_data["discord_id"] for account_data in accounts] server_id = config["sync_roles"]["server_id"] server = client.get_server(str(server_id)) if server is None: raise RuntimeError("Could not find server %r" % (server_id)) role_name = config["sync_roles"]["role"] role_to_sync = None for role in server.roles: if role.name == role_name: role_to_sync = role break else: raise ValueError("Role %r not found on server %r" % (role_name, server)) for member in server.members: member_roles = list(member.roles) if member.id in discord_ids: if role_to_sync not in member_roles: print("Adding role to %r" % (member)) await client.add_roles(member, role_to_sync) else: if role_to_sync in member_roles: print("Removing role from %r" % (member)) await client.remove_roles(member, role_to_sync) print("Done") await client.logout() @client.event async def on_message(message): if not sync_roles: await message_handler.handle(message) client.run(config["token"])
Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() return session if __name__ == "__main__": # Socket communicator is for talking with the clients db_session = connect_database() new_logger = create_logger() server = ServerSocketLayer(9000, logger=new_logger) # Server controller pings receives and sends messages and checks on the clients message_handler = MessageHandler() utility_handler = UtilityHandler() message_handler.register_handler(utility_handler, "utility") from addons.drone_add_on.handlers.drone_handler import DroneHandler drone_handler_object = DroneHandler() message_handler.register_handler(drone_handler_object, "drone") server_controller = SocketServerController(server, message_handler, db_session, logger=new_logger) # core logic is the ui which allows us to create and send commands to the clients and also see info about them! ui_logic = ServerLogic() ui_logic.server_controller = server_controller