-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tasya.py
319 lines (277 loc) · 11 KB
/
Tasya.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
#!/usr/bin/python3
# # -*- coding: utf-8 -*-
import asyncio
import coloredlogs
import logging
import config
import sys
import io
import base64
import discord
from discord import app_commands
from game import GameView, add_game, edit_game, get_games
from music import MusicController
from talk import generate
from stt import recognize
from image import caption
coloredlogs.install(
level="INFO",
fmt="[{asctime}] [{levelname:<8}] {name}: {message}",
style="{",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("main")
class MyClient(discord.Client):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
super().__init__(intents=intents)
self.tree = app_commands.CommandTree(self)
self.music = MusicController()
self.talk_lock = asyncio.Lock()
async def setup_hook(self):
if len(sys.argv) > 1:
if sys.argv[1] == "-u":
logger.warning("Syncing command tree")
await self.tree.sync()
client = MyClient()
@client.event
async def on_ready():
logger.info(f"Logged in as {client.user} (ID: {client.user.id})")
logger.info("------")
music_grp = app_commands.Group(name="music", description="Музычка")
@client.tree.command()
async def game(interaction: discord.Interaction):
"""Поиграть с друзьяшками"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /game"
)
view = GameView(interaction.guild_id)
if len(view.games) == 0:
await interaction.response.send_message(
content="Сначала добавь хоть одну игру!"
)
else:
await interaction.response.send_message(view=view)
@client.tree.command()
@app_commands.describe(
name="Название игры",
player_count="Необходимое количество игроков",
role="Роль, которую пингуем при сборе",
)
async def gaymeadd(
interaction: discord.Interaction,
name: str,
player_count: app_commands.Range[int, 2, 20],
role: discord.Role,
):
"""Добавить новую игру в список"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /gameadd"
)
res = add_game(name, player_count, role.id, interaction.guild_id)
if res:
await interaction.response.send_message(content="Игра успешно добавлена!")
else:
await interaction.response.send_message(content="Ошибка при добавлении.")
@client.tree.command()
@app_commands.describe(
name="Название игры",
player_count="Необходимое количество игроков",
role="Роль, которую пингуем при сборе",
)
async def gaymeedit(
interaction: discord.Interaction,
name: str,
player_count: app_commands.Range[int, 2, 20],
role: discord.Role,
):
"""Отредактировать игру"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /gameedit"
)
games = get_games(interaction.guild_id)
sel_game = None
for game in games:
if game.name == name:
sel_game = game
if sel_game is None:
await interaction.response.send_message(content="Такой игры не существует.")
return
res = edit_game(name, interaction.guild_id, player_count, role.id)
if res:
await interaction.response.send_message(
content=f"**{name}** успешно обновлена!"
)
else:
await interaction.response.send_message(content="Ошибка при обновлении.")
@music_grp.command(name="play")
@app_commands.describe(link="Ссылка или запрос")
async def music_play(interaction: discord.Interaction, link: str):
"""Добавить песню в очередь"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music play"
)
if (player := client.music.get_player(interaction.guild)) is None:
if (voice := interaction.user.voice) is not None:
player = await client.music.connect(voice.channel)
else:
return await interaction.response.send_message(
"Вы не в голосовом канале!", delete_after=30
)
res = await player.queue(link)
await interaction.response.send_message(
f"[{res['title']}]({res['url']}) добавлено в очередь"
)
@music_grp.command(name="pause")
async def music_pause(interaction: discord.Interaction):
"""Поставить на паузу"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music pause"
)
if (player := client.music.get_player(interaction.guild)) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
player.pause()
await interaction.response.send_message("Поставлено на паузу.", delete_after=30)
@music_grp.command(name="resume")
async def music_resume(interaction: discord.Interaction):
"""Продолжить воспроизведение"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music resume"
)
if (player := client.music.get_player(interaction.guild)) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
player.resume()
await interaction.response.send_message("Продолжаю.", delete_after=30)
@music_grp.command(name="skip")
async def music_skip(interaction: discord.Interaction):
"""Включить следующую песню"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music skip"
)
if (player := client.music.get_player(interaction.guild)) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
player.skip()
await interaction.response.send_message("Включаю следующую...", delete_after=30)
@music_grp.command(name="volume")
@app_commands.describe(volume="Громкость")
async def music_volume(
interaction: discord.Interaction, volume: app_commands.Range[int, 1, 100]
):
"""Установить громкость"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music volume"
)
if (player := client.music.get_player(interaction.guild)) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
player.set_volume(volume / 100)
await interaction.response.send_message(
f"Громкость установлена на {volume}", delete_after=30
)
@music_grp.command(name="stop")
async def music_stop(interaction: discord.Interaction):
"""Остановить музыку"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music stop"
)
if client.music.get_player(interaction.guild) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
await client.music.destroy_player(interaction.guild)
await interaction.response.send_message("Музыка выключена.", delete_after=30)
@music_grp.command(name="now")
async def music_now(interaction: discord.Interaction):
"""Что сейчас играет"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music now"
)
if client.music.get_player(interaction.guild) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
await interaction.response.send_message(
embed=await client.music.now(interaction.guild)
)
@music_grp.command(name="queue")
async def music_queue(interaction: discord.Interaction):
"""Очередь песен"""
logger.info(
f"({interaction.guild.name}) {interaction.user.display_name} used /music queue"
)
if client.music.get_player(interaction.guild) is None:
return await interaction.response.send_message(
"Ничего не играет!", delete_after=30
)
await interaction.response.send_message(
embed=await client.music.queue(interaction.guild)
)
client.tree.add_command(music_grp)
@client.event
async def on_message(message):
if message.author == client.user:
logger.info(
f"({message.guild.name}) {client.user.display_name}: {message.content}"
)
return
for att in message.attachments:
if att.filename == "voice-message.ogg":
logger.info(
f"({message.guild.name}) {message.author.display_name}: *voice message*"
)
spoken_text = await recognize(await att.read())
if spoken_text != "":
await message.reply(spoken_text)
ref = message.reference
if ref is not None:
ref = ref.resolved
if ref is not None:
ref = ref.author
if (
"Тас" in message.content
or ref == client.user
or client.user in message.mentions
):
logger.info(
f"({message.guild.name}) {message.author.display_name}: {message.content}"
)
logger.info("I was mentioned")
messages = [
hist_message async for hist_message in message.channel.history(limit=50)
]
cntr_you = 0
cont = ""
for hist_message in messages:
text = hist_message.clean_content.replace("\n", " ")
for att in hist_message.attachments:
if "image" in att.content_type:
img_stream = io.BytesIO()
await att.save(img_stream)
b64_bytes = base64.b64encode(img_stream.getvalue())
img_caption = await caption(b64_bytes.decode())
text += (
"\n<|start_header_id|>model<|end_header_id|>*shows <|model|> a picture of "
+ img_caption
+ "*<|eot_id|>"
)
if hist_message.author == client.user:
name = "<|start_header_id|>model<|end_header_id|>"
else:
name = "<|start_header_id|>user<|end_header_id|>"
cntr_you += 1
cont = name + ": " + text + "<|eot_id|>\n" + cont
if cntr_you > 14:
break
async with message.channel.typing():
resp = await generate(config.prompt, cont, client.talk_lock)
await message.reply(resp)
client.run(config.bot_token, log_level=logging.DEBUG, log_handler=logging.NullHandler())