-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
378 lines (316 loc) · 12.2 KB
/
bot.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import sys
sys.path.append('..')
from discord.ext import commands
from discord import FFmpegPCMAudio, Embed
from discord import utils
import os
import json
import asyncio
import settings
import requests
from random import choice, uniform
from os import path
from os import listdir
from gtts import gTTS
import requests
import re
from tqdm import tqdm
last = None
def create_sentence_mapper():
os.makedirs('clips', exist_ok=True)
raw_sounds = requests.get('https://prophet-button.netlify.app/')
extracted_data = re.findall(r'src=\"(.*\.mp3)\".*\">([^\"<]*)<', raw_sounds.text)
sentence_mapper = dict()
for (filename, sentence) in tqdm(extracted_data):
sentence_mapper[' '.join(sentence.split()).strip()] = filename
r = requests.get(f'https://prophet-button.netlify.app/sound/{filename}')
with open(f'clips/{filename}', 'wb') as f:
f.write(r.content)
with open('sentence_mapper.json', 'w') as f:
json.dump(sentence_mapper, f)
def get_sentence_mapper():
global sentence_mapper
try:
with open('sentence_mapper.json', 'r') as f:
sentence_mapper = json.load(f)
except FileNotFoundError:
create_sentence_mapper()
get_sentence_mapper()
get_sentence_mapper()
bot = commands.Bot(command_prefix=['clip!', 'c!', '?'])
@bot.event
async def on_ready():
"""
First greeting when the bot comes up
"""
print("I'm ready to go!")
voice_client = None
@bot.command(aliases=["p"])
async def play(ctx, *args):
"""
Play sounds
Example: `?p สวัสดีครับ`
"""
global voice_client
if len(args) == 0 or ctx.author.voice is None:
if len(args) == 0:
print("No args, idiot.")
if ctx.author.voice is None:
print("Connect to voice, idiot.")
return
if args[0] == 'help':
text = ", ".join([f'`{x}`' for x in sentence_mapper.keys()])
embed = Embed(title='🧐Sasada Teaching Lession', description=text, color=0xae00ff)
await ctx.send(embed=embed)
return
sentence = ' '.join(args)
if sentence not in sentence_mapper.keys():
print("No this sentence in sentence_mapper")
await ctx.reply("Not this sentence in list")
return
filename = 'clips/' + sentence_mapper[sentence]
if not path.exists(filename):
print("No such file (" + sentence + ".mp3)")
await ctx.reply(sentence + " does not exists.")
return
global last
last = sentence_mapper[sentence ]
user_channel = ctx.author.voice.channel
if ctx.me.voice is None or ctx.me.voice.channel != user_channel:
voice_client = await user_channel.connect()
voice_client.play(FFmpegPCMAudio(source=filename))
while voice_client.is_playing():
await(asyncio.sleep(0.2))
@bot.command(aliases=["loop"])
async def play_loop(ctx, *args):
"""
Play sounds
Example: `?loop สวัสดีครับ`
"""
global voice_client
if len(args) == 0 or ctx.author.voice is None:
if len(args) == 0:
print("No args, idiot.")
if ctx.author.voice is None:
print("Connect to voice, idiot.")
return
if args[0] == 'help':
text = ", ".join([f'`{x}`' for x in sentence_mapper.keys()])
embed = Embed(title='🧐Sasada Teaching Lession', description=text, color=0xae00ff)
await ctx.send(embed=embed)
return
sentence = ' '.join(args)
if sentence not in sentence_mapper.keys():
print("No this sentence in sentence_mapper")
await ctx.reply("Not this sentence in list")
return
filename = 'clips/' + sentence_mapper[sentence]
if not path.exists(filename):
print("No such file (" + sentence + ".mp3)")
await ctx.reply(sentence + " does not exists.")
return
global last
last = sentence_mapper[sentence ]
user_channel = ctx.author.voice.channel
if ctx.me.voice is None or ctx.me.voice.channel != user_channel:
voice_client = await user_channel.connect()
while True:
voice_client.play(FFmpegPCMAudio(source=filename))
while voice_client.is_playing():
await(asyncio.sleep(0.2))
@bot.command(aliases=["ls"])
async def get_existing_sentences(ctx):
"""
List all recorded sounds that can be played
Example: `?ls`
"""
global sentence_mapper
embed = Embed(title="Sasada Bot Command List ", description="wanna learn some Sasada's teaching 👉👌 kiddo?", color=0xae00ff)
embed.add_field(name="Teaching", value="`?p action`\n", inline=True)
embed.add_field(name="Cursing", value="`?b name`\nrandomly cursing ur friend", inline=True)
await ctx.send(embed=embed)
SENTENCES = [
'ไอ้ {name} ไอ้หน้าหี',
'ไอ้ {name} หัวควย',
'พ่อมึงตาย ไอ้ {name}',
'ไอ้ {name} ไอ้ควาย',
'ไอ้ {name} ไอ้สัส',
'ไอ้บ้าเอ้ย',
'อี ดอกทอง {name}',
'ไอ้สันขวาน {name}',
'ไอ้ระยำ {name}',
'ฉันไม่ด่าคุณหรอก คุณ {name}',
'ไอ้ {name} ไอ้หน้าหี ดับเบิ้ลหี สองหี สามหี',
'หลอนละมึง ไอ {name}',
'อย่าหลอนให้มาก ไอ้ {name}',
]
@bot.command(aliases=["b"])
async def cursing(ctx, *args):
"""
Curse other people
Example: `?b ทู`
"""
global voice_client
FILENAME = 'clips/curse.mp3'
if len(args) == 0:
print("No args, idiot.")
name = args[0]
for i in range(1):
tts = gTTS(text=choice(SENTENCES).format(name=name), lang='th')
tts.save(FILENAME)
user_channel = ctx.author.voice.channel
if ctx.me.voice is None or ctx.me.voice.channel != user_channel:
voice_client = await user_channel.connect()
voice_client.play(FFmpegPCMAudio(source=FILENAME))
while voice_client.is_playing():
await(asyncio.sleep(0.2))
@bot.command(aliases=["r"])
async def random(ctx):
r = choice(listdir("clips"))
r = r[:-4]
await play(ctx, r)
@bot.command(aliases=["dc"])
async def discon(ctx):
global voice_client
await voice_client.disconnect(force=True)
@bot.command(aliases=["re"])
async def replay(ctx):
if last is None:
return
await play(ctx, last)
GREETINGS = [
'จ๊ะเอ๋ {name}',
'ฮู่เล่ {name}',
'ทาดา {name}',
'ฮัลโหล {name}',
'ยาเลยาเล {name}',
'อย่าหลอน {name}',
'ออกไปเลย {name}',
'ใครเข้ามาหน่ะ อ๋อ {name}',
'{name} เข้ามาทำไม',
'{name} ออกไปเดี๋ยวนี้เลยนะ',
'{name} ออกไปไกลๆเลย',
'{name} บอก กามู',
'กูตื่นแล้ว ไอ้ {name} ตื่นรึยัง',
'งานการไม่ทำหรอวะ ไอ้ {name}',
'{name} อยากเข้าก็เข้า ทำตัวเป็นลุงตู่ไปได้'
]
BYES = [
'ใครจะอยู่ก็อยู่ แต่ {name} ไม่อยู่แล้ว',
'{name} ออกไปทำเหี้ยไร',
'{name} ออกไปแล้ว แต่ลุงตู่ยังไม่ออก',
'{name} ไปละ ด่ามันได้',
'จะไปไหนก็ไป {name}',
'กูถีบไอ่ {name} ออกไปแล้ว',
'ลาก่อย {name}',
'ไอ่ {name} มันหนีไปกรอกน้ำให้แม่',
'{name} บอกว่าไกปู',
'{name} กับเพื่อนกับฝูงไม่เคยจะอยู่',
'ใครออกไปน้า อ๋อ {name}',
'ไอ่เอ๋อ {name} นี่ออกไปอีกละ',
'ว้ายๆ ไอ้ {name} ไปนอนแล้ว',
'{name} บอกกูไปขี้แปป',
'{name} หนีเพื่อนไปดูหมอลำอีกแล้ว'
]
BYES_FROM_FILE = [
'preview_4.mp3',
'disconnect.mp3',
'cp-sound-get-out.mp3',
]
GREETINGS_FROM_FILE = [
'cp-sound-really.mp3',
'cp-sound-wow.mp3',
'pornhub-community-intro.mp3',
'hello_motherfrucker.mp3',
'.mp3_5M3Zb4t',
'among.mp3',
'kids_cheering.mp3',
'dry-fart.mp3',
]
ID_2_USER = {
332486822297075712 : 'flap'
}
USER_2_GREETINGS = {
'flap' : [
'โฟกัสอยู่นี่รึเปล่า',
'สวัสดีพวกหัวเหลี่ยม',
'{name} คิดถึงโฟกัสเลยมาหา'
]
}
USER_2_BYES = {
'flap' : [
'โฟกัสไม่อยู่ {name} ไปละ',
'โฟกัสไม่อยู่ ผมไปดีกว่า'
]
}
def download_file_from_myinstants(filename):
url = f'https://www.myinstants.com/media/sounds/{filename}'
r = requests.get(url)
with open(f'clips/{filename}', 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return None
for name in GREETINGS_FROM_FILE:
download_file_from_myinstants(name)
for name in BYES_FROM_FILE:
download_file_from_myinstants(name)
@bot.event
async def on_voice_state_update(member, before, after) :
"""
This event could trigger when someone update their voice state
voice state: None, self mute, self deafen, connect to channel ,etc.
But for now we use it as "connect to some channel from None" greeting (change channel excluded)
"""
global voice_client
if before.channel is None and after.channel is not None and not member.bot:
voice_client = await after.channel.connect()
await(asyncio.sleep(0.5))
message = choice(GREETINGS + GREETINGS_FROM_FILE)
is_user_specific = uniform(0,1) < 0.3
if member.id in ID_2_USER and is_user_specific:
user_str = ID_2_USER[member.id]
message = choice(USER_2_GREETINGS[user_str])
FILENAME = 'clips/welcome.mp3'
tts = gTTS(text=message.format(name=member.display_name), lang='th')
tts.save(FILENAME)
elif message in GREETINGS:
FILENAME = 'clips/welcome.mp3'
tts = gTTS(text=message.format(name=member.display_name), lang='th')
tts.save(FILENAME)
elif message in GREETINGS_FROM_FILE:
FILENAME = f'clips/{message}'
voice_client.play(FFmpegPCMAudio(source=FILENAME))
while voice_client.is_playing():
await(asyncio.sleep(0.5))
try:
if voice_client.channel is not None:
await voice_client.disconnect()
except AttributeError:
pass
if before.channel is not None and after.channel is None and not member.bot :
voice_client = await before.channel.connect()
await(asyncio.sleep(0.5))
message = choice(BYES + BYES_FROM_FILE)
is_user_specific = uniform(0,1) < 0.3
if member.id in ID_2_USER and is_user_specific:
user_str = ID_2_USER[member.id]
message = choice(USER_2_BYES[user_str])
FILENAME = 'clips/bye.mp3'
tts = gTTS(text=message.format(name=member.display_name), lang='th')
tts.save(FILENAME)
elif message in BYES:
FILENAME = 'clips/bye.mp3'
tts = gTTS(text=message.format(name=member.display_name), lang='th')
tts.save(FILENAME)
else :
FILENAME = f'clips/{message}'
voice_client.play(FFmpegPCMAudio(source=FILENAME))
while voice_client.is_playing():
await(asyncio.sleep(0.5))
try:
if voice_client.channel is not None:
await voice_client.disconnect()
except AttributeError:
pass
bot.run(settings.DISCORD_TOKEN)