diff --git a/Fun.py b/Fun.py index 175e242..6e31239 100644 --- a/Fun.py +++ b/Fun.py @@ -15,6 +15,15 @@ class Fun: ctx.message.author.mention) await self.client.say(message) + async def on_message(self,message): + if message.author == self.client.user: + return + # if the user id posts a message in chat then it executes code + if message.author.id == "146032122774159360" and message.server.id == "505113732456775690": + await self.client.add_reaction(message, "👺") + # if a message contains "boost" in any place and the server id is the one specified it will send a message + if "boost" in message.content and message.server.id == "497091157591982081": + await self.client.send_message(message.channel, "https://gyazo.com/2a785b68d62386ad3566f671c81e532c.png") def setup(client): client.add_cog(Fun(client)) \ No newline at end of file diff --git a/Music.py b/Music.py index 8291be7..b733031 100644 --- a/Music.py +++ b/Music.py @@ -5,7 +5,7 @@ from discord.ext import commands # functions ran on it e.g. pause/resume players = {} queues = {} -voice_client={} +voice_client = {} class Music: @@ -13,13 +13,19 @@ class Music: self.client = client def check_queue(self, id): + # try is for key errors if a queue doesn't exist try: + # if the queue is empty disconnect if not queues[id]: voice = voice_client[id] + # disconnects the voice is written like this since co-routines don't run in afters so you have to do + # it in a specific way discord.compat.run_coroutine_threadsafe(voice.disconnect(), self.client.loop) - elif queues[id]: + if queues[id]: + players[id].stop() song = queues[id][0] + # supposed to send a message with info of the next song but it doesn't work consistently yet discord.compat.run_coroutine_threadsafe(self.client.say( "Now playing {} by {} ,time:{}mins".format(song.title, song.uploader, round(song.duration / 60), 2)), @@ -28,7 +34,9 @@ class Music: players[id] = player player.start() except: - voice=voice_client[id] + # disconnects the voice is written like this since co-routines don't run in afters so you have to do it in a + # specific way + voice = voice_client[id] discord.compat.run_coroutine_threadsafe(voice.disconnect(), self.client.loop) @@ -36,20 +44,30 @@ class Music: brief="plays youtube audio", pass_context=True) async def play(self, ctx, *url: str): - url = "{}".format(" ".join(url)) """creates a new voice client session, coverts a youtube video into a playable format and plays it, - if a player exists it creats a queue """ + if a player exists it creates a queue """ + # appends arguments into one string so it can have spaces in the search + url = "{}".format(" ".join(url)) + # sets a voice client = to the one currently in the voice channel so it can be parsed ,if it doesn't exist it + # will be null voice = self.client.voice_client_in(ctx.message.server) + # puts the voice client in a dictionary so the context object can be used outside the command specifically + # for disconnecting after the steam is over voice_client[ctx.message.server.id] = voice + # options for the youtube dl player opts = { 'default_search': 'auto', 'quiet': True, 'skip_download': True } + + # will go through try if a voice client exists already try: if voice.is_connected(): - player = await voice.create_ytdl_player(url, ytdl_options=opts, after=lambda: self.check_queue(ctx.message.server.id)) - + player = await voice.create_ytdl_player(url, ytdl_options=opts + ,after=lambda: self.check_queue(ctx.message.server.id) + ) + # adding players to a queue or creating a queue depending on if one already exists or not if ctx.message.server.id in queues: queues[ctx.message.server.id].append(player) else: @@ -100,24 +118,27 @@ class Music: """resumes the servers player getting the playing from the players dictionary""" players[ctx.message.server.id].resume() + ############################################################################################ + @commands.command(description="skips to the next song in queue", brief="skips song", pass_context=True) async def skip(self, ctx): + """performs the check_queue function that happens after a song is ended due to this it's buggy as hell as the + after for the players trigger as well""" voice = self.client.voice_client_in(ctx.message.server) players[ctx.message.server.id].stop() self.check_queue(ctx.message.server.id) - @commands.command(description="", - brief="", + @commands.command(description="returns the current players youtube url", + brief="gets the url of the video playing", pass_context=True) async def url(self,ctx): - player=players[ctx.message.server.id] + """supposed to get the current url of the current player but it just lies to me""" + player = players[ctx.message.server.id] await self.client.say(player.url) - ############################################################################################ - - +# adding the music cog def setup(client): client.add_cog(Music(client)) \ No newline at end of file diff --git a/discordBot.py b/discordBot.py index 6929f69..2ebde8e 100644 --- a/discordBot.py +++ b/discordBot.py @@ -7,12 +7,12 @@ import youtube_dl import random import requests from lxml import html +from discord.utils import get BOT_PREFIX = ("?") # token would be where you put your bot's token to join as the client but it's going on github so nah -TOKEN = "ommited" - +TOKEN = "" client = commands.Bot(command_prefix=BOT_PREFIX) extensions=["Fun","Music"] @@ -126,14 +126,45 @@ async def roll(ctx,dice): await client.say("{0} you rolled a: {1}".format(ctx.message.author.mention, dicedict[dice])) +@client.command(description="randomly picks a winner from the user list", + brief="randomly picks a winner from the user list", + pass_context=True) +async def giveaway(ctx): + memberlist=[] + for member in ctx.message.server.members: + for role in member.roles: + if str(member.status) == "online" and role.name == "User": + memberlist.append(member) + await client.say("{} has won the giveaway".format(random.choice(memberlist).mention)) + +# used guide https://www.youtube.com/watch?v=gxKM6J5VmIc&t=708s +############################################################################################ +@client.command(brief="loads command categories back") +async def load(extension): + try: + client.load_extension(extension) + except Exception as error: + print("{} cannot be loaded [{}] ".format(extension, error)) + + +@client.command(brief="unloads command categories") +async def unload(extension): + try: + client.unload_extension(extension) + except Exception as error: + print("{} cannot be unloaded [{}] ".format(extension, error)) + +############################################################################################ + + @client.event async def on_ready(): # says that the bot is playing a game called name - await client.change_presence(game=Game(name="Gnot a gnoblin")) + await client.change_presence(game=Game(name="Gnot a gnoblin|?help")) # prints logged in in the terminal for the code runner to know that they logged on print("Logged in as " + client.user.name) -if __name__=="__main__": +if __name__ == "__main__": for extension in extensions: try: client.load_extension(extension) @@ -143,14 +174,9 @@ if __name__=="__main__": @client.event async def on_message(message): + # makes it so the bot doesn't reply to itself if message.author == client.user: return - # if the user id posts a message in chat then it executes code - #if message.author.id == "146032122774159360" or message.author.id == "95549859188187136" or message.author.id == "180022945379385344": - #await client.add_reaction(message, "👺") - # if a message contains "boost" in any place it will send a message - if "boost" in message.content: - await client.send_message(message.channel, "https://gyazo.com/2a785b68d62386ad3566f671c81e532c") # overwrites the on_message so that it can listen for bot commands await client.process_commands(message)