-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
445 lines (331 loc) · 16 KB
/
bot.py
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
import os
import datetime
import asyncio
import discord
from discord.ext import tasks
from natsort import natsorted
from game import Game
RAISED_HAND = "\N{Raised Hand}"
participation_message = None
try:
with open("TOKEN.txt", mode = "r") as f:
TOKEN = f.read()
except:
print("bot のトークンを TOKEN.txt に書き込んでください")
exit()
try:
with open("home_channel_id.txt", mode = "r") as f:
HOME_CHANNEL_ID = int(f.read())
except:
HOME_CHANNEL_ID = None
client = discord.Client(intents=discord.Intents.all())
@client.event
async def on_ready():
global game
game = Game(client, HOME_CHANNEL_ID)
if HOME_CHANNEL_ID is not None:
category = client.get_channel(HOME_CHANNEL_ID).category
log_channel = None
for channel_i in category.text_channels:
if channel_i.name == "log":
log_channel = channel_i
if log_channel is None:
permission = {
category.guild.default_role: discord.PermissionOverwrite(read_messages=False),
client.user: discord.PermissionOverwrite(read_messages=True)
}
log_channel = await category.create_text_channel(name="log", overwrites=permission)
await log_channel.send("Activated")
try:
game.load()
if game.is_ongoing:
print("Latest game loaded")
if game.current_turn == game.number_of_participants - 1:
await game.next_job()
except:
game.is_accepting = 0
game.is_ongoing = 0
print("Latest game did not load")
daily_job.start()
print("Successfully activated")
@client.event
async def on_message(message):
global HOME_CHANNEL_ID, game, participation_message
game_admin_role = discord.utils.get(message.guild.roles, name="1WGP_admin")
if game_admin_role is None:
game_admin_role = await message.guild.create_role(name="1WGP_admin")
have_admin_permission = message.author.guild_permissions.administrator or game_admin_role in message.author.roles
if not message.content.startswith("!"):
return
elif message.content.startswith("!set_home_channel"):
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
HOME_CHANNEL_ID = message.channel.id
with open("home_channel_id.txt", mode = "w") as f:
f.write(str(HOME_CHANNEL_ID))
await message.channel.send("このチャンネルを 1WGP のホームチャンネルに設定しました")
elif HOME_CHANNEL_ID is None:
await message.channel.send("```\n!set_home_channel\n```でホームチャンネルを設定してください")
elif message.content.startswith("!give_admin_role"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
for user_mention in message.mentions:
member = message.guild.get_member(user_mention.id)
await member.add_roles(game_admin_role)
await message.channel.send(f"{member.mention} にゲーム管理者のロールを付与しました")
elif message.content.startswith("!new_game"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_ongoing:
await message.channel.send("現在進行中のゲームがあるようです。新しくゲームを始める場合、 \
```\n!cancel_game\n```\nを用いて進行中のゲームを中断してください")
else:
participation_message = await message.channel.send(f"参加希望者はこのメッセージに {RAISED_HAND} のリアクションをつけてください")
await participation_message.add_reaction(RAISED_HAND)
game = Game(client, HOME_CHANNEL_ID)
elif message.content.startswith("!confirm"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_accepting:
participant_mention_list = [participant_i.mention for participant_i in game.participants]
confirm_message = "以下のメンバーでゲームを開始します\n" + "\n".join(participant_mention_list)
await message.channel.send(confirm_message)
game.start()
game.save()
for individual_channel in game.individual_channels:
await individual_channel.send(f"{game.deadline} 23:59 までにお題を送信してください")
else:
await message.channel.send("```\n!new_game\n```\nを用いてゲーム参加者を募集してください")
elif message.content.startswith("!cancel_game"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
game.reset()
await message.channel.send("現在進行中のゲームを中断しました")
elif message.content.startswith("!skip_turn"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_ongoing:
await game.next_job()
elif message.content.startswith("!show_subjects"):
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_ongoing:
subjects = []
for turn in range(0, game.current_turn, 2):
for game_index in range(game.number_of_participants):
target_path = os.path.join(game.start_date, str(turn), str(game_index))
file_name = natsorted(os.listdir(target_path))[-1]
with open(os.path.join(target_path, file_name), mode = "r") as f:
subjects.append(f"{turn} {game_index} ```{f.read()}```")
await message.channel.send("\n".join(subjects))
elif message.content.startswith("!show_turn"):
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_ongoing:
await message.channel.send(f"{game.current_turn}")
elif message.content.startswith("!add_participants"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_accepting:
for member in message.mentions:
# TODO: あとで on_reaction_add とまとめる。複数回追加される可能性あり
user = client.get_user(member.id)
category = message.channel.category
channel_name = user.name
game.append_participant(user)
# 目的のチャンネルがすでに作られているとき
channel_exists = 0
for channel_i in category.text_channels:
if channel_name == channel_i.name:
game.append_channel(channel_i)
channel_exists = 1
break
if channel_exists:
continue
# プライベートチャンネルに設定するための dict
permission = {
message.guild.default_role: discord.PermissionOverwrite(read_messages=False),
user: discord.PermissionOverwrite(read_messages=True),
client.user: discord.PermissionOverwrite(read_messages=True)
}
channel = await category.create_text_channel(name=channel_name, overwrites=permission)
game.append_channel(channel)
await channel.send("このチャンネルで画像、テキストの送受信をしてください")
elif message.content.startswith("!remind"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_ongoing:
sending_object = "お題" if game.current_turn == 0 else "絵" if game.current_turn % 2 else "絵の説明"
for user_i in game.participants:
if user_i in game.completed_users:
continue
for channel_i in game.individual_channels:
if channel_i.name == user_i.name:
await channel_i.send(f"現在提出が確認されていません。{game.deadline} 23:59 までに{sending_object}を送信してください。")
break
elif message.content.startswith("!next"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
if game.is_ongoing:
game.is_waiting_for_next = 0
elif message.content.startswith("!reboot"):
if message.channel.id != HOME_CHANNEL_ID:
return
if not have_admin_permission:
await message.channel.send("このコマンドを実行する権限がありません")
return
exit()
elif message.content.startswith("!help"):
help_images = [
discord.File(os.path.join("help", "set_home_channel.png")),
discord.File(os.path.join("help", "new_game.png")),
discord.File(os.path.join("help", "confirm.png")),
discord.File(os.path.join("help", "send_subject.png")),
discord.File(os.path.join("help", "send_picture.png")),
]
with open(os.path.join("help", "message.txt"), mode = "r") as f:
help_message = f.read()
await message.channel.send(help_message, files=help_images)
elif message.content.startswith("!progress"):
if game.is_ongoing:
await message.channel.send(
f"現在の提出状況\n{len(game.completed_users)} / {game.number_of_participants}")
else:
await message.channel.send("現在進行中のゲームはないようです")
elif message.content.startswith("!send_subject"):
if message.channel not in game.individual_channels:
return
if message.channel.name != message.author.name and not have_admin_permission:
return
if not game.is_ongoing:
await message.channel.send("現在進行中のゲームがありません")
return
if game.current_turn % 2 == 1:
await message.channel.send("現在、絵を送信するフェーズです。\n```\n!send_picture\n```\nを用いて画像を送信してください")
return
for user_i in game.participants:
if user_i.name == message.channel.name:
author = user_i
break
user_index = game.passing_table[game.current_turn].index(author)
target_path = os.path.join(game.start_date, str(game.current_turn), str(user_index))
files = os.listdir(target_path)
accepted_subject = message.content.replace("!send_subject", "")
with open(os.path.join(target_path, f"{len(files)}_subject.txt"), mode = "w") as f:
f.write(accepted_subject)
await message.channel.send(f"以下の文章を受理しました\n```\n{accepted_subject}\n```")
game.completed_users.add(author)
if len(game.completed_users) == game.number_of_participants and not game.is_in_phase_transition:
game.is_in_phase_transition = 1
await asyncio.sleep(300)
await game.next_job()
game.is_in_phase_transition = 0
elif message.content.startswith('!send_picture'):
if message.channel not in game.individual_channels:
return
if message.channel.name != message.author.name and not have_admin_permission:
return
if not game.is_ongoing:
await message.channel.send("現在進行中のゲームがありません")
return
if game.current_turn % 2 == 0:
await message.channel.send("現在、お題もしくは絵の説明を送信するフェーズです。\n```\n!send_subject\n```\nを用いて文章を送信してください")
return
for user_i in game.participants:
if user_i.name == message.channel.name:
author = user_i
break
user_index = game.passing_table[game.current_turn].index(author)
target_path = os.path.join(game.start_date, str(game.current_turn), str(user_index))
attachment = message.attachments[0]
files = os.listdir(target_path)
file_name = os.path.join(target_path, f"{len(files)}_{attachment.filename}")
await attachment.save(file_name)
await message.channel.send(f"以下の画像を受理しました", file=discord.File(file_name))
game.completed_users.add(author.id)
if len(game.completed_users) == game.number_of_participants and not game.is_in_phase_transition:
game.is_in_phase_transition = 1
await asyncio.sleep(300)
await game.next_job()
game.is_in_phase_transition = 0
else:
await message.channel.send("定義されていないコマンドです")
@client.event
async def on_reaction_add(reaction, user):
global game, participation_message
message = reaction.message
category = message.channel.category
channel_name = user.name
if participation_message is None:
return
if message != participation_message or user == client.user:
return
game.append_participant(user)
# 目的のチャンネルがすでに作られているとき
for channel_i in category.text_channels:
if channel_name == channel_i.name:
game.append_channel(channel_i)
return
# プライベートチャンネルに設定するための dict
permission = {
message.guild.default_role: discord.PermissionOverwrite(read_messages=False),
user: discord.PermissionOverwrite(read_messages=True),
client.user: discord.PermissionOverwrite(read_messages=True)
}
channel = await category.create_text_channel(name=channel_name, overwrites=permission)
game.append_channel(channel)
await channel.send("このチャンネルで画像、テキストの送受信をしてください")
@client.event
async def on_reaction_remove(reaction, user):
global game, participation_message
message = reaction.message
if participation_message is None:
return
if message != participation_message or user == client.user:
return
game.remove_participant(user)
for channel_i in message.channel.category.text_channels:
if user.name == channel_i.name:
game.remove_channel(channel_i)
return
@tasks.loop(seconds=60)
async def daily_job():
global game
if not game.is_ongoing:
return
now = datetime.datetime.now().strftime("%H:%M")
today = str(datetime.date.today())
if game.is_in_phase_transition:
return
if today == game.deadline and now == "23:59":
await game.next_job()
elif today > game.deadline:
await game.next_job()
client.run(TOKEN)