Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
305 lines (221 loc) · 7.26 KB
/
Copy pathbot.py
File metadata and controls
305 lines (221 loc) · 7.26 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
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
import queue
import discord
from discord.ext import commands
from discord import app_commands
import edge_tts
import asyncio
import wave
import time
# from langdetect import detect
from discord.ext import voice_recv
from dotenv import load_dotenv
import json
from datetime import datetime
from pathlib import Path
import os
import random
import threading
load_dotenv()
TOKEN = os.environ["BOT_TOKEN"]
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="?", intents=intents)
#app_commands.CommandTree(bot) # Initialize command tree for slash commands
voices = asyncio.run(edge_tts.list_voices())
status = ""
recording = False
voice_client = None
wave_file = None
alone_timer = None
# ---------- Audio Sink ----------
class Recorder(voice_recv.AudioSink):
def __init__(self):
super().__init__()
# ----- Session Folder -----
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.session_dir = Path("sessions") / timestamp
self.users_dir = self.session_dir / "users"
self.users_dir.mkdir(parents=True, exist_ok=True)
# ----- Track state -----
self.files = {}
self.queues = {}
self.threads = {}
self.running = True
self.join_offsets = {}
self.start_time_ms = int(time.time() * 1000)
# Metadata
self.metadata = {
"session_start": timestamp,
"users": {}
}
def wants_opus(self):
return False
# ----- Get session elapsed ms -----
def current_offset_ms(self):
return int(time.time() * 1000) - self.start_time_ms
# ---------- Worker Thread ----------
def user_worker(self, user_id):
wf = self.files[user_id]
q = self.queues[user_id]
while self.running or not q.empty():
try:
data = q.get(timeout=0.5)
wf.writeframes(data)
except queue.Empty:
continue
# ---------- Add User ----------
def add_user(self, user):
filepath = self.users_dir / f"{user.id}.{user.name}.wav"
wf = wave.open(str(filepath), "wb")
wf.setnchannels(2)
wf.setsampwidth(2)
wf.setframerate(48000)
self.files[user.id] = wf
self.queues[user.id] = queue.Queue()
# Metadata
offset = self.current_offset_ms()
self.join_offsets[user.id] = offset
self.metadata["users"][str(user.id)] = {
"name": user.name,
"join_offset_ms": offset
}
# Start worker thread
thread = threading.Thread(
target=self.user_worker,
args=(user.id,),
daemon=True
)
thread.start()
self.threads[user.id] = thread
# ---------- Write Audio ----------
def write(self, user, data):
if not data.pcm:
return
# Add new user dynamically
if user.id not in self.files:
self.add_user(user)
packet_size = len(data.pcm)
silence = bytes(packet_size)
# Push audio to queues
for uid in self.files.keys():
if uid == user.id:
self.queues[uid].put(data.pcm)
else:
self.queues[uid].put(silence)
# ---------- Cleanup ----------
def cleanup(self):
self.running = False
# Wait for workers
for thread in self.threads.values():
thread.join()
# Close files
for wf in self.files.values():
wf.close()
# Save metadata
meta_path = self.session_dir / "metadata.json"
with open(meta_path, "w", encoding="utf8") as f:
json.dump(self.metadata, f, indent=4)
# ---------- Bot Events ----------
@bot.event
async def on_ready():
await bot.tree.sync()
print(f"Logged in as {bot.user}")
# ---------- Join Command ----------
@bot.tree.command(name="join")
async def join(interaction: discord.Interaction):
global voice_client
if interaction.user.voice is None:
await interaction.response.send_message(
"You must be in a voice channel",
ephemeral=True
)
return
channel = interaction.user.voice.channel
voice_client = await channel.connect(cls=voice_recv.VoiceRecvClient)
await interaction.response.send_message(
"Joined voice channel",
ephemeral=True
)
# ---------- Start Recording ----------
@bot.tree.command(name="record", description="Start recording meeting")
async def record(interaction: discord.Interaction):
global recording, voice_client
if voice_client is None:
await interaction.response.send_message(
"Bot not in voice channel!",
ephemeral=True
)
return
voice_client.listen(Recorder())
recording = True
await interaction.response.send_message(
"Recording started",
ephemeral=True # ← ONLY user sees this
)
# ---------- Stop Recording ----------
@bot.tree.command(name="stop", description="Stop recording meeting")
async def stop(interaction: discord.Interaction):
await stop_recording(interaction)
async def stop_recording(interaction: discord.Interaction):
global recording, voice_client
recording = False
if voice_client and voice_client.is_listening():
voice_client.stop_listening()
if interaction:
await interaction.response.send_message(
"Recording stopped",
ephemeral=True
)
# ---------- SAY COMMAND ----------
@bot.tree.command(name="say", description="Bot speaks text")
@app_commands.describe(text="Text for bot to speak")
async def say(interaction: discord.Interaction, text: str):
global voice_client
if voice_client is None:
await interaction.response.send_message(
"Bot is not in voice channel",
ephemeral=True
)
return
await interaction.response.send_message(
"Speaking...",
ephemeral=True
)
# Generate speech file
communicate = edge_tts.Communicate(text, random.choice(voices)["ShortName"])
await communicate.save("speech.mp3")
# Stop if already speaking
if voice_client.is_playing():
voice_client.stop()
# Play audio using FFmpeg
audio = discord.FFmpegPCMAudio("speech.mp3")
voice_client.play(audio)
# ---------- Auto Leave When Alone ----------
@bot.event
async def on_voice_state_update(member, before, after):
global voice_client
if voice_client is None:
return
# ---- Bot kicked detection ----
if member == bot.user and after.channel is None:
print("Bot was disconnected.")
await stop_recording()
return
# ---- Empty channel detection ----
channel = voice_client.channel
if channel is None:
return
humans = [m for m in channel.members if not m.bot]
if len(humans) == 0:
await handle_empty_channel()
async def handle_empty_channel():
global voice_client
print("Channel empty. Waiting 30 seconds.")
await asyncio.sleep(30)
if voice_client is None:
return
channel = voice_client.channel
humans = [m for m in channel.members if not m.bot]
if len(humans) == 0:
print("Still empty. Leaving.")
await voice_client.disconnect()
bot.run(TOKEN)