-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (64 loc) · 2.38 KB
/
Copy pathmain.py
File metadata and controls
78 lines (64 loc) · 2.38 KB
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
from datetime import datetime
import discord
import os
import speech2text
from dotenv import load_dotenv
load_dotenv()
token = str(os.getenv("TOKEN"))
bot = discord.Bot()
connections = {}
@bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
@bot.event
async def on_voice_state_update(member, before, after):
if member.id == bot.user.id and member.guild.id in connections and after.channel is None:
vc = connections[member.guild.id]
vc.stop_recording()
@bot.slash_command(description = "Ping")
async def ping(ctx):
latency = bot.latency * 1000
await ctx.respond(f"Pong! Latency is {int(latency)}ms")
@bot.slash_command(description = "Stalk a Voice Channel")
async def stalk(ctx):
voice = ctx.author.voice
if not voice:
return await ctx.respond("You aren't in a voice channel!")
vc = await voice.channel.connect()
connections.update({ctx.guild.id: vc})
recordDatetime = datetime.now()
vc.start_recording(
discord.sinks.MP3Sink(),
stop_callback,
ctx.channel,
recordDatetime
)
await ctx.respond("Started stalking. .")
@bot.slash_command(description = "Stop Stalking")
async def stop(ctx):
if ctx.guild.id in connections:
vc = connections[ctx.guild.id]
vc.stop_recording()
del connections[ctx.guild.id]
await ctx.delete()
await vc.disconnect()
await ctx.respond("Stopped stalking. .")
else:
await ctx.respond("I am currently not stalking!")
async def stop_callback(sink: discord.sinks, channel: discord.TextChannel, recordTimestamp: datetime):
await sink.vc.disconnect()
timestamp = datetime.now().strftime("%d-%m-%Y %H%M%S")
dir = os.path.join("output", timestamp)
os.mkdir(dir)
userTranscripts = {}
for user_id, audio in sink.audio_data.items():
file = os.path.join(dir, f"{user_id}.{sink.encoding}")
with open(file, "wb") as f:
f.write(audio.file.getbuffer())
user = channel.guild.get_member(user_id)
userTranscripts.update(speech2text.get_user_transcript(recordTimestamp, user.display_name, file))
if len(userTranscripts):
speech2text.compile_user_transcript(sorted(userTranscripts.items()), dir)
file = discord.File(os.path.join(dir, "transcript.txt"), f"{dir}.txt")
await channel.send(f"Finished stalking!", file=file)
bot.run(token)