-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathrkcodingmusic.py
119 lines (89 loc) · 4.07 KB
/
rkcodingmusic.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
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)
client = commands.Bot(command_prefix='?')
status = ['Jamming out to music!', 'Eating!', 'Sleeping!']
@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='play', help='This command plays music')
async def play(ctx, url):
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()
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=client.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('**Now playing:** {}'.format(player.title))
@client.command(name='stop', help='This command stops the music and makes the bot leave the voice channel')
async def stop(ctx):
voice_client = ctx.message.guild.voice_client
await voice_client.disconnect()
@tasks.loop(seconds=20)
async def change_status():
await client.change_presence(activity=discord.Game(choice(status)))
client.run('token')