-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
324 lines (261 loc) · 9.4 KB
/
main.py
File metadata and controls
324 lines (261 loc) · 9.4 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# to add bot to your server click here : https://discord.com/oauth2/authorize?client_id=730602425807011847&permissions=8&scope=bot
import os
import re
import json
import math
import discord
from discord.ext import commands
# user Libraries
from music_q import Q
from secret import discord_token
import scraping
import spotify
from utils import play_song_function
my_secret = discord_token
# Discord added this as an extra permission to allow retrieving members related data
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='-', intents = intents)
que = Q()
# Template function kept from a tutorial
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]["q"] + " - " + json_data[0]["a"]
return quote
@bot.event
async def on_ready():
print(" We have logged in as {0.user}".format(bot))
@bot.command(aliases=['j'])
async def join(ctx):
# Connect directly
try:
vc = ctx.author.voice.channel
await vc.connect()
# Dont do anything if error
except AttributeError:
return
# Disconnect then connect to new channel
except Exception as e:
print(e)
await ctx.voice_client.disconnect()
vc = ctx.author.voice.channel
await vc.connect()
@bot.command(aliases=['dc', 'die', 'disconnect'])
async def leave(ctx, arg = "y"):
que.clear_que(ctx,arg)
try:
await ctx.voice_client.disconnect()
except:
return
@bot.command(aliases=['s'])
async def save(ctx):
que.save_data(ctx)
@bot.command()
async def link(ctx):
await ctx.send(f'>>> Youtube link : {que.nowplaying(ctx, "YT-video")}')
@bot.command(aliases=['l'])
async def loop(ctx):
if que.loop_switch(ctx):
await ctx.send(f">>> Now looping Queue")
else:
await ctx.send(f">>> Queue Looping disabled")
@bot.command(aliases=['sh'])
async def shuffle(ctx):
if que.shuffle_switch(ctx):
await ctx.send(f">>> Now shuffling Queue")
else:
await ctx.send(f">>> Queue shuffling disabled")
@bot.command(aliases=['t'])
async def test(ctx, *, arg):
await ctx.send(arg)
@bot.command(aliases=['jm'])
async def jump(ctx, arg:int):
que.jump(ctx, (arg - 1))
vcclient = ctx.voice_client
if vcclient.is_playing():
vcclient.stop()
play_song_function(ctx, discord, que)
await ctx.send(f">>> \nNow Playing: \n{que.index[ctx.guild] + 1}.{que.nowplaying(ctx)} [{que.nowplaying(ctx, 'user')}] \n.")
@bot.command(aliases=['p'])
async def play(ctx, *arg):
http_check = re.search('http', arg[0])
if not http_check:
arg = " ".join(arg)
STARTPOINT = 0
ENDPOINT = None
else:
if len(arg) >= 3:
STARTPOINT = int(arg[-2])
ENDPOINT = int(arg[-1])
arg = " ".join(arg[0:-2])
else:
STARTPOINT = 0
ENDPOINT = None
arg = " ".join(arg)
vc = ctx.author.voice.channel
try:
await vc.connect()
except AttributeError: # Dont do anything if error
print("AttributeError")
except Exception as e:
print(e)
try:
SONG_ADD_POSITION = len(que.guild[ctx.guild])
except:
SONG_ADD_POSITION = 0
await que.add_entry(ctx, arg, STARTPOINT, ENDPOINT, SONG_ADD_POSITION)
return
@bot.command(aliases=['pn'])
async def playnext(ctx, *arg):
http_check = re.search('http', arg[0])
if not http_check:
arg = " ".join(arg)
STARTPOINT = 0
ENDPOINT = None
else:
if len(arg) >= 3:
STARTPOINT = int(arg[-2])
ENDPOINT = int(arg[-1])
arg = " ".join(arg[0:-2])
else:
STARTPOINT = 0
ENDPOINT = -1
arg = " ".join(arg)
vc = ctx.author.voice.channel
try:
await vc.connect()
except AttributeError: # Dont do anything if error
print("AttributeError")
except Exception as e:
print(e)
try:
SONG_ADD_POSITION = que.index[ctx.guild] + 1
song, num_of_songs = await que.add_entry(ctx, arg, STARTPOINT, ENDPOINT, SONG_ADD_POSITION)
except:
await ctx.send(f">>> \nThere is no Queue to add to yet \n.")
@bot.command(aliases=['i'])
async def index(ctx):
await ctx.send(f">>> Current Song Index : {que.index[ctx.guild] + 1} | Looping: {que.loop[ctx.guild]} | Shuffle: {que.shuffle[ctx.guild]}")
@bot.command()
async def ping(ctx):
await ctx.send(f">>> {ctx.voice_client.average_latency * 1000} ms")
@bot.command(aliases=['np'])
async def nowplaying(ctx, *, arg = "name"):
if ctx.voice_client.is_playing():
await ctx.send(f">>> Now playing: \n{que.index[ctx.guild] + 1}. {que.nowplaying(ctx, arg)} [{que.nowplaying(ctx, 'user')}]")
else:
await ctx.send(f">>> No song playing rn")
@bot.command(aliases=['n', 'skip'])
async def next(ctx):
vcclient = ctx.voice_client
if vcclient.is_playing():
vcclient.stop()
play_song_function(ctx, discord, que)
# Using Now playing because next follows index
await ctx.send(f">>> \nNow Playing: \n{que.index[ctx.guild] + 1}.{que.nowplaying(ctx)} [{que.nowplaying(ctx, 'user')}] \n.")
@bot.command(aliases=[])
async def prev(ctx):
my_que = que.guild[ctx.guild][que.prev_track(ctx)]["url"]
vcclient = ctx.voice_client
if vcclient.is_playing():
vcclient.stop()
vcclient.play(discord.FFmpegPCMAudio(my_que))
vcclient.source = discord.PCMVolumeTransformer(vcclient.source)
vcclient.source.volume = 1
await ctx.send(f">>> Now Playing: \n{que.index[ctx.guild] + 1}.{que.nowplaying(ctx)} [{que.nowplaying(ctx, 'user')}] \n.")
@bot.command(aliases=['q'])
async def queue(ctx, page:int = 0):
page_size = 8
songs = len(que.guild[ctx.guild])
num_of_pages = [round(songs/page_size), round(songs/page_size) + 1][round(songs/page_size) < (songs/page_size)]
print(page)
if not page:
page = math.floor(que.index[ctx.guild] / 8) + 1 # default page
await ctx.send(f"```css\n.Page_{page}_of_{num_of_pages} | .Total_number_of_Songs: {songs}\
\n\n{que.my_que(ctx, page, page_size)}```")
@bot.command(aliases=['r', 'rm', 'del'])
async def remove(ctx, num: int):
try:
zero_adjusted_num = num - 1
await ctx.send(f"```Removed:\n{num}.{que.guild[ctx.guild][zero_adjusted_num]['name']}```")
que.delete_entry(ctx, zero_adjusted_num)
# In case we remove the now playing song
# Using the next function will should get us to index 0
if num == que.index[ctx.guild]:
que.index[ctx.guild] = que.index[ctx.guild] - 1
except ValueError:
await ctx.send(">>> Whoops, numbers only please!!")
return
except KeyError:
await ctx.send(">>> Whoops, nothing to clear!!")
except Exception as e:
print(e)
await ctx.send(">>> Whoops, something went wrong!!")
return
@bot.command(aliases=[])
async def clear(ctx, arg = 'y'):
que.clear_que(ctx, arg)
@bot.command(aliases=['m'])
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
print(member)
await member.edit(mute=True)
@bot.command(aliases=['um'])
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
@bot.command(aliases=['pa', 'pall'])
async def playall(ctx, arg, STARTPOINT = 0, ENDPOINT = None):
vc = ctx.author.voice.channel
await ctx.send(">>> Please wait ...")
try:
await vc.connect()
except AttributeError: # Dont do anything if error
print("AttributeError")
except Exception as e:
print(e)
try:
SONG_ADD_POSITION = len(que.guild[ctx.guild])
except:
SONG_ADD_POSITION = -1
song, num_of_songs = await que.add_entry(ctx, arg, STARTPOINT, ENDPOINT, SONG_ADD_POSITION)
# play_song_function(ctx, discord, que)
vcclient = ctx.voice_client
if not vcclient.is_playing():
# using add entry because we want entry song only, no need for index
await ctx.send(f">>> \nNow Playing: \n{num_of_songs} songs added by [{song['user'][0:-5]}] \n.")
else:
await ctx.send(f">>> \nAdded to Q: \n{num_of_songs} songs added by [{song['user'][0:-5]}] at index {len(que.guild[ctx.guild])}\n.")
@bot.command(aliases=['pna', 'pnal', 'pnall'])
async def playnextall(ctx, arg, STARTPOINT = 0, ENDPOINT = None):
vc = ctx.author.voice.channel
try:
await vc.connect()
except AttributeError: # Dont do anything if error
print("AttributeError")
except Exception as e:
print(e)
try:
SONG_ADD_POSITION = que.index[ctx.guild] + 1
song, num_of_songs = await que.add_entry(ctx, arg, STARTPOINT, ENDPOINT, SONG_ADD_POSITION)
await ctx.send(f">>> \nAdded to Q: \n{num_of_songs} songs added by [{song['user'][0:-5]}] at index {SONG_ADD_POSITION}\n.")
except:
await ctx.send(f">>> \nThere is no Queue to add to yet\n.")
@bot.command(aliases=[])
async def dj(ctx):
output = ["```CSS\nDJs for the current queue are:\n"]
dj = []
for entry in que.guild[ctx.guild]:
dj.append(entry['user'][0:-5])
user = set(dj)
bullets = 1
for name in user:
string = f"{bullets}. {name} added: {dj.count(name)} songs"
output.append(string)
bullets += 1
output.append('```')
await ctx.send("\n".join(output))
# async def history(ctx, arg = ctx.author):
bot.run(my_secret)