-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
2269 lines (1824 loc) · 111 KB
/
main.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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Discord bot import
import discord
from discord import app_commands
from discord import ui
from discord.ext import tasks
import os
from dotenv import load_dotenv
import glob
import ndjson
import datetime
import dateutil.parser
from mk8dx import Track
import asyncio
import math
import shutil
import openpyxl
# Bot start
# envの読込
load_dotenv()
# 変数宣言
# データ保存をシーズンごとに分ける用
now_season = 9
# add,deleteの操作がない時に処理をとめる時間(秒)
stop_time = 600
# 文字列にした時間を計算できる形式に変換する時に使用
time_format = '%Y-%m-%d %H:%M:%S.%f'
# 時間をJSTの時間に変換するときに使用
JST = datetime.timezone(datetime.timedelta(hours=+9), 'JST')
# 時間をUTCの時間に変換するときに使用
UTC = datetime.timezone(datetime.timedelta(hours=0), 'UTC')
# 頭に記号がある他のBotのコマンドに反応しないように登録する場所
prefixes = ['!', '#', '$', '%', '&', '(', ')', '=', '-', '~', '^', '|', '`', ':', '*', '+', ';', '<', ',', '>', '.', '?', '_']
intents = discord.Intents.all()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
# Bot開始時にさせる処理
@client.event
async def on_ready():
print("接続しました!")
await client.change_presence(activity=discord.Game(name="rank collect"))
await tree.sync()#スラッシュコマンドを同期
print("グローバルコマンド同期完了!")
# guild_jsonフォルダがあるかの確認
files = glob.glob('./*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == "guild_json"):
print("guild_jsonファイルを確認しました!")
judge = 1
break
if judge != 1:
os.mkdir('guild_json')
print("guild_jsonファイルがなかったため作成しました!")
# delete_jsonフォルダがあるかの確認
files = glob.glob('./*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == "delete_json"):
print("delete_jsonファイルを確認しました!")
judge = 1
break
if judge != 1:
os.mkdir('delete_json')
print("delete_jsonファイルがなかったため作成しました!")
# add_jsonフォルダがあるかの確認
files = glob.glob('./*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == "add_json"):
print("add_jsonファイルを確認しました!")
judge = 1
break
if judge != 1:
os.mkdir('add_json')
print("add_jsonファイルがなかったため作成しました!")
# user_jsonフォルダがあるかの確認
files = glob.glob('./*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == "user_json"):
print("user_jsonファイルを確認しました!")
judge = 1
break
if judge != 1:
os.mkdir('user_json')
print("user_jsonファイルがなかったため作成しました!")
# language_jsonフォルダがあるかの確認
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == "language_json"):
print("language_jsonファイルを確認しました!")
judge = 1
break
if judge != 1:
os.mkdir('language_json')
print("language_jsonファイルがなかったため作成しました!")
# 定期的に動かすループ処理の開始
time_check.start()
print("時間の確認を開始します!")
# サーバーに招待された場合に特定の処理をする
@client.event
async def on_guild_join(guild):
file = str(guild.id) + ".ndjson"
content = {
"language_mode" : "ja"
}
with open('./language_json/' + file, 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
print("招待されたため" + str(guild.id) + "のlanguage jsonを作成しました。")
# サーバーからキック、BANされた場合に特定の処理をする
@client.event
async def on_guild_remove(guild):
file = str(guild.id) + ".ndjson"
os.remove("./language_json/" + file)
print("キックまたはBANされたため、" + str(guild.id) + "のlanguage jsonを削除しました。")
files = glob.glob('./guild_json/*.ndjson')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if os.path.split(files[i])[1] == str(guild.id) + ".ndjson":
judge = 1
break
if judge == 1:
os.remove("./guild_json/" + file)
print("キックまたはBANされたため、" + str(guild.id) + "のguild jsonを削除しました。")
files = glob.glob('./add_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if os.path.split(files[i])[1] == str(guild.id):
judge = 1
break
if judge == 1:
os.remove("./add_json/" + file)
print("キックまたはBANされたため、" + str(guild.id) + "のadd jsonを削除しました。")
files = glob.glob("./delete_json/" + str(guild.id) + "/")
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if os.path.split(files[i])[1] == str(guild.id):
judge = 1
break
if judge == 1:
os.remove("./delete_json/" + str(guild.id) + "/" )
print("キックまたはBANされたため、" + str(guild.id) + "のdelete jsonを削除しました。")
#メッセージを取得した時に実行される
@client.event
async def on_message(message):
# Botのメッセージは除外
if message.author.bot:
return
# 頭に記号がある場合は除外
for i in range(0, len(prefixes)):
if message.content.startswith(prefixes[i]) == True:
return
try:
# 言語の確認
file = str(message.guild.id) + ".ndjson"
with open('./language_json/' + file) as f:
read_data = ndjson.load(f)
language = read_data[0]["language_mode"]
# 現在のモードは何かを確認
file = str(message.guild.id) + ".ndjson"
#print(message.guild.id)
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + file) as f:
read_data = ndjson.load(f)
data_location = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location = i
#print(read_data)
break
mode = read_data[data_location]["mode"]
# コマンドが送信されたチャンネルでしか反応しないようにする
if message.channel.id == read_data[data_location]["channel"]:
# モードがaddの場合
if mode == "add":
# 入力されたメッセージを取得
#print(message.channel.id)
msg = message.content.split()
course = str(msg[0])
rank = int(msg[1])
# 入力されたコース名がデータにあるかの判定
try:
name = Track.from_nick(course)
print(name.abbr)
except Exception as e:
if language == "ja":
embed=discord.Embed(title="そのコースデータは存在しません!", color=0xff0000)
embed.add_field(name="入力されたコース名を確認してください。", value="", inline=False)
elif language == "en":
embed=discord.Embed(title="Not course data!", color=0xff0000)
embed.add_field(name="Check typing course name.", value="", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
else:
#入力された順位が範囲内であるかの判定
if rank < 1 or rank > 12:
if language == "ja":
embed=discord.Embed(title="入力された順位が有効範囲外です!", color=0xff0000)
embed.add_field(name="有効な順位の範囲は1~12位です。", value="", inline=False)
elif language == "en":
embed=discord.Embed(title="The entered rank is out of the valid range!", color=0xff0000)
embed.add_field(name="The valid ranking range is 1~12.", value="", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
# 既に登録があるかの確認
# コースを保存するユーザーファイル
files = glob.glob('./user_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(message.author.id)):
judge = 1
break
# なければ作成
if judge == 0:
os.mkdir("./user_json/" + str(message.author.id))
# シーズンを分けるファイル
files = glob.glob('./user_json/' + str(message.author.id) + '/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(now_season)):
judge = 1
break
# なければ作成
if judge == 0:
os.mkdir('./user_json/' + str(message.author.id) + '/' + str(now_season))
# ndjsonファイルに書き込むものの定義
content = {
"time" : str(datetime.datetime.now()),
"rank": rank
}
# ndjsonファイルに書き込む
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson", 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
# 平均順位の算出
# ファイルを読み込む
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson") as f:
read_data = ndjson.load(f)
sum = 0
avg = 0
for i in range(0, len(read_data)):
sum += read_data[i]["rank"]
avg = sum / len(read_data)
# メッセージ表示
if language == "ja":
embed=discord.Embed(title="\n" +"Season" + str(now_season) +" 順位記録\n\n" + name.full_name_ja,color=0x00ff40)
elif language == "en":
embed=discord.Embed(title="Ranking register!\n" +"Season" + str(now_season) +" Rank Record\n\n" + name.full_name,color=0x00ff40)
embed.set_author(name=message.author.name, icon_url=message.author.avatar)
if language == "ja":
embed.add_field(name="現在の平均順位", value=str(round(avg, 1)) + "位", inline=True)
if len(read_data) >= 2: # 過去の記録が存在するときはこっち
embed.add_field(name="前回の順位", value=str(read_data[len(read_data)-2]["rank"]) + "位", inline=True)
else: # 記録がないときはこっち
embed.add_field(name="前回の順位", value="なし", inline=True)
elif language == "en":
embed.add_field(name="Now average Rank", value=str(round(avg, 1)), inline=True)
if len(read_data) >= 2: # 過去の記録が存在するときはこっち
embed.add_field(name="Previous rank", value=str(read_data[len(read_data)-2]["rank"]), inline=True)
else: # 記録がないときはこっち
embed.add_field(name="Previous rank", value="No data.", inline=True)
embed.set_image(url="https://ay2416.github.io/Rank-Collector/stage_picture/" + str(name.id) + ".jpg")
embed.set_footer(text="Picture : ©Mario Kart Blog")
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
# add_jsonのcountを書き換え
with open('./add_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson') as f:
read_data = ndjson.load(f)
now_count = read_data[0]["count"]
#print(now_count)
# もし12回目の入力であれば終了する
if now_count == 12:
# 処理が終わったのでadd_jsonを削除する
# add_jsonのギルドフォルダ内のフォルダの情報を取得
files = glob.glob('./add_json/' + str(message.guild.id) + '/*.ndjson')
os.remove('./add_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson')
# ファイルの個数が1だった場合はギルドフォルダも削除する
if len(files) == 1:
os.rmdir('./add_json/' + str(message.guild.id))
# guild_jsonの読込
with open('./guild_json/' + file) as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
read_data[data_location1]["mode"] = "null"
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
# メッセージ表示
if language == "ja":
embed=discord.Embed(title="12レース分入力されたためaddモードを終了します!", description="まだ追加したい場合は、\n/addコマンドでstartしてください。", color=0x00ff40)
elif language == "en":
embed=discord.Embed(title="Exit [add mode] because 12 races have been entered!", description="If you still want to add more, \nstart with the /add command.", color=0x00ff40)
await message.channel.send(embed=embed)
return
content = {
"count" : now_count + 1,
}
os.remove('./add_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson')
with open('./add_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
# モードがdeleteの場合
elif mode == "delete":
# 現在のユーザーがどこの手順を行っているかを保存しているものを呼び出し
with open('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + ".ndjson") as f:
read_data = ndjson.load(f)
count = read_data[0]["count"]
#print(count)
# 手順3:続けるか続けないのか
if count == 2:
name = Track.from_nick(read_data[0]["course"])
msg = message.content.split()
select = str(msg[0])
# 続ける場合
if select == "Yes" or select == "yes":
# 既に登録があるかの確認
# ギルドフォルダー
files = glob.glob('./delete_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(message.guild.id)):
judge = 1
break
# なければ作成
if judge == 0:
os.mkdir("./delete_json/" + str(message.guild.id))
# delete_jsonの作成
content = {
"count" : 1,
"course" : name.abbr
}
os.remove('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson')
with open('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
# コースデータの読込
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson") as f:
read_data = ndjson.load(f)
# 表示させる
if language == "ja":
embed=discord.Embed(title="Season" + str(now_season) + " 過去記録\n\n" + name.full_name_ja, description="**削除したい記録を番号で選択してください。**\n※直近分表示(最大25個まで)", color=0x00008b)
elif language == "en":
embed=discord.Embed(title="Season" + str(now_season) + " Old Record\n\n" + name.full_name, description="**Select the record you wish to delete by number.**\n*Display of most recent minutes (up to 25)", color=0x00008b)
embed.set_author(name=message.author.name, icon_url=message.author.avatar)
num = 1
for i in range(len(read_data)-1,-1,-1):
if language == "ja":
date = dateutil.parser.parse(read_data[i]["time"]).astimezone(JST)
embed.add_field(name="", value=str(num) + ". " + date.strftime("%Y/%m/%d %H:%M:%S") + " JST : " + str(read_data[i]["rank"]) + "位", inline=False)
elif language == "en":
date = dateutil.parser.parse(read_data[i]["time"]).astimezone(UTC)
embed.add_field(name="", value=str(num) + ". " + date.strftime("%Y/%m/%d %H:%M:%S") + " UTC : " + str(read_data[i]["rank"]), inline=False)
num = num + 1
embed.set_image(url="https://ay2416.github.io/Rank-Collector/stage_picture/" + str(name.id) + ".jpg")
embed.set_footer(text="Picture : ©Mario Kart Blog")
await message.channel.send(embed=embed)
# 続けない場合
elif select == "No" or select == "no":
# 既に登録があるかの確認
# ギルドフォルダー
files = glob.glob('./delete_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(message.guild.id)):
judge = 1
break
# なければ作成
if judge == 0:
os.mkdir("./delete_json/" + str(message.guild.id))
#delete_jsonの作成
content = {
"count" : 0
}
os.remove('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson')
with open('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
'''
#コースデータの読込 多分この処理いらないはず 消しても良し
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson") as f:
read_data = ndjson.load(f)
'''
if language == "ja":
embed=discord.Embed(title="終了しました!", description="別のコースの記録の削除を行いたい場合はコース名を入力してください。\n削除を終了したい場合は、/deleteコマンドでstopを実行してください。", color=0x00ff40)
elif language == "en":
embed=discord.Embed(title="Completed!", description="If you want to delete a record from another course, enter the name of the course.\nIf you wish to terminate the deletion, execute stop with the /delete command.", color=0x00ff40)
await message.channel.send(embed=embed)
# 該当する文字が入力されなかった場合
else:
if language == "ja":
embed=discord.Embed(title="入力された文字が間違っています!", color=0xff0000)
embed.add_field(name="Yes/No(yes/no)で解答してください。", value="", inline=False)
elif language == "en":
embed=discord.Embed(title="The entered characters are incorrect!", color=0xff0000)
embed.add_field(name="Please answer with Yes/No (yes/no).", value="", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
# 手順2:削除する〇番目の指定
elif count == 1:
name = Track.from_nick(read_data[0]["course"])
msg = message.content.split()
#print(msg[0])
num = int(msg[0])
# コースデータの読込
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson") as f:
read_data = ndjson.load(f)
# 入力された数が範囲内であるかの判定
if num < 1 or num > 25:
if language == "ja":
embed=discord.Embed(title="入力された数が有効範囲外です!", color=0xff0000)
embed.add_field(name="有効な数の範囲は1~25です。", value=" ", inline=False)
elif language == "en":
embed=discord.Embed(title="The entered rank is out of the valid range!", color=0xff0000)
embed.add_field(name="The valid ranking range is 1~12.", value="", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
# コースデータの更新
if len(read_data) == 1:
os.remove('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson")
if language == "ja":
embed=discord.Embed(title="削除が完了しました!", description="このコースのデータがすべて削除されたため、このコースの削除の処理を終了します。\n別のコースの削除を行いたい場合はコース名を入力してください。\n削除を終了したい場合は/deleteコマンドでstopをしてください。",color=0x00ff40)
elif language == "en":
embed=discord.Embed(title="Deletion completed!", description="The process of deleting this course is terminated because all data for this course has been deleted.\nIf you want to delete another course, enter the name of the course.\nIf you want to finish deleting the course, use the /delete command to stop.",color=0x00ff40)
await message.channel.send(embed=embed)
# 既に登録があるかの確認
# ギルドフォルダー
files = glob.glob('./delete_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(message.guild.id)):
judge = 1
break
# なければ作成
if judge == 0:
os.mkdir("./delete_json/" + str(message.guild.id))
# delete_jsonの作成
content = {
"count" : 0
}
os.remove('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson')
with open('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
else:
data_location = 0
for i in range(len(read_data)-1,-1,-1):
if i == len(read_data)- num:
data_location = i
#print(read_data)
break
#if data_write == 1:
os.remove('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson")
for i in range(0,len(read_data)):
if i != data_location:
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson", 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
if language == "ja":
embed=discord.Embed(title="削除が完了しました!", description="まだ同じコースの記録の削除を続けますか?(Yes/No)",color=0x00ff40)
elif language == "en":
embed=discord.Embed(title="Deletion completed!", description="Do you still want to continue deleting records for the same course? (Yes/No)",color=0x00ff40)
await message.channel.send(embed=embed)
# 既に登録があるかの確認
# ギルドフォルダー
files = glob.glob('./delete_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(message.guild.id)):
judge = 1
break
# なければ作成
if judge == 0:
os.mkdir("./delete_json/" + str(message.guild.id))
# delete_jsonの作成
content = {
"count" : 2,
"course" : name.abbr
}
os.remove('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson')
with open('./delete_json/' + str(message.guild.id) + '/' + str(message.author.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(content)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
# 手順1:削除するコースの指定
else:
msg = message.content.split()
#print(msg[0])
course = str(msg[0])
# 入力されたコース名がデータにあるかの判定
try:
name = Track.from_nick(course)
print(name.abbr)
except Exception as e:
if language == "ja":
embed=discord.Embed(title="そのコースデータは存在しません!", color=0xff0000)
embed.add_field(name="入力されたコース名を確認してください。", value=" ", inline=False)
elif language == "en":
embed=discord.Embed(title="That course data does not exist!", color=0xff0000)
embed.add_field(name="Please confirm the course name entered.", value=" ", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
# コースデータが存在しているかの確認
# コースを保存するユーザーデータフォルダがあるか
files = glob.glob('./user_json/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(message.author.id)):
judge = 1
break
# なければエラー
if judge == 0:
if language == "ja":
embed=discord.Embed(title="あなたは順位の記録を登録していません!", color=0xff0000)
embed.add_field(name="順位の記録を登録してください。", value=" ", inline=False)
elif language == "en":
embed=discord.Embed(title="You have not registered your rank record!", color=0xff0000)
embed.add_field(name="Register your ranking record.", value=" ", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
# seasonフォルダが作成されているか
files = glob.glob('./user_json/' + str(message.author.id) + '/*')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == str(now_season)):
judge = 1
break
# なければエラー
if judge == 0:
if language == "ja":
embed=discord.Embed(title="あなたはシーズンが変わってから順位の記録をしていません!", color=0xff0000)
embed.add_field(name="順位の記録を登録してください。", value=" ", inline=False)
elif language == "en":
embed=discord.Embed(title="You haven't recorded your standings since the season changed!", color=0xff0000)
embed.add_field(name="Register your ranking record.", value=" ", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
# コース名.ndjsonがあるか
files = glob.glob('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/*.ndjson')
judge = 0
for i in range(0, len(files)):
#print(os.path.split(files[i])[1])
if(os.path.split(files[i])[1] == name.abbr + '.ndjson'):
judge = 1
break
# なければエラー
if judge == 0:
if language == "ja":
embed=discord.Embed(title="そのコースの順位の記録は存在しません!", color=0xff0000)
embed.add_field(name="そのコースの順位の記録を登録してください。", value=" ", inline=False)
elif language == "en":
embed=discord.Embed(title="No record of the rankings for that course exists!", color=0xff0000)
embed.add_field(name="Register a record of your rank for that course.", value=" ", inline=False)
await message.channel.send(embed=embed)
# guild_jsonのtimeを更新
# 同じチャンネルのデータがある部分を探す
# guild_jsonの読込
with open('./guild_json/' + str(message.guild.id) + '.ndjson') as f:
read_data = ndjson.load(f)
data_location1 = 0
for i in range(0, len(read_data)):
if read_data[i]["channel"] == message.channel.id:
data_location1 = i
#print(read_data)
break
# ndjsonにtimeを書き込み
read_data[data_location1]["time"] = str(datetime.datetime.now())
os.remove('./guild_json/' + str(message.guild.id) + '.ndjson')
for i in range(0, len(read_data)):
with open('./guild_json/' + str(message.guild.id) + '.ndjson', 'a') as f:
writer = ndjson.writer(f)
writer.writerow(read_data[i])
return
#コースデータの読込
with open('./user_json/' + str(message.author.id) + '/' + str(now_season) + '/' + name.abbr + ".ndjson") as f:
read_data = ndjson.load(f)
# 表示させる
if language == "ja":
embed=discord.Embed(title="Season" + str(now_season) + " 過去記録\n\n" + name.full_name_ja, description="**削除したい記録を番号で選択してください。**\n※直近分表示(最大25個まで)", color=0x00008b)
elif language == "en":
embed=discord.Embed(title="Season" + str(now_season) + " Old Record\n\n" + name.full_name, description="**Select the record you wish to delete by number.**\n*Display of most recent minutes (up to 25)", color=0x00008b)
embed.set_author(name=message.author.name, icon_url=message.author.avatar)
num = 1
for i in range(len(read_data)-1,-1,-1):
if language == "ja":
date = dateutil.parser.parse(read_data[i]["time"]).astimezone(JST)
embed.add_field(name="", value=str(num) + ". " + date.strftime("%Y/%m/%d %H:%M:%S") + " JST : " + str(read_data[i]["rank"]) + "位", inline=False)
elif language == "en":
date = dateutil.parser.parse(read_data[i]["time"]).astimezone(UTC)
embed.add_field(name="", value=str(num) + ". " + date.strftime("%Y/%m/%d %H:%M:%S") + " UTC : " + str(read_data[i]["rank"]), inline=False)
num = num + 1
embed.set_image(url="https://ay2416.github.io/Rank-Collector/stage_picture/" + str(name.id) + ".jpg")
embed.set_footer(text="Picture : ©Mario Kart Blog")
await message.channel.send(embed=embed)
'''
50以上の数字が50のままにあるバグあり
embed=discord.Embed(title="Season" + str(now_season) + " 過去記録\n\n" + name.full_name_ja, description="**削除したい記録を番号で選択してください。**", color=0x00008b)
embed.set_author(name=message.author.name, icon_url=message.author.avatar)
cut = 25
for i in range(0, len(read_data)):
date = dateutil.parser.parse(read_data[i]["time"]).astimezone(JST)
embed.add_field(name=" ", value=str(i) + ". " + date.strftime("%Y/%m/%d %H:%M:%S") + " JST : " + str(read_data[i]["rank"]) + "位", inline=False)
if i == len(read_data) - 1:
#表示させる
embed.set_image(url="https://ay2416.github.io/Rank-Collector/stage_picture/" + str(name.id) + ".jpg")
embed.set_footer(text="Picture : ©Mario Kart Blog")
await message.channel.send(embed=embed)
return
if i + 1 == cut:
cut = cut + 25
#表示させる
await message.channel.send(embed=embed)
await asyncio.sleep(2)
embed=discord.Embed(title="", color=0x00008b)
'''
# 既に登録があるかの確認