add basic functionality mirrored from arthurbot

This commit is contained in:
Arthur Lu
2022-08-22 19:20:11 +00:00
parent 4e2074c975
commit 5db11730ac
5 changed files with 302 additions and 0 deletions

62
src/config.py Normal file
View File

@@ -0,0 +1,62 @@
import json
sample_json = """
{
"guild":{
"token": "",
"prefix": "$",
"roleid": 0,
"username": "music-bot"
},
}
"""
def load_config(path, config_vector):
try:
f = open(path, "r")
config_vector.update(json.load(f))
f.close()
return 0
except:
f = open(path, "w")
f.write(sample_json)
f.close()
return 1
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

96
src/musicbot.py Normal file
View File

@@ -0,0 +1,96 @@
from asyncio import sleep
import discord
from discord.ext import commands
import yt_dlp
from config import *
config_path = "config.json"
ffmpeg_options = {
'options': '-vn'
}
token = get_token(config_path)
async def determine_prefix(bot, message):
return get_prefix(config_path)
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix = determine_prefix, description='very cool', intents = intents)
@bot.event
async def on_ready():
print('Logged in as {0} ({0.id})'.format(bot.user))
print('------')
@bot.command()
async def setprefix(ctx, *arg):
if(len(arg) != 1):
await ctx.send("usage: setprefix <prefix>")
else:
prefix = arg[0]
with open("prefix", "w") as f:
f.write(prefix)
f.close()
await ctx.send("set prefix to: {0}".format(prefix))
@bot.command()
async def setrole(ctx, *arg: discord.Role):
if(len(arg) != 1):
await ctx.send("usage: setrole @<rolename>")
else:
roleid = arg[0].id
with open("roleid", "w") as f:
f.write(str(roleid))
f.close()
await ctx.send("set followable role to {0}".format(arg[0]))
@bot.command()
async def playmusic(ctx, *arg):
ydl_opts = {
'format': 'mp4',
'quiet': True,
'paths': {
'home': './session/'
},
'outtmpl': {
'default': '%(autonumber)s.%(ext)s',
},
'postprocessors': [{
'key': 'FFmpegExtractAudio',
}],
}
try:
await ctx.voice_client.disconnect()
except:
pass
url = arg[0]
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
duration = info.get('duration')
name = info.get('title')
if duration < 1200:
await ctx.send("downloading music requested: {0}".format(name))
ydl.download([url])
audio = "session/00001.m4a"
await ctx.author.voice.channel.connect()
ctx.voice_client.play(discord.FFmpegPCMAudio(audio), after=lambda e: print('Player error: %s' % e) if e else None)
while ctx.voice_client.is_playing():
await sleep(0.01)
await ctx.voice_client.disconnect()
else:
await ctx.send("music requested was too long ({0} > 1200)".format(duration))
bot.run(token)