-
Notifications
You must be signed in to change notification settings - Fork 0
/
dahna.py
134 lines (105 loc) · 3.96 KB
/
dahna.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
#A Discord bot written in Python
#https://realpython.com/how-to-make-a-discord-bot-python/
#https://youtu.be/nW8c7vT6Hl4
import asyncio
import discord
import youtube_dl
import random
from discord.ext import commands
#Suppress noise about console usage from errors
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'
}
client = commands.Bot(command_prefix = '$')
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
bitconnect_sounds = ['bitconnect1.mp3', 'bitconnect2.mp3', 'bitconnect3.mp3']
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=True):
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.event
async def on_ready():
await client.change_presence(activity=discord.Game('With yo mamma'))
print('DahnaBot is loaded.')
@client.event
async def on_member_join(member):
print(f'{member} has joined the server!')
@client.command()
async def hello(ctx):
await ctx.send(f'Hello {ctx.author.name}!')
@client.command()
async def join(ctx):
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(ctx.author.voice.channel)
await ctx.author.voice.channel.connect()
@client.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
@client.command()
async def ping(ctx):
await ctx.send(f'DahnaBots ping is {round(client.latency * 1000)}ms')
@client.command()
async def play(ctx, *, url):
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=None)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
@client.command()
async def d20(ctx):
roll = random.randint(1, 20)
await ctx.send(f'{ctx.author.name} threw a d20 and rolled a {roll}.')
@client.command()
async def d12(ctx):
roll = random.randint(1, 12)
await ctx.send(f'{ctx.author.name} threw a d12 and rolled a {roll}.')
@client.command()
async def d6(ctx):
roll = random.randint(1, 6)
await ctx.send(f'{ctx.author.name} threw a d6 and rolled a {roll}.')
@client.command()
async def taboo(ctx, *, message):
message_cop1 = message.upper()
message_cop2 = message_cop1
message_cop2 = message_cop2.replace("S","Z")
message_cop2 = message_cop2.replace("B","13")
message_cop2 = message_cop2.replace("I","1")
await ctx.send(f'{message_cop1} | {message_cop2}')
@client.command()
async def bitconnect(ctx):
if ctx.voice_client is not None:
await ctx.voice_client.move_to(ctx.author.voice.channel)
else:
await ctx.author.voice.channel.connect()
await ctx.voice_client.play(discord.FFmpegPCMAudio(random.choice(bitconnect_sounds)))
@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member, *, reason=None):
await ctx.send(f'Going to kick {member}.')
await member.kick(reason=reason)
await ctx.send(f'User {member} has been kicked.')
client.run('token')