-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubtitlebot.py
63 lines (53 loc) · 1.57 KB
/
subtitlebot.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
import discord
import time
import speech_recognition as sr
with open('discord_token.txt') as f:
token = f.readlines()[1]
client = discord.Client()
rec = sr.Recognizer()
time_limit = 5.0
class timer:
def __init__(self):
self.time = 0.0
def start(self):
self.time = time.time()
def end(self):
self.time = time.time() - self.time
def reset(self):
if self.time != 0.0:
self.time = 0.0
timer = timer()
@client.event
async def on_ready():
print('Bot Logged In As {0.user}.'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
channel = message.channel
return
if message.content.startswith('::start'):
await timer.start()
await channel.send('Ready For Listening.')
if message.content.startswith('::end'):
await timer.reset()
await channel.send('Listening Finished.')
while timer.time != 0.0:
with sr.Microphone() as source:
audio = rec.listen(source, phrase_time_limit=time_limit)
timer.end()
if timer.time == time_limit:
try:
text = rec.recognize_google(audio)
channel.send(text)
except ValueError:
channel.send('No One Was Speaking.')
timer.start()
else:
channel.send('Timer Error.')
timer.start()
if __name__ == '__main__':
try:
print(token)
client.run(token, bot=True)
except:
print('Login Error.')