-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_link.py
180 lines (128 loc) · 4.02 KB
/
discord_link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# discountGroovy.py
import os
import asyncio
import discord
#from dotenv import load_dotenv
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from youtubesearchpython import VideosSearch
import time
import youtube_dl
import ffmpeg
import re
#load_dotenv()
TOKEN = 'enter token here'
bot = commands.Bot(command_prefix='?')
music_queue = []
async def music_player(ctx):
while len(music_queue) >= 0:
voice = get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing() is True:
await asyncio.sleep(10)
continue
elif len(music_queue) == 0:
await dc(ctx)
return
curr_ctx = ctx
url = music_queue.pop(0)
if url == "something to block":
await ctx.send("Whilst i do agree, no")
await asyncio.sleep(10)
continue
# Play the youtube audio, taken from stackoverflow
ydl_opts = {
'format': 'worst',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
# Checking to make sure that the video is under 10 minutes in length
dictMeta = ydl.extract_info(url, download=False)
vid_length = dictMeta['duration']
if vid_length > 600:
msg = 'what u think u doing eh?'
await ctx.send(msg)
return
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio("song.mp3"))
voice.volume = 75
voice.is_playing()
msg = "Now playing " + url
await ctx.send(msg)
await asyncio.sleep(10)
@bot.command()
async def skip(ctx):
if len(music_queue) == 0:
await ctx.send("No songs in the queue")
else:
voice = get(bot.voice_clients, guild=ctx.guild)
voice.stop()
await music_player(ctx)
@bot.command()
async def play(ctx, *args):
url = args[0]
# Checking if the url is a youtube link, if not then use youtubesearchpython
if not re.match(r'.*www.youtube.*', url):
# Join list into args
url = ' '.join(args)
tempSearch = VideosSearch(url, limit = 1)
tempURL = tempSearch.result()['result'][0]['link']
url = tempURL
# Connect the bot to the channel if it is not already
if ctx.guild.voice_client not in bot.voice_clients:
channel = ctx.author.voice.channel
await channel.connect()
# Add the url to the music queue
music_queue.append(url)
msg = "Added " + url + " to the queue"
if len(music_queue) == 0:
await ctx.send("Queue is empty")
return
await ctx.send(msg)
await music_player(ctx)
def start_playing(voice_client, player):
music_queue[0] = player
i = 0
while i < len(music_queue):
try:
voice_client.play(music_queue[i], after=lambda e: print('Player error: %s' % e) if e else None)
except:
pass
i += 1
@bot.command()
async def stop(ctx):
msg = "I really should stop playing shouldn't I?"
voice = get(bot.voice_clients, guild=ctx.guild)
voice.stop()
music_queue.clear()
await ctx.send(msg)
@bot.command()
async def pause(ctx):
msg = "Pausing"
await ctx.send(msg)
voice = get(bot.voice_clients, guild=ctx.guild)
voice.pause()
@bot.command()
async def resume(ctx):
msg = "Resuming"
voice = get(bot.voice_clients, guild=ctx.guild)
voice.resume()
await ctx.send(msg)
@bot.command()
async def dc(ctx):
voice = get(bot.voice_clients, guild=ctx.guild)
await voice.disconnect()
music_queue.clear()
msg = "Bye Bye!"
await ctx.send(msg)
@bot.command()
async def queue(ctx):
await ctx.send(music_queue)
bot.run(TOKEN)