Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import discord
- from discord.ext import commands
- import aiomysql
- if not discord.opus.is_loaded():
- # the 'opus' library here is opus.dll on windows
- # or libopus.so on linux in the current directory
- # you should replace this with the location the
- # opus library is located in and with the proper filename.
- # note that on windows this DLL is automatically provided for you
- discord.opus.load_opus('opus')
- class VoiceEntry:
- def __init__(self, message, player):
- self.requester = message.author
- self.channel = message.channel
- self.player = player
- def __str__(self):
- fmt = '*{0.title}* uploaded by {0.uploader} and requested by {1.display_name}'
- duration = self.player.duration
- if duration:
- fmt = fmt + ' [length: {0[0]}m {0[1]}s]'.format(divmod(duration, 60))
- return fmt.format(self.player, self.requester)
- class VoiceState:
- def __init__(self, bot):
- self.current = None
- self.voice = None
- self.bot = bot
- self.play_next_song = asyncio.Event()
- self.songs = asyncio.Queue()
- self.skip_votes = set() # a set of user_ids that voted
- self.audio_player = self.bot.loop.create_task(self.audio_player_task())
- def is_playing(self):
- if self.voice is None or self.current is None:
- return False
- player = self.current.player
- return not player.is_done()
- @property
- def player(self):
- return self.current.player
- def skip(self):
- self.skip_votes.clear()
- if self.is_playing():
- self.player.stop()
- def toggle_next(self):
- self.bot.loop.call_soon_threadsafe(self.play_next_song.set)
- async def audio_player_task(self):
- while True:
- self.play_next_song.clear()
- self.current = await self.songs.get()
- await self.bot.send_message(self.current.channel, 'Now playing ' + str(self.current))
- self.current.player.start()
- await self.play_next_song.wait()
- class Music:
- """Voice related commands.
- Works in multiple servers at once.
- """
- def __init__(self, bot):
- self.bot = bot
- self.voice_states = {}
- def decodel(self, text):
- encoder = text
- encoder = encoder.replace("C",",")
- encoder = encoder.replace("L","[")
- encoder = encoder.replace("R","]")
- encoder = encoder.replace("S"," ")
- encoder = encoder.replace("Q","'")
- return encoder
- def get_voice_state(self, server):
- state = self.voice_states.get(server.id)
- if state is None:
- state = VoiceState(self.bot)
- self.voice_states[server.id] = state
- return state
- async def create_voice_client(self, channel):
- voice = await self.bot.join_voice_channel(channel)
- state = self.get_voice_state(channel.server)
- state.voice = voice
- def __unload(self):
- for state in self.voice_states.values():
- try:
- state.audio_player.cancel()
- if state.voice:
- self.bot.loop.create_task(state.voice.disconnect())
- except:
- pass
- @commands.command(pass_context=True, no_pm=True)
- async def join(self, ctx, *, channel : discord.Channel):
- """Joins a voice channel."""
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- try:
- await self.create_voice_client(channel)
- except discord.ClientException:
- await self.bot.say('Already in a voice channel...')
- except discord.InvalidArgument:
- await self.bot.say('This is not a voice channel...')
- else:
- await self.bot.say('Ready to play audio in ' + channel.name)
- @commands.command(pass_context=True, no_pm=True)
- async def summon(self, ctx):
- """Summons the bot to join your voice channel."""
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- summoned_channel = ctx.message.author.voice_channel
- if summoned_channel is None:
- await self.bot.say('You are not in a voice channel.')
- return False
- state = self.get_voice_state(ctx.message.server)
- if state.voice is None:
- state.voice = await self.bot.join_voice_channel(summoned_channel)
- else:
- await state.voice.move_to(summoned_channel)
- return True
- @commands.command(pass_context=True, no_pm=True)
- async def play(self, ctx, *, song : str):
- """Plays a song.
- If there is a song currently in the queue, then it is
- queued until the next song is done playing.
- This command automatically searches as well from YouTube.
- The list of supported sites can be found here:
- https://rg3.github.io/youtube-dl/supportedsites.html
- """
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- print(rl)
- print(rl[0])
- cont = msg.author.id in rl
- print(cont)
- f = False
- if cont == f:
- await self.bot.say("You are not allowed to use this music feature")
- else:
- opts = {
- 'default_search': 'auto',
- 'quiet': True,
- }
- if state.voice is None:
- success = await ctx.invoke(self.summon)
- if not success:
- return
- try:
- player = await state.voice.create_ytdl_player(song, ytdl_options=opts, after=state.toggle_next)
- except Exception as e:
- fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
- await self.bot.send_message(ctx.message.channel, fmt.format(type(e).__name__, e))
- else:
- vol_check = "SELECT defvol FROM servers WHERE id=" + msg.server.id
- vol_exec = await cursor.execute(vol_check)
- if vol_exec == 0:
- defvol = 30
- else:
- r = await cursor.fetchone()
- defvol = int(r[0])
- player.volume = defvol / 100
- entry = VoiceEntry(ctx.message, player)
- await self.bot.say('Enqueued ' + str(entry))
- await state.songs.put(entry)
- @commands.command(pass_context=True, no_pm=True)
- async def volume(self, ctx, value : int):
- """Sets the volume of the currently playing song."""
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- state = self.get_voice_state(ctx.message.server)
- if state.is_playing():
- player = state.player
- player.volume = value / 100
- await self.bot.say('Set the volume to {:.0%}'.format(player.volume))
- @commands.command(pass_context=True, no_pm=True)
- async def pause(self, ctx):
- """Pauses the currently played song."""
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- state = self.get_voice_state(ctx.message.server)
- if state.is_playing():
- player = state.player
- player.pause()
- @commands.command(pass_context=True, no_pm=True)
- async def resume(self, ctx):
- """Resumes the currently played song."""
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- state = self.get_voice_state(ctx.message.server)
- if state.is_playing():
- player = state.player
- player.resume()
- @commands.command(pass_context=True, no_pm=True)
- async def stop(self, ctx):
- """Stops playing audio and leaves the voice channel.
- This also clears the queue.
- """
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- server = ctx.message.server
- state = self.get_voice_state(server)
- if state.is_playing():
- player = state.player
- player.stop()
- try:
- state.audio_player.cancel()
- del self.voice_states[server.id]
- await state.voice.disconnect()
- except:
- pass
- @commands.command(pass_context=True, no_pm=True)
- async def skip(self, ctx):
- """Vote to skip a song. The song requester can automatically skip.
- 3 skip votes are needed for the song to be skipped.
- """
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- state = self.get_voice_state(ctx.message.server)
- if not state.is_playing():
- await self.bot.say('Not playing any music right now...')
- return
- voter = ctx.message.author
- if voter == state.current.requester:
- await self.bot.say('Requester requested skipping song...')
- state.skip()
- elif voter.id not in state.skip_votes:
- state.skip_votes.add(voter.id)
- total_votes = len(state.skip_votes)
- if total_votes >= 3:
- await self.bot.say('Skip vote passed, skipping song...')
- state.skip()
- else:
- await self.bot.say('Skip vote added, currently at [{}/3]'.format(total_votes))
- else:
- await self.bot.say('You have already voted to skip this song.')
- @commands.command(pass_context=True, no_pm=True)
- async def playing(self, ctx):
- """Shows info about the currently played song."""
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- state = self.get_voice_state(ctx.message.server)
- if mal_exec == 0:
- await self.bot.say("Your server aint whitelisted boiii")
- return
- else:
- r = await cursor.fetchone()
- r = str(r[0])
- r = self.decodel(r)
- rl = eval(r)
- if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
- await self.bot.say("You are not allowed to use the music feature")
- return
- state = self.get_voice_state(ctx.message.server)
- if state.current is None:
- await self.bot.say('Not playing anything.')
- else:
- skip_count = len(state.skip_votes)
- await self.bot.say('Now playing {} [skips: {}/3]'.format(state.current, skip_count))
- @commands.command(pass_context=True, no_pm=True)
- async def defvolume(self, ctx, value : int):
- """Sets the default volume"""
- defvol = value
- msg = ctx.message
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- vol_check = "SELECT defvol FROM servers WHERE id=" + msg.server.id
- vol_exec = await cursor.execute(vol_check)
- if vol_exec == 0:
- await self.bot.say('Your server aint in the whitelist for settings m8')
- else:
- if msg.author == msg.server.owner or msg.author.id == "152810994933039104":
- update_vol = "UPDATE servers SET defvol=" + str(defvol) + " WHERE id=" + msg.server.id + ";"
- warked = await cursor.execute(update_vol)
- if warked == 1:
- await self.bot.say("Set the default volume to " + str(defvol) + "%")
- else:
- await self.bot.say("idk wtf is going on")
- else:
- await self.bot.say("You are not the server owner!")
- @commands.command(pass_context=True, no_pm=True)
- async def allowuser(self, ctx):
- """Allow a user to use music"""
- conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
- async with conn.cursor() as cursor:
- msg = ctx.message
- if msg.author == msg.server.owner or msg.author.id == "152810994933039104":
- mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
- mal_exec = await cursor.execute(mal_check)
- if mal_exec == 1:
- rt = await cursor.fetchone()
- rt = self.decodel(rt[0])
- r = eval(str(rt))
- if msg.mentions:
- if not msg.mentions[0].id in r:
- r.append(msg.mentions[0].id)
- encoder = str(r)
- encoder = encoder.replace(",","C")
- encoder = encoder.replace("[","L")
- encoder = encoder.replace("]","R")
- encoder = encoder.replace(" ","S")
- encoder = encoder.replace("'","Q")
- print(encoder)
- update_list = "UPDATE servers SET mallowed='{}' WHERE id='{}';".format(encoder, msg.server.id)
- print(update_list)
- warked = await cursor.execute(update_list)
- if warked == 1:
- await self.bot.say("Added " + msg.mentions[0].name + " to the allowed list")
- else:
- await self.bot.say("That user is already allowed!")
- else:
- await self.bot.say("You didnt mention anyone")
- bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), description='Gedyys Beta Bot')
- loop = asyncio.get_event_loop()
- bot.add_cog(Music(bot))
- @bot.event
- async def on_ready():
- print('Logged in as:\n{0} (ID: {0.id})'.format(bot.user))
- bot.run('MjcyODQwODI3MzI2NDMxMjMy.C3PwqQ.RJPtUcCBBeF_q2YMSXG6N-xkEB0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement