add role and vc check,

fix config to be stateful
This commit is contained in:
Arthur Lu 2022-08-22 20:03:31 +00:00
parent 5db11730ac
commit bde5bfe530
2 changed files with 30 additions and 47 deletions

View File

@ -4,7 +4,7 @@ sample_json = """
{ {
"guild":{ "guild":{
"token": "", "token": "",
"prefix": "$", "prefix": "?",
"roleid": 0, "roleid": 0,
"username": "music-bot" "username": "music-bot"
}, },
@ -27,36 +27,4 @@ def save_config(path, config_vector):
f = open(path, "w") f = open(path, "w")
json.dump(config_vector, f, ensure_ascii=False, indent=4) json.dump(config_vector, f, ensure_ascii=False, indent=4)
f.close() f.close()
return 0 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

View File

@ -10,15 +10,19 @@ ffmpeg_options = {
'options': '-vn' 'options': '-vn'
} }
token = get_token(config_path) config = {}
x = load_config(config_path, config)
async def determine_prefix(bot, message): if x == 1:
return get_prefix(config_path) print('Failed to load config, exiting')
exit(1)
token = config['guild']['token']
prefix = config['guild']['prefix']
intents = discord.Intents.default() intents = discord.Intents.default()
intents.members = True intents.members = True
intents.message_content = 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 @bot.event
async def on_ready(): async def on_ready():
@ -33,9 +37,8 @@ async def setprefix(ctx, *arg):
else: else:
prefix = arg[0] prefix = arg[0]
with open("prefix", "w") as f: bot.config['guild']['prefix'] = prefix
f.write(prefix) save_config(config_path, bot.config)
f.close()
await ctx.send("set prefix to: {0}".format(prefix)) await ctx.send("set prefix to: {0}".format(prefix))
@ -47,14 +50,26 @@ async def setrole(ctx, *arg: discord.Role):
else: else:
roleid = arg[0].id roleid = arg[0].id
with open("roleid", "w") as f: bot.config['guild']['roleid'] = roleid
f.write(str(roleid)) save_config(config_path, bot.config)
f.close()
await ctx.send("set followable role to {0}".format(arg[0])) await ctx.send("set playable role to {0}".format(arg[0]))
@bot.command() @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 <YouTube url>")
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 = { ydl_opts = {
'format': 'mp4', 'format': 'mp4',