MyLab専用Discord Bot 設計・実装ガイド¶
OpenClawとは別に、MyLabプロジェクト専用の機能を持つDiscord Botを実装します。
概要¶
このBotは、MyLabの既存機能(会話ログ保存、バズツイート収集、コード実行等)をDiscordから呼び出せるようにします。
OpenClawとの使い分け¶
| 用途 | 使用するBot |
|---|---|
| 一般的な質問・タスク | OpenClaw |
| 会話ログ保存 | MyLab Bot |
| バズツイート収集 | MyLab Bot |
| プロジェクト固有の操作 | MyLab Bot |
機能一覧¶
Phase 1: 基本機能¶
- ✅ 会話ログ保存 - Discordから会話をObsidianに保存
- ✅ ステータス確認 - Bot稼働状況の確認
- ✅ ヘルプ - コマンド一覧表示
Phase 2: 拡張機能¶
- 🔄 バズツイート収集 - Discordコマンドでバズツイート収集を開始
- 🔄 Web記事要約 - URLを送るとMarkdownで要約
- 🔄 コード実行 - Pythonスクリプトの実行
Phase 3: 高度な機能¶
- 🔄 Claude API統合 - DiscordからClaude APIを直接呼び出し
- 🔄 定期タスク - 定期的にバズツイート収集やレポート生成
- 🔄 通知機能 - タスク完了時にメンション通知
コマンド設計¶
スラッシュコマンド¶
/mylab help
- コマンド一覧を表示
/mylab save-log <title> <summary> [details]
- 会話ログをObsidianに保存
/mylab buzz [genre]
- バズツイート収集を開始
/mylab summarize <url>
- Web記事を要約
/mylab status
- Bot稼働状況を表示
メッセージコマンド(プレフィックス: $)¶
実装構成¶
scripts/discord_bot/
├── bot.py # メインBotスクリプト
├── commands/
│ ├── __init__.py
│ ├── log_commands.py # 会話ログ関連コマンド
│ ├── buzz_commands.py # バズツイート関連コマンド
│ └── utils_commands.py # ユーティリティコマンド
├── config.py # 設定ファイル
├── requirements.txt # 依存ライブラリ
└── README.md # 使用方法
セットアップ手順¶
1. Discord Bot の作成¶
docs/discord-bot-setup.md を参照してBot Tokenを取得
2. 依存関係のインストール¶
3. 設定ファイルの作成¶
Credentials/mylab_discord_bot.json を作成:
{
"bot_token": "MTxxxxxxxxxxxxx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"guild_id": "your-server-id",
"admin_users": ["your-user-id"],
"log_channel_id": "channel-id-for-logs",
"created_at": "2026-02-07",
"description": "MyLab専用Discord Bot"
}
4. Bot の起動¶
または、バックグラウンドで常駐:
使用例¶
会話ログ保存¶
/mylab save-log title:Discord連携実装 summary:OpenClawとMyLab Botを実装
Bot: ✅ 会話ログを保存しました!
📁 ローカル: logs/20260207_Discord連携実装.md
📝 Obsidian: vault/Resources/ConversationLogs/20260207_Discord連携実装.md
バズツイート収集¶
/mylab buzz genre:テクノロジー
Bot: 🔍 バズツイート収集を開始します...
ジャンル: テクノロジー
[30秒後]
✅ 収集完了!15件のツイートを取得しました。
📊 Google Sheets: [リンク]
Web記事要約¶
/mylab summarize url:https://example.com/article
Bot: 📄 記事を要約しています...
[20秒後]
✅ 要約完了!
📝 Obsidian: vault/Resources/Articles/20260207_記事タイトル.md
セキュリティ¶
権限管理¶
特定のユーザーのみコマンドを実行可能:
# bot.py
ADMIN_USERS = ["user-id-1", "user-id-2"]
@bot.slash_command()
async def admin_command(ctx):
if ctx.author.id not in ADMIN_USERS:
await ctx.respond("❌ このコマンドは管理者のみ実行可能です。")
return
レート制限¶
連続実行を防ぐ:
from discord.ext import commands
@commands.cooldown(1, 60, commands.BucketType.user)
@bot.slash_command()
async def buzz(ctx):
# 1ユーザーあたり60秒に1回まで
pass
トラブルシューティング¶
Bot が起動しない¶
Credentials/mylab_discord_bot.jsonが正しく設定されているか確認- Bot Token が有効か確認
コマンドが表示されない¶
- Bot がサーバーに招待されているか確認
guild_idが正しく設定されているか確認- Bot を再起動
スクリプトが実行されない¶
- パスが正しいか確認(絶対パスを使用推奨)
- Python環境が正しいか確認
高度な機能(Phase 3)¶
Claude API統合¶
import anthropic
@bot.slash_command()
async def ask(ctx, question: str):
"""Claude APIに質問"""
client = anthropic.Anthropic(api_key=CLAUDE_API_KEY)
message = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
messages=[{"role": "user", "content": question}]
)
await ctx.respond(message.content[0].text)
定期タスク¶
from discord.ext import tasks
@tasks.loop(hours=24)
async def daily_buzz_collection():
"""毎日バズツイートを収集"""
# バズツイート収集処理
pass
@bot.event
async def on_ready():
daily_buzz_collection.start()
デプロイオプション¶
ローカル実行(推奨)¶
- 自分のPCで常駐させる
- Windowsタスクスケジューラで自動起動
クラウド実行¶
- Heroku、Railway、Render等で常駐
- ただし、ローカルファイルへのアクセスが制限される
Docker実行¶
次のステップ¶
scripts/discord_bot/bot.pyの実装- コマンドの実装
- テスト実行
- 本番環境デプロイ