-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
298 lines (256 loc) · 11 KB
/
main.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
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
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
import asyncio
import yt_dlp as ytdl
load_dotenv()
TOKEN = os.getenv('discord_token')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
ytdl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': 'data/%(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',
}
ffmpeg_options = {
'options': '-vn',
}
ytdl = ytdl.YoutubeDL(ytdl_format_options)
bot = commands.Bot(
command_prefix=commands.when_mentioned_or("/"),
description='Simple music bot',
intents=intents,
)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume = 0.2):
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:
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
def getResponse(rawUserInput):
userInput = rawUserInput.lower()
if userInput == '':
return 'Well, you are awfully silent...'
elif 'help' in userInput:
return 'Commands: To receive a private message, write a DM at the beginning of the sentence.'
elif 'hello' in userInput:
return 'Hello there'
elif 'obi-wan' in userInput or 'obiwan' in userInput or 'obi wan kenobi' in userInput:
return 'I will do what I must.'
elif 'you were the chosen one' in userInput or 'it was said that you would destroy the sith, not join them!' in userInput or 'bring balance to the force, not leave it in darkness!' in userInput:
return 'I HATE YOU!!!!'
elif 'i hate you' in userInput:
return 'You were my brother anakin. I loved you'
else:
return 'Do or do not. There is no try'
async def sendMessage(message, rawUserInput) -> None:
if not rawUserInput:
print('Message was empty because intents were not enabled')
return
isPrivate = rawUserInput[0] == 'dm'
if isPrivate:
rawUserInput = rawUserInput[1:]
try:
response = getResponse(rawUserInput)
await message.author.send(response) if isPrivate else await message.channel.send(response)
except Exception as e:
print(e)
class MyClient(discord.Client):
async def on_ready(self) -> None:
print(f'{client.user} is connected to the following guilds:')
for guild in client.guilds:
print(f'{guild.name} (id:{guild.id})')
async def on_message(self, message) -> None:
if message.content.startswith('-'):
if message.author == client.user:
return
else:
username = message.author
rawUserInput = message.content
channel = message.channel
print(f'[{channel}] [{username}]: "{rawUserInput}"')
await sendMessage(message, rawUserInput)
async def on_member_join(self, member):
channel = member.guild.system_channel
if channel is not None:
msg = f'**Did you hear that? {member.mention}**'
await channel.send(msg)
async def on_member_remove(self, member):
channel = member.guild.system_channel
if channel is not None:
msg = f'**Farewell {member.mention}. May the force be with you**'
await channel.send(msg)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.role_message_id = 1237450786196160613
self.emoji_to_role = {
discord.PartialEmoji(name='jedi_master', id=1237434764193959936): 1237415351462006866,
discord.PartialEmoji(name='one_ring', id=1237442284878954599): 1237416413015179478,
discord.PartialEmoji(name='auror', id=1237444023942123582): 1237417424618524683,
}
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
# print(payload.emoji)
if payload.message_id != self.role_message_id:
return
guild = self.get_guild(payload.guild_id)
if guild is None:
return
try:
roleID = self.emoji_to_role[payload.emoji]
except KeyError:
return
role = guild.get_role(roleID)
if role is None:
return
try:
await payload.member.add_roles(role)
except discord.HTTPException as de:
print(f'An error occurred! {de}')
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
if payload.message_id != self.role_message_id:
return
guild = self.get_guild(payload.guild_id)
if guild is None:
return
try:
roleID = self.emoji_to_role[payload.emoji]
except KeyError:
return
role = guild.get_role(roleID)
if role is None:
return
member = guild.get_member(payload.user_id)
if member is None:
return
try:
await member.remove_roles(role)
except discord.HTTPException as de:
print(f'An error occurred! {de}')
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.queue = []
self.current_song = None
async def play_next(self, ctx):
musician = ctx.voice_client
if len(self.queue) > 0:
self.current_song = self.queue.pop(0)
musician.play(self.current_song, after=lambda e: print(f'Player error: {e}') if e else None)
embed_msg = discord.Embed(description=f'Şimdi oynatılıyor **{self.current_song.title}**', color=discord.Color.blue())
await ctx.send(embed = embed_msg)
else:
self.current_song = None
embed_msg = discord.Embed(description=f'Sıra da herhangi bir müzik yok. **/rplay** ile müzik ekleyebilirsiniz.', color=discord.Color.blue())
await ctx.send(embed = embed_msg)
@commands.command()
async def join(self, ctx, *, channel: discord.VoiceChannel):
"""Bot belirtilen ses kanalına katılır"""
musician = ctx.voice_client
if musician is not None:
return await musician.move_to(channel)
await channel.connect()
@commands.command()
async def localplay(self, ctx, *, query):
"""Bilgisayar da yüklü olan bir dosyayı oynatır"""
musician = ctx.voice_client
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query))
musician.play(source, after=lambda e: print(f'Player error: {e}') if e else None)
await ctx.send(f'**Şimdi oynatılıyor:** {query}')
@commands.command()
async def rplay(self, ctx, *, url):
"""Link ile istenilen müzikleri oynatır"""
musician = ctx.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop)
self.queue.append(player)
if musician.is_playing() or musician.is_paused():
embed_msg = discord.Embed(description=f'Sıraya eklendi **{player.title}**', color=discord.Color.blue())
await ctx.send(embed = embed_msg)
else:
await self.play_next(ctx)
@commands.command()
async def rskip(self, ctx):
"""Bir sonraki müziğe geçer."""
musician = ctx.voice_client
if musician.is_playing():
musician.stop()
await self.play_next(ctx)
@commands.command()
async def rstream(self, ctx, *, url):
"""Link ile istenilen müzikleri yayınlar"""
musician = ctx.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
musician.play(player, after=lambda e: print(f'Player error: {e}') if e else None)
embed_msg = discord.Embed(description=f'Şimdi oynatılıyor **{player.title}**', color=discord.Color.blue())
await ctx.send(embed = embed_msg)
@commands.command()
async def rvolume(self, ctx, volume: int):
"""Oynatıcının ses seviyesini ayarlar"""
musician = ctx.voice_client
if musician is None:
embed_msg = discord.Embed(description='Oynatıcı bir **ses kanalına** bağlı değil!', color=discord.Color.red())
return await ctx.send(embed = embed_msg)
musician.source.volume = volume / 100
embed_msg = discord.Embed(description=f'Ses seviyesi **%{volume}** olarak değiştirildi', color=discord.Color.random())
await ctx.send(embed = embed_msg)
@commands.command()
async def rpause(self, ctx):
"""Bot çalmayı geçici olarak durdurur"""
musician = ctx.voice_client
if musician.is_playing():
musician.pause()
embed_msg = discord.Embed(description='Oynatıcı duraklatıldı', color=discord.Color.red())
await ctx.send(embed = embed_msg)
else:
musician.resume()
embed_msg = discord.Embed(description='Oynatıcı devam ediyor', color=discord.Color.blue())
await ctx.send(embed = embed_msg)
@commands.command()
async def rstop(self, ctx):
"""Bot çalmayı durdurur ve ses kanalından çıkar """
musician = ctx.voice_client
musician.stop()
self.queue = []
await musician.disconnect()
embed_msg = discord.Embed(description='Oynatıcı durduruldu', color=discord.Color.red())
await ctx.send(embed = embed_msg)
@localplay.before_invoke
@rplay.before_invoke
@rstream.before_invoke
async def ensure_voice(self, ctx):
musician = ctx.voice_client
if musician is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
embed_msg = discord.Embed(description='Önce bir **ses kanalına** bağlı olmanız gerekiyor!', color=discord.Color.red())
await ctx.send(embed = embed_msg)
raise commands.CommandError('Author not connected to a voice channel.')
client = MyClient(intents=intents)
async def main() -> None:
async with bot:
await bot.add_cog(Music(bot))
await bot.start(TOKEN)
client.run(TOKEN)
if __name__ == '__main__':
main()