-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
152 lines (117 loc) · 5.21 KB
/
bot.py
File metadata and controls
152 lines (117 loc) · 5.21 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
import os
from dotenv import load_dotenv
import asyncio
import discord
from discord.ext import commands
from audio_processor import process_audio
import config as config
connections = {}
summarize_loop_tasks = {}
class Sam(commands.Cog):
def __init__(self, bot_: commands.Bot):
self.bot = bot_
@commands.command()
async def start(self, ctx: commands.Context):
channel = ctx.message.author.voice.channel
if ctx.voice_client is not None:
await ctx.voice_client.move_to(channel)
connections[ctx.guild.id] = ctx.voice_client
else:
vc = await channel.connect()
connections.update({ctx.guild.id: vc})
self.join_command_channel_id = ctx.message.channel.id
async def on_recording_ended(sink, channel: discord.TextChannel, *args):
# process_audio(sink, channel, *args)
self.bot.loop.create_task(process_audio(sink, channel, self, *args))
ctx.voice_client.start_recording(discord.sinks.WaveSink(), on_recording_ended, channel)
@commands.command()
async def stop(self, ctx: commands.Context):
if ctx.guild.id not in connections:
print("guild ", ctx.guild.id, " is not in connections")
ctx.message.reply("recording was not active.")
else:
# if ctx.guild.id in summarize_loop_tasks: # todo
#
try:
print(connections)
vc = connections[ctx.guild.id]
await ctx.message.reply("alright, just a second...")
vc.stop_recording()
except Exception as e:
print("error: ", e)
await ctx.message.reply("failed to stop recording. ")
@commands.command()
async def loop(self, ctx: commands.Context):
print("user is requesting summarize")
if (not ctx.message.author.voice or not ctx.message.author.voice.channel):
return await ctx.message.reply("You must be in a vc first.")
channel = ctx.message.author.voice.channel
if ctx.voice_client is not None:
await ctx.voice_client.move_to(channel)
connections[ctx.guild.id] = ctx.voice_client
else:
vc = await channel.connect()
connections.update({ctx.guild.id: vc})
self.join_command_channel_id = ctx.message.channel.id
if ctx.guild.id not in summarize_loop_tasks:
print("creating summarize loop")
task = self.bot.loop.create_task(self.summarize_loop(ctx))
summarize_loop_tasks[ctx.guild.id] = task
async def summarize_loop(self, ctx: commands.Context):
channel = ctx.channel
print(f"waiting for {config.sleepTime} seconds.")
await asyncio.sleep(config.sleepTime)
try:
while True:
if ctx.guild.id not in connections:
print("guild ", ctx.guild.id, " is not in connections, break-ing.")
break
vc = connections[ctx.guild.id]
recording_done = asyncio.Event()
async def on_recording_ended(sink, channel: discord.TextChannel, *args):
# asyncio.run_coroutine_threadsafe(process_audio(sink, channel, self, *args), self.bot.loop)
# self.bot.loop.create_task(process_audio(sink, channel, self, *args))
asyncio.create_task(process_audio(sink, channel, self, *args))
recording_done.set() # allow loop to continue recording right away
vc.start_recording(discord.sinks.WaveSink(), on_recording_ended, channel)
await asyncio.sleep(config.contextTime)
print(f"recorded for {config.contextTime} seconds.")
vc.stop_recording()
await recording_done.wait()
except Exception as e:
print("error in summarizing loop: ", e)
if ctx.guild.id in connections:
await connections[ctx.guild.id].disconnect()
del connections[ctx.guild.id]
summarize_loop_tasks.pop(ctx.guild.id, None)
@commands.command()
async def leave(self, ctx: commands.Context):
if ctx.guild.id in connections:
vc = connections[ctx.guild.id]
vc.stop_recording()
del connections[ctx.guild.id]
await ctx.voice_client.disconnect(force=True)
@loop.before_invoke
@start.before_invoke
async def ensure_voice(self, ctx: commands.Context):
if ctx.voice_client is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
await ctx.send("You are not connected to a voice channel.")
raise commands.CommandError("Author not connected to a voice channel.")
elif ctx.voice_client.is_playing():
ctx.voice_client.stop()
bot = commands.Bot(
command_prefix=commands.when_mentioned_or("sam "),
description="test description",
# intents=intents,
intents=discord.Intents.default().all(),
)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("------")
load_dotenv()
bot.add_cog(Sam(bot))
bot.run(os.getenv("TOKEN"))