jam134 commited on
Commit
def8a8e
·
verified ·
1 Parent(s): 6493f30

Update bots/literal_prison.py

Browse files
Files changed (1) hide show
  1. bots/literal_prison.py +195 -183
bots/literal_prison.py CHANGED
@@ -1,184 +1,196 @@
1
-
2
- import discord
3
- from discord.ext import commands
4
- import json
5
- import os
6
- import asyncio
7
- import sys
8
-
9
- # 設定 intents
10
- intents = discord.Intents.default()
11
- intents.message_content = True
12
- intents.messages = True
13
- intents.guilds = True
14
- bot = commands.Bot(command_prefix='!', intents=intents)
15
-
16
- # 禁止連結文件的路徑(相對於專案資料夾)
17
- banned_links_file = os.path.join(os.path.dirname(__file__), 'banned_links.json')
18
-
19
- # 載入禁止連結
20
- def load_banned_links():
21
-
22
- if os.path.exists(banned_links_file) and os.path.getsize(banned_links_file) > 0:
23
- try:
24
- with open(banned_links_file, 'r') as file:
25
- data = json.load(file)
26
-
27
- # 確保返回的是字典
28
- if isinstance(data, dict):
29
- return data
30
- else:
31
- print(f"Error: Expected a dictionary but got {type(data).__name__}")
32
- except json.JSONDecodeError as e:
33
- print(f"JSON decode error: {e}")
34
- except IOError as e:
35
- print(f"File read error: {e}")
36
- return {} # 返回空字典
37
-
38
- # 儲存禁止連結
39
- def save_banned_links(banned_links):
40
- print(f"Attempting to save to {banned_links_file}")
41
- try:
42
- with open(banned_links_file, 'w') as file:
43
- json.dump(banned_links, file, indent=4)
44
- print(f'Successfully saved banned links to {banned_links_file}')
45
- except IOError as e:
46
- print(f'File write error: {e}')
47
- except Exception as e:
48
- print(f'Error saving banned links: {e}')
49
-
50
- # 當 bot 準備好時執行
51
- @bot.event
52
- async def on_ready():
53
- print(f'Logged in as {bot.user}')
54
- print('Bot is ready!')
55
- # 開始控制台輸入功能
56
- asyncio.create_task(console_input_loop())
57
-
58
- # 檢查消息內容
59
- @bot.event
60
- async def handle_message(message):
61
- if message.author == bot.user:
62
- return
63
-
64
- banned_links = load_banned_links().get(str(message.guild.id), [])
65
- if isinstance(banned_links, list): # 確保 banned_links 是列表
66
- for link in banned_links:
67
- if link in message.content:
68
- await message.delete()
69
- await message.channel.send(f'{message.author.mention} 你的消息包含了禁止的連結!')
70
- break
71
- else:
72
- print(f"Error: Expected a list but got {type(banned_links).__name__}")
73
-
74
- # 確保指令能夠被處理
75
-
76
-
77
- # 添加禁止連結
78
- @bot.command(name='alink')
79
- @commands.has_permissions(administrator=True)
80
- async def add_banned_link(ctx, *, link):
81
- print(f'Received add banned link command with link: {link}')
82
- banned_links = load_banned_links()
83
- guild_id = str(ctx.guild.id)
84
-
85
- if guild_id not in banned_links:
86
- banned_links[guild_id] = [] # 確保 banned_links[guild_id] 是一個列表
87
-
88
- if link in banned_links[guild_id]:
89
- await ctx.send('這個連結已經被禁止了。')
90
- return
91
-
92
- banned_links[guild_id].append(link)
93
- save_banned_links(banned_links)
94
- await ctx.send(f'已添加禁止連結: {link}')
95
-
96
- # 刪除禁止連結
97
- @bot.command(name='dlink')
98
- @commands.has_permissions(administrator=True)
99
- async def remove_banned_link(ctx, *, link):
100
- print(f'Received remove banned link command with link: {link}')
101
- banned_links = load_banned_links()
102
- guild_id = str(ctx.guild.id)
103
-
104
- if guild_id not in banned_links or link not in banned_links[guild_id]:
105
- await ctx.send('這個連結不存在於禁止列表中。')
106
- return
107
-
108
- banned_links[guild_id].remove(link)
109
- save_banned_links(banned_links)
110
- await ctx.send(f'已移除禁止連結: {link}')
111
-
112
- # 控制台輸入循環
113
- @bot.event
114
- async def on_ready():
115
- print(f'Logged in as {bot.user.name}')
116
- print('Bot is ready!')
117
- # 開始控制台輸入功能
118
- asyncio.create_task(console_input_loop())
119
-
120
- async def console_input_loop():
121
- while True:
122
- try:
123
- command = await asyncio.get_event_loop().run_in_executor(None, input, "輸入指令: ")
124
- command = command.strip().lower()
125
-
126
- if command == 'serverinfo':
127
- print("\n已加入的伺服器:")
128
- for index, guild in enumerate(bot.guilds):
129
- print(f"{index}: 伺服器名稱: {guild.name}, 伺服器 ID: {guild.id}")
130
- try:
131
- guild_index = int(await asyncio.get_event_loop().run_in_executor(None, input, "選擇伺服器 (輸入數字): "))
132
- if 0 <= guild_index < len(bot.guilds):
133
- guild = bot.guilds[guild_index]
134
- banned_links = load_banned_links()
135
- guild_id = str(guild.id)
136
- if guild_id not in banned_links:
137
- banned_links[guild_id] = [] # 確保 banned_links[guild_id] 是一個��表
138
-
139
- print(f"\n伺服器 {guild.name} 的禁止連結:")
140
- if banned_links[guild_id]:
141
- for link in banned_links[guild_id]:
142
- print(link)
143
- else:
144
- print(f"伺服器 {guild.name} 沒有禁止連結。")
145
-
146
- # 在選擇伺服器後,添加或刪除禁止連結
147
- while True:
148
- action = await asyncio.get_event_loop().run_in_executor(None, input, "輸入操作 (添加: 'add', 刪除: 'remove', 結束: 'exit'): ")
149
- action = action.strip().lower()
150
-
151
- if action == 'add':
152
- link = await asyncio.get_event_loop().run_in_executor(None, input, "輸入要添加的禁止連結: ")
153
- link = link.strip()
154
- if link:
155
- if link not in banned_links[guild_id]:
156
- banned_links[guild_id].append(link)
157
- save_banned_links(banned_links) # 確保保存的是更新後的 banned_links
158
- print(f'已添加禁止連結: {link} 到伺服器 {guild.name}')
159
- else:
160
- print(f'這個連結已經被禁止了。')
161
- elif action == 'remove':
162
- link = await asyncio.get_event_loop().run_in_executor(None, input, "輸入要刪除的禁止連結: ")
163
- link = link.strip()
164
- if link:
165
- if link in banned_links[guild_id]:
166
- banned_links[guild_id].remove(link)
167
- save_banned_links(banned_links) # 確保保存的是更新後的 banned_links
168
- print(f'已移除禁止連結: {link} 從伺服器 {guild.name}')
169
- else:
170
- print(f'這個連結不存在於禁止列表中。')
171
- elif action == 'exit':
172
- break
173
- else:
174
- print("不支援的操作。")
175
- else:
176
- print("請輸入有效的數字。")
177
- except ValueError:
178
- print("請輸入有效的數字。")
179
- else:
180
- print("不支援的指令。")
181
- except KeyboardInterrupt:
182
- print('程式結束')
183
- await bot.close()
 
 
 
 
 
 
 
 
 
 
 
 
184
  sys.exit()
 
1
+
2
+ import discord
3
+ from discord.ext import commands
4
+ import json
5
+ import os
6
+ import asyncio
7
+ import sys
8
+
9
+ # 設定 intents
10
+ intents = discord.Intents.default()
11
+ intents.message_content = True
12
+ intents.messages = True
13
+ intents.guilds = True
14
+ bot = commands.Bot(command_prefix='!', intents=intents)
15
+
16
+ # 禁止連結文件的路徑(相對於專案資料夾)
17
+ banned_links_file = os.path.join(os.path.dirname(__file__), 'banned_links.json')
18
+
19
+ # 載入禁止連結
20
+ def load_banned_links():
21
+
22
+ if os.path.exists(banned_links_file) and os.path.getsize(banned_links_file) > 0:
23
+ try:
24
+ with open(banned_links_file, 'r') as file:
25
+ data = json.load(file)
26
+
27
+ # 確保返回的是字典
28
+ if isinstance(data, dict):
29
+ return data
30
+ else:
31
+ print(f"Error: Expected a dictionary but got {type(data).__name__}")
32
+ except json.JSONDecodeError as e:
33
+ print(f"JSON decode error: {e}")
34
+ except IOError as e:
35
+ print(f"File read error: {e}")
36
+ return {} # 返回空字典
37
+
38
+ # 儲存禁止連結
39
+ def save_banned_links(banned_links):
40
+ print(f"Attempting to save to {banned_links_file}")
41
+ try:
42
+ with open(banned_links_file, 'w') as file:
43
+ json.dump(banned_links, file, indent=4)
44
+ print(f'Successfully saved banned links to {banned_links_file}')
45
+ except IOError as e:
46
+ print(f'File write error: {e}')
47
+ except Exception as e:
48
+ print(f'Error saving banned links: {e}')
49
+
50
+ # 當 bot 準備好時執行
51
+ @bot.event
52
+ async def on_ready():
53
+ print(f'Logged in as {bot.user}')
54
+ print('Bot is ready!')
55
+ # 開始控制台輸入功能
56
+ asyncio.create_task(console_input_loop())
57
+
58
+ # 檢查消息內容
59
+ @bot.event
60
+ async def handle_message(message):
61
+ if message.author == bot.user:
62
+ return
63
+
64
+ banned_links = load_banned_links().get(str(message.guild.id), [])
65
+ if isinstance(banned_links, list): # 確保 banned_links 是列表
66
+ for link in banned_links:
67
+ if link in message.content:
68
+ await message.delete()
69
+ await message.channel.send(f'{message.author.mention} 你的消息包含了禁止的連結!')
70
+ break
71
+ else:
72
+ print(f"Error: Expected a list but got {type(banned_links).__name__}")
73
+
74
+ # 確保指令能夠被處理
75
+
76
+
77
+ # 添加禁止連結
78
+ @bot.command(name='alink')
79
+ @commands.has_permissions(administrator=True)
80
+ async def add_banned_link(ctx, *, link):
81
+ print(f'Received add banned link command with link: {link}')
82
+ banned_links = load_banned_links()
83
+ guild_id = str(ctx.guild.id)
84
+
85
+ if guild_id not in banned_links:
86
+ banned_links[guild_id] = [] # 確保 banned_links[guild_id] 是一個列表
87
+
88
+ if link in banned_links[guild_id]:
89
+ await ctx.send('這個連結已經被禁止了。')
90
+ return
91
+
92
+ banned_links[guild_id].append(link)
93
+ save_banned_links(banned_links)
94
+ await ctx.send(f'已添加禁止連結: {link}')
95
+
96
+ # 刪除禁止連結
97
+ @bot.command(name='dlink')
98
+ @commands.has_permissions(administrator=True)
99
+ async def remove_banned_link(ctx, *, link):
100
+ print(f'Received remove banned link command with link: {link}')
101
+ banned_links = load_banned_links()
102
+ guild_id = str(ctx.guild.id)
103
+
104
+ if guild_id not in banned_links or link not in banned_links[guild_id]:
105
+ await ctx.send('這個連結不存在於禁止列表中。')
106
+ return
107
+
108
+ banned_links[guild_id].remove(link)
109
+ save_banned_links(banned_links)
110
+ await ctx.send(f'已移除禁止連結: {link}')
111
+
112
+ @bot.command(name='plinks')
113
+ async def list_banned_links(ctx):
114
+ banned_links = load_banned_links()
115
+ guild_id = str(ctx.guild.id)
116
+
117
+ if guild_id not in banned_links or not banned_links[guild_id]:
118
+ await ctx.send('這個伺服器目前沒有禁止的連結。')
119
+ return
120
+
121
+ links = banned_links[guild_id]
122
+ links_list = '\n'.join(links)
123
+ await ctx.send(f'禁止的連結列表:\n{links_list}')
124
+ # 控制台輸入循環
125
+ @bot.event
126
+ async def on_ready():
127
+ print(f'Logged in as {bot.user.name}')
128
+ print('Bot is ready!')
129
+ # 開始控制台輸入功能
130
+ asyncio.create_task(console_input_loop())
131
+
132
+ async def console_input_loop():
133
+ while True:
134
+ try:
135
+ command = await asyncio.get_event_loop().run_in_executor(None, input, "輸入指令: ")
136
+ command = command.strip().lower()
137
+
138
+ if command == 'serverinfo':
139
+ print("\n已加入的伺服器:")
140
+ for index, guild in enumerate(bot.guilds):
141
+ print(f"{index}: 伺服器名稱: {guild.name}, 伺服器 ID: {guild.id}")
142
+ try:
143
+ guild_index = int(await asyncio.get_event_loop().run_in_executor(None, input, "選擇伺服器 (輸入數字): "))
144
+ if 0 <= guild_index < len(bot.guilds):
145
+ guild = bot.guilds[guild_index]
146
+ banned_links = load_banned_links()
147
+ guild_id = str(guild.id)
148
+ if guild_id not in banned_links:
149
+ banned_links[guild_id] = [] # 確保 banned_links[guild_id] 是一個列表
150
+
151
+ print(f"\n伺服器 {guild.name} 的禁止連結:")
152
+ if banned_links[guild_id]:
153
+ for link in banned_links[guild_id]:
154
+ print(link)
155
+ else:
156
+ print(f"伺服器 {guild.name} 沒有禁止連結。")
157
+
158
+ # 在選擇伺服器後,添加或刪除禁止連結
159
+ while True:
160
+ action = await asyncio.get_event_loop().run_in_executor(None, input, "輸入操作 (添加: 'add', 刪除: 'remove', 結束: 'exit'): ")
161
+ action = action.strip().lower()
162
+
163
+ if action == 'add':
164
+ link = await asyncio.get_event_loop().run_in_executor(None, input, "輸入要添加的禁止連結: ")
165
+ link = link.strip()
166
+ if link:
167
+ if link not in banned_links[guild_id]:
168
+ banned_links[guild_id].append(link)
169
+ save_banned_links(banned_links) # 確保保存的是更新後的 banned_links
170
+ print(f'已添加禁止連結: {link} 到伺服器 {guild.name}')
171
+ else:
172
+ print(f'這個連結已經被禁止了。')
173
+ elif action == 'remove':
174
+ link = await asyncio.get_event_loop().run_in_executor(None, input, "輸入要刪除的禁止連結: ")
175
+ link = link.strip()
176
+ if link:
177
+ if link in banned_links[guild_id]:
178
+ banned_links[guild_id].remove(link)
179
+ save_banned_links(banned_links) # 確保保存的是更新後的 banned_links
180
+ print(f'已移除禁止連結: {link} 從伺服器 {guild.name}')
181
+ else:
182
+ print(f'這個連結不存在於禁止列表中。')
183
+ elif action == 'exit':
184
+ break
185
+ else:
186
+ print("不支援的操作。")
187
+ else:
188
+ print("請輸入有效的數字。")
189
+ except ValueError:
190
+ print("請輸入有效的數字。")
191
+ else:
192
+ print("不支援的指令。")
193
+ except KeyboardInterrupt:
194
+ print('程式結束')
195
+ await bot.close()
196
  sys.exit()