From bde5bfe5302fbb1b925fe87bcc13c2be99dc83b3 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 22 Aug 2022 20:03:31 +0000 Subject: [PATCH] add role and vc check, fix config to be stateful --- src/config.py | 36 ++---------------------------------- src/musicbot.py | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/config.py b/src/config.py index 826a8c5..c16e699 100644 --- a/src/config.py +++ b/src/config.py @@ -4,7 +4,7 @@ sample_json = """ { "guild":{ "token": "", - "prefix": "$", + "prefix": "?", "roleid": 0, "username": "music-bot" }, @@ -27,36 +27,4 @@ def save_config(path, config_vector): f = open(path, "w") json.dump(config_vector, f, ensure_ascii=False, indent=4) f.close() - return 0 - -def get_token(config_path): - config_vector = {} - success = load_config(config_path, config_vector) - if success == 0: - return config_vector["guild"]["token"] - else: - return None - -def get_prefix(config_path): - config_vector = {} - success = load_config(config_path, config_vector) - if success == 0: - return config_vector["guild"]["prefix"] - else: - return None - -def get_roleid(config_path): - config_vector = {} - success = load_config(config_path, config_vector) - if success == 0: - return config_vector["guild"]["roleid"] - else: - return None - -def get_username(config_path): - config_vector = {} - success = load_config(config_path, config_vector) - if success == 0: - return config_vector["guild"]["username"] - else: - return None \ No newline at end of file + return 0 \ No newline at end of file diff --git a/src/musicbot.py b/src/musicbot.py index 86f1e4e..61e7888 100644 --- a/src/musicbot.py +++ b/src/musicbot.py @@ -10,15 +10,19 @@ ffmpeg_options = { 'options': '-vn' } -token = get_token(config_path) - -async def determine_prefix(bot, message): - return get_prefix(config_path) +config = {} +x = load_config(config_path, config) +if x == 1: + print('Failed to load config, exiting') + exit(1) +token = config['guild']['token'] +prefix = config['guild']['prefix'] intents = discord.Intents.default() intents.members = True intents.message_content = True -bot = commands.Bot(command_prefix = determine_prefix, description='very cool', intents = intents) +bot = commands.Bot(command_prefix = prefix, description='very cool', intents = intents) +bot.config = config @bot.event async def on_ready(): @@ -33,9 +37,8 @@ async def setprefix(ctx, *arg): else: prefix = arg[0] - with open("prefix", "w") as f: - f.write(prefix) - f.close() + bot.config['guild']['prefix'] = prefix + save_config(config_path, bot.config) await ctx.send("set prefix to: {0}".format(prefix)) @@ -47,14 +50,26 @@ async def setrole(ctx, *arg: discord.Role): else: roleid = arg[0].id - with open("roleid", "w") as f: - f.write(str(roleid)) - f.close() + bot.config['guild']['roleid'] = roleid + save_config(config_path, bot.config) - await ctx.send("set followable role to {0}".format(arg[0])) + await ctx.send("set playable role to {0}".format(arg[0])) @bot.command() -async def playmusic(ctx, *arg): +async def play(ctx, *arg): + + roleid = bot.config['guild']['roleid'] + + if(len(arg) != 1): + await ctx.send("usage: play ") + return + elif(ctx.author.voice == None): + await ctx.send("you're not in a voice channel") + return + elif(roleid not in [role.id for role in ctx.author.roles]): + print() + await ctx.send("you do not have the role to play music") + return ydl_opts = { 'format': 'mp4',