-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathrkcodingmusicbotfinal.py
More file actions
223 lines (168 loc) · 6.96 KB
/
rkcodingmusicbotfinal.py
File metadata and controls
223 lines (168 loc) · 6.96 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
import asyncio
import discord
from discord.ext import commands, tasks
from discord.voice_client import VoiceClient
import youtube_dl
from random import choice
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = data.get('url')
@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
def is_connected(ctx):
voice_client = ctx.message.guild.voice_client
return voice_client and voice_client.is_connected()
client = commands.Bot(command_prefix='?')
status = ['Jamming out to music!', 'Eating!', 'Sleeping!']
queue = []
loop = False
@client.event
async def on_ready():
change_status.start()
print('Bot is online!')
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='general')
await channel.send(f'Welcome {member.mention}! Ready to jam out? See `?help` command for details!')
@client.command(name='ping', help='This command returns the latency')
async def ping(ctx):
await ctx.send(f'**Pong!** Latency: {round(client.latency * 1000)}ms')
@client.command(name='hello', help='This command returns a random welcome message')
async def hello(ctx):
responses = ['***grumble*** Why did you wake me up?', 'Top of the morning to you lad!', 'Hello, how are you?', 'Hi', '**Wasssuup!**']
await ctx.send(choice(responses))
@client.command(name='die', help='This command returns a random last words')
async def die(ctx):
responses = ['why have you brought my short life to an end', 'i could have done so much more', 'i have a family, kill them instead']
await ctx.send(choice(responses))
@client.command(name='credits', help='This command returns the credits')
async def credits(ctx):
await ctx.send('Made by `RK Coding`')
await ctx.send('Thanks to `DiamondSlasher` for coming up with the idea')
await ctx.send('Thanks to `KingSticky` for helping with the `?die` and `?creditz` command')
@client.command(name='creditz', help='This command returns the TRUE credits')
async def creditz(ctx):
await ctx.send('**No one but me, lozer!**')
@client.command(name='join', help='This command makes the bot join the voice channel')
async def join(ctx):
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel")
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
@client.command(name='leave', help='This command stops the music and makes the bot leave the voice channel')
async def leave(ctx):
voice_client = ctx.message.guild.voice_client
await voice_client.disconnect()
@client.command(name='loop', help='This command toggles loop mode')
async def loop_(ctx):
global loop
if loop:
await ctx.send('Loop mode is now `False!`')
loop = False
else:
await ctx.send('Loop mode is now `True!`')
loop = True
@client.command(name='play', help='This command plays music')
async def play(ctx):
global queue
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel")
return
elif len(queue) == 0:
await ctx.send('Nothing in your queue! Use `?queue` to add a song!')
else:
try:
channel = ctx.message.author.voice.channel
await channel.connect()
except:
pass
server = ctx.message.guild
voice_channel = server.voice_client
while queue:
try:
while voice_channel.is_playing() or voice_channel.is_paused():
await asyncio.sleep(2)
pass
except AttributeError:
pass
try:
async with ctx.typing():
player = await YTDLSource.from_url(queue[0], loop=client.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
if loop:
queue.append(queue[0])
del(queue[0])
await ctx.send('**Now playing:** {}'.format(player.title))
except:
break
@client.command(name='volume', help='This command changes the bots volume')
async def volume(ctx, volume: int):
if ctx.voice_client is None:
return await ctx.send("Not connected to a voice channel.")
ctx.voice_client.source.volume = volume / 100
await ctx.send(f"Changed volume to {volume}%")
@client.command(name='pause', help='This command pauses the song')
async def pause(ctx):
server = ctx.message.guild
voice_channel = server.voice_client
voice_channel.pause()
@client.command(name='resume', help='This command resumes the song!')
async def resume(ctx):
server = ctx.message.guild
voice_channel = server.voice_client
voice_channel.resume()
@client.command(name='stop', help='This command stops the song!')
async def stop(ctx):
server = ctx.message.guild
voice_channel = server.voice_client
voice_channel.stop()
@client.command(name='queue')
async def queue_(ctx, *, url):
global queue
queue.append(url)
await ctx.send(f'`{url}` added to queue!')
@client.command(name='remove')
async def remove(ctx, number):
global queue
try:
del(queue[int(number)])
await ctx.send(f'Your queue is now `{queue}!`')
except:
await ctx.send('Your queue is either **empty** or the index is **out of range**')
@client.command(name='view', help='This command shows the queue')
async def view(ctx):
await ctx.send(f'Your queue is now `{queue}!`')
@tasks.loop(seconds=20)
async def change_status():
await client.change_presence(activity=discord.Game(choice(status)))
client.run('token')