-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
945 lines (875 loc) · 42.9 KB
/
Copy pathmain.py
File metadata and controls
945 lines (875 loc) · 42.9 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
import asyncio
import json
import logging
import os
import sys
import aiohttp
import discord
from discord import app_commands
from discord.ext import commands, tasks
from MOMOKA.GUI import attach_gui_logging, run_log_viewer_thread, set_bot_ref, set_dark_mode
from MOMOKA.storage import NS_LOGGING_CHANNELS, get_default_settings_db
from MOMOKA.utilities.command_i18n import CommandDescriptionTranslator
from MOMOKA.utilities.restart_notice import shutdown_allowed_user_id
from MOMOKA.version import VERSION, get_build_date, status_version_string
# Python 3.11 未満では依存パッケージ(discord.py 2.7 / torch 等)の動作保証外のため起動を拒否する
if sys.version_info < (3, 11):
# 現在のインタプリタバージョンをユーザー向けに表示する
_current = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
# 標準エラーへ要件を明示して終了する
sys.stderr.write(
f"MOMOKA requires Python 3.11 or higher (detected {_current}).\n"
"Please recreate the virtual environment with Python 3.11 "
"(e.g. `py -3.11 -m venv .venv` or startMOMOKA.bat).\n"
)
# 非ゼロ終了コードでプロセスを終了する
sys.exit(1)
def _preload_torch() -> None:
"""venv の torch を最優先で読み込み、C10 二重登録を防ぐ。
PATH 上に別環境の ``torch\\lib``(例: グローバル Python 3.11 の torch 2.7)が
残っていると、c10.dll が二重ロードされ
``Key already registered with the same priority: C10`` で即終了することがある。
Discord ログイン直後の Cog(TTS / 画像)読込より前に確定させる。
"""
# 現在の PATH を分割する
path_parts = os.environ.get("PATH", "").split(os.pathsep)
# 他環境の torch\\lib を除外した PATH を組み立てる
filtered_parts = []
for part in path_parts:
# パス比較用に区切りを正規化する
norm = part.replace("/", "\\").lower()
# site-packages\\torch\\lib を含むエントリは衝突源なので捨てる
if "\\torch\\lib" in norm:
continue
# 残すパスを追加する
filtered_parts.append(part)
# 浄化した PATH を書き戻す
os.environ["PATH"] = os.pathsep.join(filtered_parts)
try:
# TTS / diffusers より先に torch 本体をロードする
import torch # noqa: F401
except ImportError:
# 画像・TTS 無効構成でも起動できるよう欠落は無視する
return
# Cog 読込より前に torch を確定する(C10 衝突回避)
_preload_torch()
# Windows ダークモード(ログビューア用)を有効化する
set_dark_mode()
# --- ロギング設定の初期化 ---
# ルートロガーの設定
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# 特定のロガーのログレベル設定
logging.getLogger("discord").setLevel(logging.WARNING)
logging.getLogger("openai").setLevel(logging.WARNING)
logging.getLogger("google.generativeai").setLevel(logging.WARNING)
logging.getLogger("google.ai").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("asyncio").setLevel(logging.WARNING)
logging.getLogger("PIL").setLevel(logging.WARNING)
class _VoiceWsTimeoutLogFilter(logging.Filter):
"""discord.voice_state の WS TimeoutError 再接続ログを1行 WARN に圧縮する。"""
def filter(self, record: logging.LogRecord) -> bool:
# 対象メッセージ以外はそのまま通す
if "Disconnected from voice" not in record.msg:
return True
# 再接続待ちログ以外はそのまま通す
if "Reconnecting" not in record.msg:
return True
# 例外種別を取り出す(無い場合は対象外)
exc = record.exc_info[1] if record.exc_info else None
# TimeoutError 以外(ConnectionClosed 等)は ERROR+traceback のまま残す
if not isinstance(exc, TimeoutError):
return True
# ERROR を WARNING へ落とす
record.levelno = logging.WARNING
# レベル名も合わせて書き換え
record.levelname = "WARNING"
# traceback を出さない
record.exc_info = None
# 整形済み traceback キャッシュも消す
record.exc_text = None
# 1行メッセージに差し替える(args の retry 秒を再利用)
record.msg = "Voice WS timeout; reconnecting in %.2fs."
return True
# VC 制御 WS の一時切断 traceback を抑制する
logging.getLogger("discord.voice_state").addFilter(_VoiceWsTimeoutLogFilter())
# GUI ログキューへルートロガー / stdout を接続する
# 注意: DiscordLogHandler は setup_hook 内で追加されるため、GUI と Discord の両方にログが送信される
log_queue, queue_handler, stdout_capture = attach_gui_logging(root_logger)
from MOMOKA.services.discord_handler import DiscordLogHandler, DiscordLogFormatter
class Momoka(commands.Bot):
"""MOMOKA Botのメインクラス"""
def __init__(
self,
*args,
config: dict,
bot_id: str,
bot_role: str,
persona_key: str,
display_name: str,
cogs_to_load: list,
enable_discord_logging: bool = True,
**kwargs
):
super().__init__(*args, **kwargs)
# マージ済み設定辞書を保持する
self.config = config
# ボット識別子(例: "plana", "arona")
self.bot_id = bot_id
# ボット役割("primary" or "companion")
self.bot_role = bot_role
# ペルソナキー(例: "plana", "arona")
self.persona_key = persona_key
# 表示名(例: "[PLANA]", "[ARONA]")
self.display_name = display_name
# Discord ログ出力を有効化するか(primary のみ True)
self.enable_discord_logging = enable_discord_logging
# ステータスローテーション用テンプレートリスト
self.status_templates = []
# ステータスローテーション用インデックス
self.status_index = 0
# ロードする Cog のリスト(primary / companion で異なる)
self.cogs_to_load = cogs_to_load
# ランタイム設定用 SQLite(プロセス共通インスタンス)
self.settings_db = get_default_settings_db()
def is_admin(self, user_id: int) -> bool:
"""ユーザーが管理者かどうかをチェック"""
# 設定値が文字列でも Discord の整数 ID と比較できるよう正規化する
admin_ids = {
int(admin_id)
for admin_id in self.config.get('admin_user_ids', [])
if str(admin_id).isdigit()
}
return user_id in admin_ids
async def notify_active_users_of_restart(self) -> None:
"""利用中ユーザー(音楽・LLM)へ再起動通知を送る。"""
# 重い Cog モジュールはメソッド内で参照する(起動時の循環を避ける)
from MOMOKA.llm.llm_cog import LLMCog
from MOMOKA.music.music_cog import MusicCog
# 並列通知用のコルーチン一覧
tasks = []
# 音楽 Cog を名前で取得する
music_cog = self.get_cog(MusicCog.COG_NAME)
# 通知メソッドがある場合のみキューに入れる
if music_cog is not None and hasattr(music_cog, "notify_admin_restart"):
# MusicCog の再起動通知をキューへ追加する
tasks.append(music_cog.notify_admin_restart())
# LLM Cog を名前で取得する
llm_cog = self.get_cog(LLMCog.COG_NAME)
# 通知メソッドがある場合のみキューに入れる
if llm_cog is not None and hasattr(llm_cog, "notify_admin_restart"):
# LLMCog の再起動通知をキューへ追加する
tasks.append(llm_cog.notify_admin_restart())
# 対象が無ければ何もしない
if not tasks:
# 早期リターン
return
# 各 Cog の通知を並行実行し、例外は個別に収集する
results = await asyncio.gather(*tasks, return_exceptions=True)
# 失敗した通知だけログに残す
for result in results:
# 例外オブジェクトかどうかを判定する
if isinstance(result, Exception):
# 通知失敗を警告ログへ出す
logging.warning("Failed to notify active users of restart: %s", result)
async def close(self) -> None:
"""終了前に VC 状態を保存し、利用中ユーザーへ通知してから接続を閉じる。"""
try:
# 音楽 Cog があれば再起動用に VC セッションを先に保存する
from MOMOKA.music.music_cog import MusicCog
# Cog を名前で取得する
music_cog = self.get_cog(MusicCog.COG_NAME)
# persist メソッドがある場合のみ保存する
if music_cog is not None and hasattr(
music_cog, "persist_vc_sessions_for_restart"
):
# notify より前にスナップショットを取る(notify は current_track を消す)
await music_cog.persist_vc_sessions_for_restart()
except Exception as e:
# 保存失敗でもシャットダウン自体は継続する
logging.warning(
"%s persist_vc_sessions_for_restart failed during close: %s",
self.display_name,
e,
)
try:
# Ctrl+C /shutdown 共通で再起動通知を送る
await self.notify_active_users_of_restart()
except Exception as e:
# 通知失敗でもシャットダウン自体は継続する
logging.warning(
"%s notify_active_users_of_restart failed during close: %s",
self.display_name,
e
)
# Discord ログ Handler を先に外して Session is closed を防ぐ
try:
# ルートロガーのハンドラ一覧を走査する
root = logging.getLogger()
# DiscordLogHandler だけ取り外す
for handler in list(root.handlers):
# クラス名で判定する(循環 import 回避)
if handler.__class__.__name__ == "DiscordLogHandler":
# ハンドラを閉じる
try:
handler.close()
except Exception:
pass
# ルートから除去する
root.removeHandler(handler)
except Exception as e:
# ハンドラ除去失敗はシャットダウンを止めない
logging.debug("%s DiscordLogHandler detach failed: %s", self.display_name, e)
# discord.py 本来のクローズ処理へ進む
await super().close()
async def setup_hook(self):
"""Botの初期セットアップ(ログイン後、接続準備完了前)"""
# 実行中の Python バージョンを起動ログへ残す(3.11 前提の切り分け用)
logging.info(
"%s Runtime Python %s.%s.%s (MOMOKA requires 3.11.x)",
self.display_name,
sys.version_info.major,
sys.version_info.minor,
sys.version_info.micro,
)
# self.config は __init__ で渡されているため、ファイル読み込みは不要
logging.info("%s マージ済み設定を使用して起動します。", self.display_name)
# プレゼンス文言は YAML(presence.status_rotation)のみ。コード内ハードコードは使わない
presence_cfg = self.config.get("presence") or {}
# presence.status_rotation を優先する
status_templates = presence_cfg.get("status_rotation")
# 無ければ旧トップレベル status_rotation(互換)
if not isinstance(status_templates, list) or not status_templates:
status_templates = self.config.get("status_rotation")
# リスト以外・空はローテ無し(activity_type=none 以外なら文言なしのまま)
if not isinstance(status_templates, list):
status_templates = []
# 文字列要素だけ残す
status_templates = [
str(item) for item in status_templates if str(item).strip()
]
# 未設定なら案内ログを出す
if not status_templates:
logging.info(
"%s presence.status_rotation が空です。"
"表示文言は configs/core_config.yaml の presence.status_rotation で設定してください。",
self.display_name,
)
# テンプレートを保持する
self.status_templates = status_templates
# ステータスローテーションタスクを開始する
self.rotate_status.start()
# Discord ログ出力は primary のみ有効化する
if self.enable_discord_logging:
# 設定辞書からログチャンネル ID リストを取得する
log_channel_ids_from_config = self.config.get('log_channel_ids', [])
# リスト形式でなければ空リストに戻す
if not isinstance(log_channel_ids_from_config, list):
log_channel_ids_from_config = []
logging.warning(
"%s configs/core_config.yaml の 'log_channel_ids' はリスト形式である必要があります。",
self.display_name
)
# SQLite から追加のチャンネル ID を読み込む
log_channel_ids_from_db = []
try:
# settings DB から logging_channels namespace を取る
data = self.settings_db.load(NS_LOGGING_CHANNELS)
# リスト形式かつ全要素が int であれば採用する
if isinstance(data, list) and all(isinstance(i, int) for i in data):
log_channel_ids_from_db = data
elif data is None:
# 未移行時は空で起動する(移行スクリプト実行を前提)
logging.info(
"%s logging_channels が DB に無いため空リストで起動します。",
self.display_name,
)
except Exception as e:
logging.error(
"%s logging_channels の読込中にエラーが発生しました: %s",
self.display_name,
e,
)
# 設定ファイルと DB のチャンネル ID を統合する
all_log_channel_ids = list(set(log_channel_ids_from_config + log_channel_ids_from_db))
# チャンネル ID が設定されていれば Discord ログハンドラを追加する
if all_log_channel_ids:
try:
# Discord ログハンドラを作成する
discord_handler = DiscordLogHandler(
bot=self,
channel_ids=all_log_channel_ids,
interval=6.0,
)
# ログレベルを INFO に設定する
discord_handler.setLevel(logging.INFO)
# フォーマッタを設定する
discord_formatter = DiscordLogFormatter('%(asctime)s - %(levelname)s - [%(funcName)s] %(message)s')
discord_handler.setFormatter(discord_formatter)
# ルートロガーへハンドラを追加する
root_logger.addHandler(discord_handler)
logging.info(
"%s Discord へのロギングをチャンネル ID %s で有効化しました。",
self.display_name,
all_log_channel_ids
)
except Exception as e:
logging.error("%s DiscordLogHandler の初期化中にエラーが発生しました: %s", self.display_name, e)
else:
logging.warning("%s ログ送信先の Discord チャンネルが設定されていません。", self.display_name)
# Cog のロードを開始する
logging.info("%s Cog のロードを開始します...", self.display_name)
loaded_cogs_count = 0
# Cog リストを順番にロードする
for module_path in self.cogs_to_load:
try:
# Cog をロードする
await self.load_extension(module_path)
logging.info("%s > Cog '%s' のロードに成功しました。", self.display_name, module_path)
# ロード成功数を加算する
loaded_cogs_count += 1
except commands.ExtensionAlreadyLoaded:
logging.debug("%s Cog '%s' は既にロードされています。", self.display_name, module_path)
except commands.ExtensionNotFound:
logging.error(
"%s > Cog '%s' が見つかりません。ファイルパスを確認してください。",
self.display_name,
module_path
)
except commands.NoEntryPointError:
logging.error(
"%s > Cog '%s' に setup 関数が見つかりません。Cog として正しく実装されていますか?",
self.display_name,
module_path
)
except Exception as e:
logging.error(
"%s > Cog '%s' のロード中に予期しないエラーが発生しました: %s",
self.display_name,
module_path,
e,
exc_info=True
)
logging.info(
"%s Cog のロードが完了しました。合計 %d 個の Cog をロードしました。",
self.display_name,
loaded_cogs_count
)
# companion ボットは同期タイミングをずらす(rate limit 回避)
if self.bot_role == "companion":
# 2秒待機して primary の同期を先に行わせる
await asyncio.sleep(2)
# description 多言語 Translator を sync 前に登録する(name は英語固定)
try:
# YAML カタログベースの Translator をセットする
await self.tree.set_translator(CommandDescriptionTranslator(self.config))
# 登録成功をログに残す
logging.info("%s コマンド description Translator を登録しました。", self.display_name)
except Exception as e:
# Translator 失敗でも起動は継続する(英語デフォルトのみ)
logging.error(
"%s コマンド description Translator の登録に失敗しました: %s",
self.display_name,
e,
exc_info=True,
)
# スラッシュコマンドの同期を行う
if self.config.get('sync_slash_commands', True):
try:
# テストギルド ID が設定されているか確認する
test_guild_id = self.config.get('test_guild_id')
if test_guild_id:
# テストギルドへグローバルコマンドを複製して同期する
guild_obj = discord.Object(id=int(test_guild_id))
self.tree.copy_global_to(guild=guild_obj)
synced_commands = await self.tree.sync(guild=guild_obj)
logging.info(
"%s %d 個のスラッシュコマンドをテストギルド %s に同期しました。",
self.display_name,
len(synced_commands),
test_guild_id
)
else:
# グローバルに同期する
synced_commands = await self.tree.sync()
logging.info(
"%s %d 個のグローバルスラッシュコマンドを同期しました。",
self.display_name,
len(synced_commands)
)
except Exception as e:
logging.error(
"%s スラッシュコマンドの同期中にエラーが発生しました: %s",
self.display_name,
e,
exc_info=True
)
else:
logging.info("%s スラッシュコマンドの同期は設定で無効化されています。", self.display_name)
# エラーハンドラを設定する
self.tree.on_error = self.on_app_command_error
def _resolve_discord_status(self, raw: object) -> discord.Status:
"""core_config.presence.status 文字列を discord.Status に変換する。"""
# 未設定はオンライン扱い
key = str(raw or "online").strip().lower()
# エイリアスを正規名へ寄せる
if key in ("do_not_disturb", "do-not-disturb", "busy"):
key = "dnd"
if key in ("offline",):
key = "invisible"
# 許容値マップ
mapping = {
"online": discord.Status.online,
"idle": discord.Status.idle,
"dnd": discord.Status.dnd,
"invisible": discord.Status.invisible,
}
# 不明値は警告して online にフォールバック
if key not in mapping:
logging.warning(
"%s Unknown presence.status=%r; falling back to online",
self.display_name,
raw,
)
return discord.Status.online
return mapping[key]
def _build_presence_activity(
self,
status_text: str,
presence_config: dict,
):
"""core_config.presence.activity_type に応じた Activity を組み立てる。"""
# 種別文字列を正規化する
activity_type = str(
presence_config.get("activity_type") or "streaming"
).strip().lower()
# アクティビティ無し
if activity_type in ("none", "off", "clear"):
return None
# streaming 用 URL(未設定時は既定 Twitch)
streaming_url = presence_config.get(
"streaming_url",
"https://www.twitch.tv/coffinnoob299",
)
# 空 URL でも Streaming は作れるが Discord が弾くことがあるため補完する
if not streaming_url:
streaming_url = "https://www.twitch.tv/coffinnoob299"
# 種別に応じて Activity を返す
if activity_type == "streaming":
return discord.Streaming(name=status_text, url=str(streaming_url))
if activity_type == "listening":
return discord.Activity(
type=discord.ActivityType.listening,
name=status_text,
)
if activity_type == "watching":
return discord.Activity(
type=discord.ActivityType.watching,
name=status_text,
)
if activity_type == "competing":
return discord.Activity(
type=discord.ActivityType.competing,
name=status_text,
)
if activity_type == "playing":
return discord.Game(name=status_text)
# 未知の種別は警告して従来どおり streaming に倒す
logging.warning(
"%s Unknown presence.activity_type=%r; falling back to streaming",
self.display_name,
activity_type,
)
return discord.Streaming(name=status_text, url=str(streaming_url))
@tasks.loop(seconds=15)
async def rotate_status(self):
"""ボットのプレゼンス(状態・アクティビティ)を定期的に更新する。"""
# プレゼンス設定を読む
presence_config = self.config.get("presence") or {}
if not isinstance(presence_config, dict):
presence_config = {}
# オンライン状態を解決する
discord_status = self._resolve_discord_status(
presence_config.get("status")
)
# アクティビティ種別(none なら文言ローテ不要)
activity_type = str(
presence_config.get("activity_type") or "streaming"
).strip().lower()
# 文言テンプレートが無く、かつ none 以外なら更新しない
if not self.status_templates and activity_type not in ("none", "off", "clear"):
return
# 表示文言を決める(none のときは空でよい)
status_text = ""
if self.status_templates and activity_type not in ("none", "off", "clear"):
# 次のテンプレートを選ぶ
status_template = self.status_templates[self.status_index]
# インデックスを進める
self.status_index = (self.status_index + 1) % len(self.status_templates)
# プレースホルダを置換する
try:
status_text = status_template.format(
guild_count=len(self.guilds),
# セマンティックバージョン(例: 1.0.0)
version=VERSION,
# 最終 git コミット日 YYYY-MM-DD
build_date=get_build_date(),
# 旧既定と同じ「prjMOMOKA Ver.YYYY-MM-DD」全文
status_version=status_version_string(),
)
except (KeyError, ValueError, IndexError):
# プレースホルダ不正時はそのまま使う
status_text = str(status_template)
# Activity を組み立てる
activity = self._build_presence_activity(status_text, presence_config)
# Discord へ反映する
try:
await self.change_presence(status=discord_status, activity=activity)
except (aiohttp.client_exceptions.ClientConnectionResetError, ConnectionResetError) as e:
logging.warning("Failed to rotate status due to connection reset: %s", e)
except Exception as e:
logging.error("Failed to rotate status: %s", e)
@rotate_status.before_loop
async def before_rotate_status(self):
"""ステータスローテーションタスクの開始を待機"""
await self.wait_until_ready()
async def on_command_error(self, ctx: commands.Context, error: commands.CommandError):
"""通常コマンド(プレフィックスコマンド)のエラーハンドリング"""
# CommandNotFoundエラーは無視(コマンドを探すモードを無効化)
if isinstance(error, commands.CommandNotFound):
return # エラーを無視して何もしない
# その他のエラーはログに記録(必要に応じて処理)
logging.debug(f"コマンドエラー: {error}")
async def _send_app_command_error(
self,
interaction: discord.Interaction,
message: str,
) -> None:
"""スラッシュコマンドのエラー応答を重複なく送信する。"""
# 初回応答済みなら followup を使い、二重応答エラーを防ぐ
if interaction.response.is_done():
await interaction.followup.send(message, ephemeral=True)
return
# 未応答なら interaction の初回応答として送信する
await interaction.response.send_message(message, ephemeral=True)
async def on_app_command_error(
self,
interaction: discord.Interaction,
error: app_commands.AppCommandError,
):
"""スラッシュコマンドのエラーハンドリング"""
# InvokeError は元例外を取り出して適切なエラー種別で判定する
original_error = (
error.original
if isinstance(error, app_commands.CommandInvokeError)
else error
)
if isinstance(original_error, app_commands.MissingPermissions):
await self._send_app_command_error(
interaction,
"❌ このコマンドを実行する権限がありません。",
)
elif isinstance(
original_error,
(app_commands.BotMissingPermissions, discord.Forbidden),
):
await self._send_app_command_error(
interaction,
"❌ ボットに必要な権限がありません。管理者に連絡してください。",
)
elif isinstance(original_error, app_commands.CommandOnCooldown):
await self._send_app_command_error(
interaction,
"⏳ このコマンドは "
f"{original_error.retry_after:.1f} 秒後に再試行できます。",
)
elif isinstance(original_error, app_commands.CheckFailure):
return
else:
# 未分類の例外は原因を記録し、利用者には共通メッセージだけを返す
logging.error("コマンドエラー: %s", original_error, exc_info=original_error)
await self._send_app_command_error(
interaction,
"❌ コマンドの実行中にエラーが発生しました。",
)
# CONFIG_FILE / DEFAULT_CONFIG_FILE は削除済み — configs/*.yaml を使用
# ===============================================================
# ===== Cog ロードリスト ========================================
# ===============================================================
# プライマリボット(PLANA)が読み込む全 Cog
PRIMARY_COGS = [
'MOMOKA.count.count_cog',
'MOMOKA.images.image_commands_cog',
'MOMOKA.llm.llm_cog',
'MOMOKA.link_fix.link_fix_cog',
'MOMOKA.media_downloader.ytdlp_downloader_cog',
'MOMOKA.music.music_cog',
'MOMOKA.notifications.earthquake_notification_cog',
'MOMOKA.notifications.twitch_notification_cog',
'MOMOKA.tracker.r6s_tracker_cog',
'MOMOKA.tracker.valorant_tracker_cog',
'MOMOKA.tts.tts_cog',
'MOMOKA.utilities.guild_events_cog',
'MOMOKA.utilities.slash_command_cog',
'MOMOKA.utilities.latex_command_cog',
]
# コンパニオンボット(ARONA)が読み込む軽量 Cog(LLM / 音楽 / スラッシュコマンド)
COMPANION_COGS = [
'MOMOKA.llm.llm_cog',
'MOMOKA.music.music_cog',
'MOMOKA.utilities.guild_events_cog',
'MOMOKA.utilities.slash_command_cog',
'MOMOKA.utilities.latex_command_cog',
]
# ===============================================================
if __name__ == "__main__":
momoka_art = r"""
███╗ ███╗ ██████╗ ███╗ ███╗ ██████╗ ██╗ ██╗ █████╗
████╗ ████║██╔═══██╗████╗ ████║██╔═══██╗██║ ██╔╝██╔══██╗
██╔████╔██║██║ ██║██╔████╔██║██║ ██║█████╔╝ ███████║
██║╚██╔╝██║██║ ██║██║╚██╔╝██║██║ ██║██╔═██╗ ██╔══██║
██║ ╚═╝ ██║╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║ ██╗██║ ██║
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝
"""
print(momoka_art)
# ログビューアをスレッドで起動する
log_viewer_thread = run_log_viewer_thread(log_queue)
print("ホスト GUI(Electron)を起動しました。")
# --- 設定の読み込みとバリデーション ---
from MOMOKA.config.loader import load_merged_config, validate_bot_tokens
from MOMOKA.bots.registry import registry
from MOMOKA.llm.concurrency import init_concurrency
from MOMOKA.services.bgutil_provider import (
BgutilProviderConfig,
BgutilProviderManager,
)
from MOMOKA.media_downloader.tunnel_file_server import (
MediaShareConfig,
TunnelFileServer,
)
from MOMOKA.storage import resolve_settings_db
# configs/*.yaml を統合した設定辞書を読み込む
try:
# マージ済み設定辞書を取得する
merged_config = load_merged_config()
print("INFO: configs/bots_config.yaml などの設定を統合しました。")
except Exception as e_load:
print(f"CRITICAL: 設定ファイルの読み込み中にエラーが発生しました: {e_load}")
sys.exit(1)
# ボットトークンの存在確認を行う
try:
# PLANA / ARONA のトークンをバリデーションする
validate_bot_tokens(merged_config)
print("INFO: ボットトークンのバリデーションに成功しました。")
except ValueError as e_token:
print(f"CRITICAL: {e_token}")
sys.exit(1)
# マージ済み設定から共有PO Token Provider設定を構築する。
bgutil_config = BgutilProviderConfig.from_mapping(merged_config)
# PLANA/ARONAで共有するProvider Managerを一度だけ生成する。
bgutil_manager = BgutilProviderManager(bgutil_config)
# /download_* 用の localhost 一時配信(Cloudflare Tunnel 公開・DB 再起動耐性)
media_share_config = MediaShareConfig.from_mapping(merged_config)
media_share_server = TunnelFileServer(
media_share_config,
settings_db=resolve_settings_db(),
)
# 通常チャット / 討論の並列背圧を初期化する
try:
# Chat と Debate で枠を分離する
init_concurrency(merged_config)
print("INFO: LLM 並列背圧(concurrency)を初期化しました。")
except Exception as e_conc:
print(f"CRITICAL: concurrency 初期化中にエラーが発生しました: {e_conc}")
sys.exit(1)
# --- Discord Intents / AllowedMentions の設定 ---
# 両ボット共通の Intents を作成する
intents = discord.Intents.default()
intents.guilds = True
intents.guild_messages = True
intents.dm_messages = True
intents.voice_states = True
# Message Content Intent(特権)を明示ON — Developer Portal側も両Botで有効必須
intents.message_content = True
intents.members = False
intents.presences = False
# メンション設定を作成する
allowed_mentions = discord.AllowedMentions(everyone=False, users=True, roles=False, replied_user=True)
# --- PLANA(プライマリボット)の作成 ---
# PLANA の設定ブロックを取得する
plana_bot_config = merged_config['bots']['plana']
# PLANA のトークンを取得する
plana_token = plana_bot_config['token']
# PLANA のペルソナキーを取得する
plana_persona = plana_bot_config.get('persona', 'plana')
# PLANA の表示名を取得する
plana_display = plana_bot_config.get('display_name', 'PLANA')
# PLANA ボットインスタンスを作成する
plana_bot = Momoka(
command_prefix=commands.when_mentioned,
intents=intents,
help_command=None,
allowed_mentions=allowed_mentions,
config=merged_config,
bot_id='plana',
bot_role='primary',
persona_key=plana_persona,
display_name=plana_display,
cogs_to_load=PRIMARY_COGS,
enable_discord_logging=True, # primary のみ Discord へログ出力
)
# レジストリへ PLANA を登録する
registry.register('plana', plana_bot, plana_display)
# 一時配信サーバを primary に渡す(cog が参照)
plana_bot.media_share_server = media_share_server
print(f"INFO: PLANA ボットを作成し、レジストリへ登録しました。")
# --- ARONA(コンパニオンボット)の作成 ---
# ARONA の設定ブロックを取得する
arona_bot_config = merged_config['bots']['arona']
# ARONA のトークンを取得する
arona_token = arona_bot_config['token']
# ARONA のペルソナキーを取得する
arona_persona = arona_bot_config.get('persona', 'arona')
# ARONA の表示名を取得する
arona_display = arona_bot_config.get('display_name', 'ARONA')
# ARONA ボットインスタンスを作成する
arona_bot = Momoka(
command_prefix=commands.when_mentioned,
intents=intents,
help_command=None,
allowed_mentions=allowed_mentions,
config=merged_config,
bot_id='arona',
bot_role='companion',
persona_key=arona_persona,
display_name=arona_display,
cogs_to_load=COMPANION_COGS,
enable_discord_logging=False, # companion は Discord ログ出力しない
)
# レジストリへ ARONA を登録する
registry.register('arona', arona_bot, arona_display)
print(f"INFO: ARONA ボットを作成し、レジストリへ登録しました。")
# GUI 稼働モニタ / シャットダウンボタンから参照できるように PLANA を共有する
set_bot_ref(plana_bot)
# ===============================================================
# ===== シャットダウン / Cogリロードコマンド(PLANA のみ) =======
# ===============================================================
@plana_bot.tree.command(
name="shutdown",
description="Shut down both bots after notifying users (owner only).",
)
async def shutdown_command(interaction: discord.Interaction):
# support.developer_user_id のみ許可する(未設定なら誰も不可)
allowed_uid = shutdown_allowed_user_id(
getattr(plana_bot, "config", None)
)
# 未設定または不一致なら拒否する
if allowed_uid is None or interaction.user.id != allowed_uid:
# 権限不足を ephemeral で返す
await interaction.response.send_message(
"❌ You are not allowed to use this command.",
ephemeral=True,
)
# 処理終了
return
# シャットダウン開始を応答する
await interaction.response.send_message(
"Shutting down both bots... Active users will be notified.",
ephemeral=False,
)
# 実行者をログに残す
logging.info(
"/shutdown was executed by user %s (%s)",
interaction.user,
interaction.user.id,
)
# レジストリの全ボットをクローズする(再起動通知を含む)
await registry.close_all()
@plana_bot.tree.command(name="list_plana_cogs", description="List loaded PLANA cogs.")
async def list_plana_cogs(interaction: discord.Interaction):
# ロード済み拡張機能のリストを取得する
loaded_extensions = list(plana_bot.extensions.keys())
if not loaded_extensions:
await interaction.response.send_message("現在ロードされているCogはありません。", ephemeral=False)
return
# Cog リストを整形する
cog_list = "\n".join([f"• `{ext}`" for ext in sorted(loaded_extensions)])
await interaction.response.send_message(
f"**PLANA ロード済みCog一覧** ({len(loaded_extensions)}個):\n{cog_list}",
ephemeral=False
)
# --- 両ボットの起動 ---
async def run_bots():
"""PLANA と ARONA の両方を並行起動する。"""
try:
try:
# Discord接続前にPO Token Providerを準備する。
provider_ready = await bgutil_manager.start()
except Exception as e_provider:
# 必須設定では不完全な音楽機能で起動しない。
if bgutil_config.required:
# 元の起動失敗を外側へ伝える。
raise
# 任意設定ならProvider無しの劣化運転を選ぶ。
provider_ready = False
# 劣化運転を明確に警告する。
logging.warning(
"BgUtils Provider is unavailable; continuing without it: %s",
e_provider,
)
# yt-dlpラッパーへProvider接続先を共有する。
from MOMOKA.music.plugins.ytdlp_wrapper import (
set_bgutil_provider_base_url,
)
# Provider利用可能時だけplugin URLを有効化する。
set_bgutil_provider_base_url(
bgutil_manager.base_url if provider_ready else None
)
# Cloudflare Tunnel 向け localhost 配信を起動する
try:
media_share_ready = await media_share_server.start()
except Exception as e_share:
logging.error(
"media_share failed to start: %s", e_share, exc_info=True
)
media_share_ready = False
if not media_share_ready:
logging.warning(
"media_share is not ready; /download_* share links will fail "
"until external_services.media_share is configured."
)
# 両ボットを並行起動する
await asyncio.gather(
plana_bot.start(plana_token),
arona_bot.start(arona_token),
)
except Exception as e_run:
# 実行中のエラーをログに残す
logging.critical(f"ボットの実行中に致命的なエラーが発生しました: {e_run}", exc_info=True)
print(f"CRITICAL: ボットの実行中に致命的なエラーが発生しました: {e_run}")
# 外側の終了コード処理へ失敗を伝える。
raise
finally:
# キャンセル・Ctrl+Cを含む全経路で両Botを閉じる。
await registry.close_all()
# 一時配信を止める
await media_share_server.stop()
# Bot停止後に自分が所有するProviderだけを停止する。
await bgutil_manager.stop()
# ホスト Electron GUI も落とす(コンソール pause 阻害防止)
try:
from MOMOKA.GUI.runner import stop_host_gui
stop_host_gui()
except Exception:
pass
# asyncio イベントループで両ボットを起動する
try:
asyncio.run(run_bots())
except KeyboardInterrupt:
# 既に run_bots 内で処理済み
print("INFO: シャットダウン完了。")
except Exception as e_main:
logging.critical(f"メインループで致命的なエラーが発生しました: {e_main}", exc_info=True)
print(f"CRITICAL: メインループで致命的なエラーが発生しました: {e_main}")
sys.exit(1)