Skip to content

Commit c1cdd60

Browse files
平地浩一claude
authored andcommitted
channel_idとis_call_end_notification_enabledをD1に永続化
再起動時に設定が初期化される問題を修正: - bot_settingsテーブルを追加 - 通知先チャンネルと終了通知設定をD1に保存・読込 - リセット時にも設定を保存するように修正 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5b219a3 commit c1cdd60

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

db_utils.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def init_db():
5454
is_target BOOLEAN NOT NULL
5555
)
5656
""")
57+
58+
execute_d1_query("""
59+
CREATE TABLE IF NOT EXISTS bot_settings (
60+
key TEXT PRIMARY KEY,
61+
value TEXT NOT NULL
62+
)
63+
""")
5764
print("D1 database initialized")
5865

5966

@@ -91,3 +98,41 @@ def load_is_target_channels() -> Dict[int, bool]:
9198
int(row["channel_id"]): bool(row["is_target"])
9299
for row in results
93100
}
101+
102+
103+
def save_channel_id(channel_id: int):
104+
"""通知先チャンネルIDをD1に保存"""
105+
execute_d1_query(
106+
"INSERT OR REPLACE INTO bot_settings (key, value) VALUES (?, ?)",
107+
["channel_id", str(channel_id)]
108+
)
109+
110+
111+
def load_channel_id() -> Optional[int]:
112+
"""通知先チャンネルIDをD1から読み込み"""
113+
result = execute_d1_query("SELECT value FROM bot_settings WHERE key = ?", ["channel_id"])
114+
results = result.get("results", [])
115+
116+
if results and len(results) > 0:
117+
return int(results[0]["value"])
118+
119+
return None
120+
121+
122+
def save_call_end_notification_enabled(enabled: bool):
123+
"""終了通知設定をD1に保存"""
124+
execute_d1_query(
125+
"INSERT OR REPLACE INTO bot_settings (key, value) VALUES (?, ?)",
126+
["is_call_end_notification_enabled", "1" if enabled else "0"]
127+
)
128+
129+
130+
def load_call_end_notification_enabled() -> Optional[bool]:
131+
"""終了通知設定をD1から読み込み"""
132+
result = execute_d1_query("SELECT value FROM bot_settings WHERE key = ?", ["is_call_end_notification_enabled"])
133+
results = result.get("results", [])
134+
135+
if results and len(results) > 0:
136+
return results[0]["value"] == "1"
137+
138+
return None

mycommands.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from params import *
44
from bot_config import *
55
import params
6-
from db_utils import save_notitext, save_is_target_channel
6+
from db_utils import save_notitext, save_is_target_channel, save_channel_id, save_call_end_notification_enabled
77

88

99
class CallNotification(app_commands.Group):
@@ -17,6 +17,7 @@ def __init__(self, name: str, client: discord.Client):
1717
async def set(self, interaction: Interaction):
1818
global channel_id
1919
channel_id = interaction.channel.id
20+
save_channel_id(channel_id) # データベースに保存
2021
await interaction.response.send_message("変更しました!")
2122

2223
class CallEndNotificationChangeButton(ui.Button):
@@ -28,6 +29,7 @@ def __init__(self, label, change):
2829
async def callback(self, interaction: discord.Interaction):
2930
global is_call_end_notification_enabled
3031
is_call_end_notification_enabled = self.change
32+
save_call_end_notification_enabled(self.change) # データベースに保存
3133
await interaction.response.edit_message(
3234
content=f"{self.label}に変更します", view=None
3335
)
@@ -64,17 +66,23 @@ async def callback(self, interaction: discord.Interaction):
6466
global channel_id, is_call_end_notification_enabled, notitext
6567
if self.initial == INITIAL_CHANNEL:
6668
channel_id = INITIAL_CHANNEL
69+
save_channel_id(INITIAL_CHANNEL) # データベースに保存
6770
resetmessage = self.client.get_channel(INITIAL_CHANNEL).name
6871
elif self.initial == INITIAL_FLAG:
6972
is_call_end_notification_enabled = INITIAL_FLAG
73+
save_call_end_notification_enabled(INITIAL_FLAG) # データベースに保存
7074
resetmessage = "終了時にも通知を行う"
7175
elif self.initial == INITIAL_TEXT:
7276
notitext = INITIAL_TEXT
77+
save_notitext(INITIAL_TEXT) # データベースに保存
7378
resetmessage = INITIAL_TEXT
7479
elif self.initial == "allreset":
7580
channel_id = INITIAL_CHANNEL
7681
is_call_end_notification_enabled = INITIAL_FLAG
7782
notitext = INITIAL_TEXT
83+
save_channel_id(INITIAL_CHANNEL) # データベースに保存
84+
save_call_end_notification_enabled(INITIAL_FLAG) # データベースに保存
85+
save_notitext(INITIAL_TEXT) # データベースに保存
7886
resetmessage = self.initial
7987
await interaction.response.edit_message(
8088
content=f"{resetmessage}", view=None

params.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import os
2-
from db_utils import load_notitext, load_is_target_channels
2+
from db_utils import (
3+
load_notitext,
4+
load_is_target_channels,
5+
load_channel_id,
6+
load_call_end_notification_enabled,
7+
)
38

49
INITIAL_CHANNEL = int(os.environ['DISCORD_CHANNEL_ID'])
510
INITIAL_TEXT = "@everyone"
611
INITIAL_FLAG = True
712

8-
channel_id = INITIAL_CHANNEL
9-
is_call_end_notification_enabled = INITIAL_FLAG
1013
e_time = {}
1114
try:
1215
notitext = load_notitext() # データベースから読み込み
1316
is_target_channel = load_is_target_channels() # データベースから読み込み
17+
# channel_idをデータベースから読み込み、なければ初期値を使用
18+
loaded_channel_id = load_channel_id()
19+
channel_id = loaded_channel_id if loaded_channel_id is not None else INITIAL_CHANNEL
20+
# is_call_end_notification_enabledをデータベースから読み込み、なければ初期値を使用
21+
loaded_end_notification = load_call_end_notification_enabled()
22+
is_call_end_notification_enabled = loaded_end_notification if loaded_end_notification is not None else INITIAL_FLAG
1423
except:
1524
notitext = INITIAL_TEXT
16-
is_target_channel = {}
25+
is_target_channel = {}
26+
channel_id = INITIAL_CHANNEL
27+
is_call_end_notification_enabled = INITIAL_FLAG

0 commit comments

Comments
 (0)