Skip to content
Permalink
Browse files
changed things
  • Loading branch information
peacoc17 committed Nov 14, 2018
1 parent 032ea75 commit 8bc302e830195bfe18b9aab0a93ed53443fc0ea8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 23 deletions.
9 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))
@@ -5,21 +5,27 @@ from discord.ext import commands
# functions ran on it e.g. pause/resume
players = {}
queues = {}
voice_client={}
voice_client = {}


class Music:
def __init__(self, client):
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,28 +34,40 @@ 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)

@commands.command(description="takes a youtube url and joins the channel and plays it",
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))
@@ -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)

0 comments on commit 8bc302e

Please sign in to comment.