-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmamoru.py
More file actions
751 lines (676 loc) · 28.3 KB
/
mamoru.py
File metadata and controls
751 lines (676 loc) · 28.3 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# mamoru for git
import asyncio
import random
import discord
from discord.ext import commands, tasks
import datetime
import json
import ast
import os
import string
from dotenv import load_dotenv
from pathlib import Path
mode = input("gati or sub: ")
if mode == "":
mode = "sub"
print(mode, "で起動します")
# gati or sub
with open(f"../data/setting/{mode}.json", "r", encoding="utf-8") as file:
data = json.load(file) # JSONデータを辞書として読み込む
env_path = Path(__file__).resolve().parent.parent / ".env"
load_dotenv(dotenv_path=env_path)
web_url = os.getenv("REDIRECT_URL").replace("callback", "")
tukaikatachid = data["tukaikatachid"]
kadaichid = data["kadaichid"]
cmdchid = data["cmdchid"]
osirasechid = data["osirasechid"]
testchid = data["testchid"]
mentionchid = data["mentionchid"]
kanrishachid = data["kanrishachid"]
logchid = data["logchid"]
guildid = data["guildid"]
TOKEN = data["TOKEN"]
if not os.path.exists("../data/count.json"):
print("no file count.json")
exit(1)
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all(), help_command=None)
@bot.event
async def on_ready():
print("start GIT mode", mode)
global today
today = datetime.datetime.now().day # 1ケタday
await bot.change_presence(activity=discord.Game(f"{today}日だお"))
loop.start()
@tasks.loop(minutes=1)
async def loop():
global today
nowday = datetime.datetime.now().day
if nowday == today:
channel = bot.get_channel(osirasechid)
else:
today = nowday
await bot.change_presence(activity=discord.Game(f"{today}日だお"))
channel = bot.get_channel(osirasechid)
await channel.send(f"日付を{today}日に更新")
await daily()
await kda()
await kdl()
await auto_set() # ->set課題へ飛ばす
@bot.command()
async def h(ctx):
if ctx.channel.id != kanrishachid:
return
# await daily()
# await kda()
# await kdl()
await auto_set()
def count(user, type):
with open("../data/count.json", "r", encoding="utf-8") as file:
data = json.load(file)
find = False
for hoge in data:
if user == hoge["user"] and type == hoge["type"]:
hoge["count"] += 1
find = True
if not find:
data.append(
{
"user": user,
"type": type,
"count": 1,
}
)
with open("../data/count.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
@bot.command()
async def ao(ctx, name, youbi, target):
if ctx.channel.id != cmdchid:
return
try: # オンデマンド自動セット
weekdays = ["月", "火", "水", "木", "金", "土", "日"]
days = ["1", "2", "3", "4", "5", "6", "7"]
if not youbi in weekdays:
await ctx.send("arg2を月〜日で指定してください")
return
if not target in days:
await ctx.send("arg3を1-7で指定してください")
return
new_data = {"pushyoubi": youbi, "targetday": target, "name": name}
with open("../data/onde.json", "r", encoding="utf-8") as file:
data = json.load(file) # JSONデータを辞書として読み込む
find = False
for ff in data:
if ff["name"] == new_data["name"]:
ff["pushyoubi"] = new_data["pushyoubi"]
ff["targetday"] = new_data["targetday"]
find = True
await ctx.send(
f"上書き完了です。__{name}__という科目を__{youbi}曜日__になった瞬間自動でセットし、__{target}日後__を締め切りとして設定し直しました。"
)
if not find:
data.append(new_data)
await ctx.send(
f"登録完了です。__{name}__という科目を__{youbi}曜日__になった瞬間自動でセットし、__{target}日後__を締め切りとして設定しました。"
)
with open("../data/onde.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
except FileNotFoundError:
await ctx.send("onde.jsonが見つかりません")
async def auto_set():
channel = bot.get_channel(logchid)
tomorrow = datetime.date.today()
today = tomorrow.strftime("%m%d") # 今日をstr 4ケタで取得
try:
with open("../data/auto.json", "r", encoding="utf-8") as file:
data = json.load(file) # JSONデータを辞書として読み込む
for line in data:
if line["pushday"] == today and line["pushed"] == "no":
line["pushed"] = "yes"
await setkadai(line["name"], line["end"])
with open("../data/auto.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
except FileNotFoundError:
channel = bot.get_channel(logchid)
await channel.send("auto.jsonが見つかりません")
try: # オンデマンド自動セット
weekdays = ["月", "火", "水", "木", "金", "土", "日"]
sss = datetime.datetime.today().weekday()
youbi = weekdays[sss] # 今の月火水木金土日
with open("../data/onde.json", "r", encoding="utf-8") as file:
data = json.load(file) # JSONデータを辞書として読み込む
todaymmdd = datetime.date.today().strftime("%m%d")
for line in data:
if youbi == line["pushyoubi"]: # 対象の日付が今日なら
targetday = int(line["targetday"])
targetdaymmdd = (
datetime.date.today() + datetime.timedelta(days=targetday)
).strftime("%m%d")
await setkadai(line["name"] + " " + todaymmdd, str(targetdaymmdd))
with open("../data/onde.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
except FileNotFoundError:
channel = bot.get_channel(logchid)
await channel.send("onde.jsonが見つかりません")
return
async def setkadai(kadai, day): ##自動
try:
int(day)
except ValueError:
return
data = {"task_name": kadai, "task_date": day, "user": ""}
try:
# ファイルが存在する場合、データを読み込む
with open("../data/task.json", "r", encoding="utf-8") as file:
file_content = file.read()
if file_content:
olddata = json.loads(file_content)
else:
olddata = []
except FileNotFoundError:
channel = bot.get_channel(logchid)
await channel.send("ファイルを作成")
olddata = []
if not isinstance(
olddata, list
): # 既存のデータをリストに変換するか、空のリストから始める
olddata = []
name_exists = any(
item["task_name"] == kadai for item in olddata
) # 重複データのチェック(課題名をチェック)
if name_exists:
return
else:
olddata.append(data)
with open(
"../data/task.json", "w", encoding="utf-8"
) as file: # ファイルへの書き込み
json.dump(olddata, file, ensure_ascii=False, indent=4)
embed = discord.Embed(title=f"{kadai}", description=f"{day}")
embed.add_field(
name=f"by 自動書記",
value="取り組む場合は🫡を押してください\n取り組んだ場合は☑を押してください\n締切済やミス等でクローズする場合は❌を押してください",
inline=False,
)
channel = bot.get_channel(kadaichid)
new_message = await channel.send(embed=embed)
channel = bot.get_channel(mentionchid)
await channel.send(f"{kadai}({day})が自動で追加されました")
await new_message.add_reaction("🫡")
await new_message.add_reaction("☑")
await new_message.add_reaction("❌")
count("自動書記", "課題定義")
async def kdl():
delday = datetime.date.today() - datetime.timedelta(days=1) # 1日前
dalday = delday.strftime("%m%d") # str 4ケタで取得
with open("../data/task.json", "r", encoding="utf-8") as file:
data = json.load(file)
for item in data:
if str(item["task_date"]) == dalday:
delname = item["task_name"]
with open("../data/task.json", "r", encoding="utf-8") as file:
tasks = json.load(file)
split_strings = [
task for task in tasks if task["task_name"] != f"{delname}"
] # 科目によってデータを消す
newalldata = ",".join(
map(str, split_strings)
) # []がないstrにする ここまで正常6:19
newalldata = newalldata.replace("'", '"')
jjdata = f"[\n{newalldata}\n]"
dict_obj = dict_obj = ast.literal_eval(jjdata)
with open("../data/task.json", "w", encoding="utf-8") as new_json_file:
json.dump(dict_obj, new_json_file, ensure_ascii=False, indent=4)
channel = bot.get_channel(osirasechid)
await channel.send(f"1日前({delday})の課題{delname}をデータから削除")
async def kda():
status = 1
tomorrow = datetime.date.today()
tomorrow = tomorrow.strftime("%m%d") # 明日をstr 4ケタで取得
with open("../data/task.json", "r", encoding="utf-8") as file:
data = json.load(file)
channel = bot.get_channel(osirasechid)
for item in data:
if str(item["task_date"]) == tomorrow:
status = 0
jtaskname = item["task_name"]
juser = item["user"] # JSONから入手したuser
if juser == "":
ms = f"### 今日は__{jtaskname}__の締切日である__{tomorrow}__です。\n取り組んでいる人はいません。"
await channel.send(ms)
else:
str_list = juser.split(",")
userid_list = [item for item in str_list]
mentions = [f"<@{user_id}>" for user_id in userid_list]
mention_text = " ".join(mentions)
ms = f"### 今日は__{jtaskname}__の締切日である__{tomorrow}__です。\n{mention_text}"
await channel.send(ms)
if status == 1:
await channel.send(
"今日締切の課題はありません。\n"
) # ここより上のtomorrowはtoday
status = 1
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
tomorrow = tomorrow.strftime("%m%d") # 明日をstr 4ケタで取得
channel = bot.get_channel(osirasechid)
with open("../data/task.json", "r", encoding="utf-8") as file:
data = json.load(file)
for item in data:
if str(item["task_date"]) == tomorrow:
status = 0
jtaskname = item["task_name"]
juser = item["user"] # JSONから入手したuser
if juser == "":
ms = f"### 明日は__{jtaskname}__の締切日である__{tomorrow}__です。\n取り組んでいる人はいません。"
await channel.send(ms)
else:
str_list = juser.split(",")
userid_list = [item for item in str_list]
mentions = [f"<@{user_id}>" for user_id in userid_list]
mention_text = " ".join(mentions)
ms = f"### 明日は__{jtaskname}__の締切日である__{tomorrow}__です。はよやれ。\n{mention_text}"
await channel.send(ms)
if status == 1:
await channel.send("明日締切の課題はありません。")
random_number = random.randint(0, 9)
await channel.send("https://picsum.photos/640/5" + str(random_number)) ##GG
async def daily(): # デイリー報告
www = datetime.datetime.now().strftime("%m%d")
channel = bot.get_channel(osirasechid)
random_number = random.randint(0, 9)
await channel.send("https://picsum.photos/640/5" + str(random_number)) ##GG
await channel.send(f"# 本日({www})の未終了課題一覧の報告です")
guild = bot.get_guild(guildid)
allmemberid = [str(member.id) for member in guild.members if not member.bot]
with open("../data/task.json", "r", encoding="utf-8") as file:
data = json.load(file)
user_tasks = {user: [] for user in allmemberid}
result = ""
for task in data:
user_ids = task["user"].split(",")
for user_id in user_ids:
if user_id in user_tasks:
user_tasks[user_id].append(
{"task_name": task["task_name"], "task_date": task["task_date"]}
)
for user_id, tasks in user_tasks.items():
# タスクが存在しないユーザーをスキップ
if not tasks:
continue
result = f"<@{user_id}>\n"
for task in tasks:
result += f'__{task["task_name"]}({task["task_date"]})__\n'
channel = bot.get_channel(osirasechid)
await channel.send(result)
@bot.command()
async def myid(ctx):
await ctx.send(f"{ctx.author.id}")
@bot.command()
async def myt(ctx):
# コマンドを実行したユーザーのメンション
user = ctx.author
# ユーザーが個人DMでコマンドを実行した場合
if True:
i = 0
await ctx.send(f"{user}の未終了テスト一覧です")
allmemberid = [f"{user.id}"]
try:
with open("../data/test.json", "r", encoding="utf-8") as file:
data = json.load(file)
except FileNotFoundError:
await ctx.send("テストデータがありません")
return
user_tasks = {user: [] for user in allmemberid}
result = ""
for task in data:
user_ids = task["user"].split(",")
for user_id in user_ids:
if user_id in user_tasks:
user_tasks[user_id].append(
{"task_name": task["task_name"], "task_date": task["task_date"]}
)
i = i + 1
for user_id, tasks in user_tasks.items():
# タスクが存在しないユーザーをスキップ
if not tasks:
continue
name = f"<@{user_id}>\n"
for task in tasks:
result += f'__{task["task_name"]}({task["task_date"]})__\n'
lines = result.split("\n")
sorted_lines = sorted(
[line for line in lines if line.rfind("(") != -1 and line.rfind(")") != -1],
key=lambda line: int(line[line.rfind("(") + 1 : line.rfind(")")]),
reverse=False,
)
sorted_data = "\n".join(sorted_lines)
await ctx.send(name + sorted_data + f"\nテスト数: {i}")
count(f"{user.id}", "!mytコマンド使用")
# サーバーチャンネルの場合は無視
else:
pass # 何もしない
@bot.command()
async def my(ctx):
# コマンドを実行したユーザーのメンション
user = ctx.author
# ユーザーが個人DMでコマンドを実行した場合
if True:
i = 0
await ctx.send(f"{user}の未終了課題一覧です")
allmemberid = [f"{user.id}"]
with open("../data/task.json", "r", encoding="utf-8") as file:
data = json.load(file)
user_tasks = {user: [] for user in allmemberid}
result = ""
for task in data:
user_ids = task["user"].split(",")
for user_id in user_ids:
if user_id in user_tasks:
user_tasks[user_id].append(
{"task_name": task["task_name"], "task_date": task["task_date"]}
)
i = i + 1
for user_id, tasks in user_tasks.items():
# タスクが存在しないユーザーをスキップ
if not tasks:
continue
name = f"<@{user_id}>\n"
for task in tasks:
result += f'__{task["task_name"]}({task["task_date"]})__\n'
lines = result.split("\n")
sorted_lines = sorted(
[line for line in lines if line.rfind("(") != -1 and line.rfind(")") != -1],
key=lambda line: int(line[line.rfind("(") + 1 : line.rfind(")")]),
reverse=False,
)
sorted_data = "\n".join(sorted_lines)
await ctx.send(
name
+ sorted_data
+ f"\n課題数: {i}\n [ここ]({web_url})で見たり自動登録が設定できます"
)
count(f"{user.id}", "!myコマンド使用")
# サーバーチャンネルの場合は無視
else:
pass # 何もしない
@bot.command()
async def fs(ctx):
if ctx.channel.id != kanrishachid:
return
await daily()
@bot.command()
async def fc(ctx):
if ctx.channel.id != kanrishachid:
return
await kda()
@bot.command()
async def ff(ctx):
if ctx.channel.id != kanrishachid:
return
await kdl()
@bot.command()
async def o(ctx, kadai, day):
if ctx.channel.id != cmdchid:
return
try:
int(day)
except ValueError:
await ctx.reply("4桁半角数字の正しい期日を入力してください")
await ctx.message.add_reaction("💩")
return
random_number = random.randint(0, 318)
if random_number == 77:
channel = bot.get_channel(cmdchid)
await channel.send("おめでとうございます。319分の1の確率を引きました!")
data = {"task_name": kadai, "task_date": day, "user": ""}
try:
# ファイルが存在する場合、データを読み込む
with open("../data/task.json", "r", encoding="utf-8") as file:
file_content = file.read()
if file_content:
olddata = json.loads(file_content)
else:
olddata = []
except FileNotFoundError:
channel = bot.get_channel(logchid)
await channel.send("ファイルを作成")
olddata = []
if not isinstance(
olddata, list
): # 既存のデータをリストに変換するか、空のリストから始める
olddata = []
name_exists = any(
item["task_name"] == kadai for item in olddata
) # 重複データのチェック(課題名をチェック)
if name_exists:
await ctx.reply("課題名が重複しています")
await ctx.message.add_reaction("💩")
return
else:
olddata.append(data)
with open(
"../data/task.json", "w", encoding="utf-8"
) as file: # ファイルへの書き込み
json.dump(olddata, file, ensure_ascii=False, indent=4)
embed = discord.Embed(title=f"{kadai}", description=f"{day}")
embed.add_field(
name=f"by {ctx.author.name}",
value="取り組む場合は🫡を押してください\n取り組んだ場合は☑を押してください\n締切済やミス等でクローズする場合は❌を押してください",
inline=False,
)
channel = bot.get_channel(kadaichid)
new_message = await channel.send(embed=embed)
await ctx.message.add_reaction("⭕")
await new_message.add_reaction("🫡")
await new_message.add_reaction("☑")
await new_message.add_reaction("❌")
count(f"{ctx.author.id}", "課題定義")
@bot.command()
async def t(ctx, kadai, day):
if ctx.channel.id != cmdchid:
return
try:
int(day)
except ValueError:
await ctx.reply("4桁半角数字の正しい期日を入力してください")
await ctx.message.add_reaction("💩")
return
if not "テスト" in kadai:
await ctx.reply("最後にテストと追加してください")
await ctx.message.add_reaction("💩")
return
random_number = random.randint(0, 318)
if random_number == 77:
channel = bot.get_channel(cmdchid)
await channel.send("おめでとうございます。319分の1の確率を引きました!")
data = {"task_name": kadai, "task_date": day, "user": ""}
try:
# ファイルが存在する場合、データを読み込む
with open("../data/test.json", "r", encoding="utf-8") as file:
file_content = file.read()
if file_content:
olddata = json.loads(file_content)
else:
olddata = []
except FileNotFoundError:
# ファイルが存在しない場合は空のリストで開始
olddata = []
# ファイルを作成
with open("../data/test.json", "w", encoding="utf-8") as file:
json.dump([], file, ensure_ascii=False, indent=4)
if not isinstance(olddata, list):
olddata = []
name_exists = any(
item["task_name"] == kadai for item in olddata
) # 重複データのチェック(課題名をチェック)
if name_exists:
await ctx.reply("課題名が重複しています")
await ctx.message.add_reaction("💩")
return
else:
olddata.append(data)
with open(
"../data/test.json", "w", encoding="utf-8"
) as file: # ファイルへの書き込み
json.dump(olddata, file, ensure_ascii=False, indent=4)
embed = discord.Embed(title=f"{kadai}", description=f"{day}")
embed.add_field(
name=f"by {ctx.author.name}",
value="取り組む場合は🫡を押してください\n取り組んだ場合は☑を押してください\n締切済やミス等でクローズする場合は❌を押してください",
inline=False,
)
channel = bot.get_channel(testchid)
new_message = await channel.send(embed=embed)
await ctx.message.add_reaction("⭕")
await new_message.add_reaction("🫡")
await new_message.add_reaction("☑")
await new_message.add_reaction("❌")
count(f"{ctx.author.id}", "テスト定義")
@bot.event
async def on_raw_reaction_add(payload):
channel = bot.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
user = await bot.fetch_user(payload.user_id)
if user == bot.user:
return
# チャンネルに応じてJSONファイルを選択
if channel.id == testchid:
json_name = "../data/test.json"
else:
json_name = "../data/task.json"
embed = message.embeds[0]
title = embed.title
reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
if str(reaction.emoji) == "🫡": # 参加マーク
try:
with open(json_name, "r", encoding="utf-8") as file:
data = json.load(file)
except FileNotFoundError:
await channel.send(f"{json_name}ファイルが見つかりません")
return
for item in data:
if str(item["task_name"]) == str(title):
juser = item["user"] # JSONから入手したuser
if juser == "": # userがいないなら
newjuser = str(user.id)
else: # userがいたら,の後に追加
split_strings = juser.split(",") # strをリストに整形
if str(user.id) in split_strings:
return
split_strings.append(str(user.id)) # リストにuser.idを追加
newjuser = ",".join(map(str, split_strings)) # []がないstrにする
item["user"] = newjuser
with open(json_name, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
return
elif str(reaction.emoji) == "☑":
try:
with open(json_name, "r", encoding="utf-8") as file:
data = json.load(file)
except FileNotFoundError:
await channel.send(f"{json_name}ファイルが見つかりません")
return
for item in data:
if str(item["task_name"]) == str(title):
juser = item["user"] # JSONから入手したuser
if juser == "":
return
split_strings = juser.split(",") # strをリストに整形
if str(user.id) not in split_strings:
return
split_strings.remove(str(user.id)) # リストからuser.idを消す
newjuser = ",".join(map(str, split_strings)) # []がないstrにする
item["user"] = newjuser
with open(json_name, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
return
elif str(reaction.emoji) == "❌":
try:
with open(json_name, "r", encoding="utf-8") as file:
tasks = json.load(file)
except FileNotFoundError:
await channel.send(f"{json_name}ファイルが見つかりません")
return
mention_text = ""
for item in tasks:
if str(item["task_name"]) == str(title):
juser = item["user"] # JSONから入手したuserのstr
if juser != "":
str_list = juser.split(",")
userid_list = [item for item in str_list if item != ""]
mentions = [f"<@{user_id}>" for user_id in userid_list]
mention_text = " ".join(mentions)
break
split_strings = [
task for task in tasks if task["task_name"] != f"{title}"
] # 科目によってデータを消す
if not split_strings:
# 空のリストの場合
with open(json_name, "w", encoding="utf-8") as new_json_file:
json.dump([], new_json_file, ensure_ascii=False, indent=4)
else:
with open(json_name, "w", encoding="utf-8") as new_json_file:
json.dump(split_strings, new_json_file, ensure_ascii=False, indent=4)
day = embed.description
embed.clear_fields()
embed.description = f"{user.name}によって{title}({day})がクローズされました\nこのメッセージは5秒後に消えます"
await message.edit(embed=embed)
await reaction.message.clear_reactions()
await asyncio.sleep(5)
await message.delete() # 2.0追加りあくしょん消す
channel = bot.get_channel(osirasechid)
if mention_text == "<@>":
mention_text = ""
await channel.send(
f"{title} {day}が{user.name}によってクローズされました。\n{mention_text}"
)
@bot.command()
async def sss(ctx):
if ctx.channel.id != kanrishachid:
return
file = open(f"tukaikata.txt", "r", encoding="utf-8")
welcome = file.read()
file.close()
embed = discord.Embed(
title="ようこそ",
description=welcome + f"\n# 自動登録は[ここ]({web_url})でできます",
)
channel = bot.get_channel(tukaikatachid)
await channel.send(embed=embed)
@bot.command()
async def app(ctx):
# 個人DMでのみ動作
if ctx.guild is not None:
return
characters = string.ascii_lowercase + string.digits
random_code = ''.join(random.choice(characters) for _ in range(8))
text=f"です。アプリ内で連携コードを入力してください\n"
try:
with open("../data/auth.json", "r", encoding="utf-8") as file:
json_data = json.load(file)
except FileNotFoundError:
# ファイルが存在しない場合は空のリストで開始
json_data = []
find=False
for item in json_data:
if item["user_id"] == str(ctx.author.id):
find=True
item["code"]=random_code # コードを更新
item["user_name"]=str(ctx.author.name)
text+=f"連携コードを更新したので以前のコードは無効化されました\n"
break
if not find:
json_data.append({
"user_name": str(ctx.author.name),
"user_id": str(ctx.author.id),
"code": random_code,
"daily_houkoku":True
})
with open("../data/auth.json", "w", encoding="utf-8") as file:
json.dump(json_data, file, ensure_ascii=False, indent=4)
await ctx.send("連携コードは")
await ctx.send(random_code)
await ctx.send(text)
def run_bot():
bot.run(TOKEN)
if __name__ == "__main__":
run_bot()