-
Notifications
You must be signed in to change notification settings - Fork 0
/
discordbot.py
executable file
·173 lines (150 loc) · 6.83 KB
/
discordbot.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
import discord
from discord.ext import commands, tasks
import generate_wargame_count_data
import generate_user_index_data
import discordbot_data
import json
from datetime import datetime
from zoneinfo import ZoneInfo
import asyncio
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
korea_timezone = ZoneInfo('Asia/Seoul')
begin_datetime = datetime(discordbot_data.begin_datetime[0],
discordbot_data.begin_datetime[1],
discordbot_data.begin_datetime[2],
discordbot_data.begin_datetime[3],
discordbot_data.begin_datetime[4],
tzinfo=korea_timezone)
end_datetime = datetime(discordbot_data.end_datetime[0],
discordbot_data.end_datetime[1],
discordbot_data.end_datetime[2],
discordbot_data.end_datetime[3],
discordbot_data.end_datetime[4],
tzinfo=korea_timezone)
def get_score_from_wargame_count_data(wargame_count:list[int]) -> int:
score = 0
for i in range(len(wargame_count)):
score += 2**(i+1)*wargame_count[i]
return score
async def time_scheduler():
await bot.wait_until_ready()
current_time = datetime.now(korea_timezone)
if current_time < begin_datetime:
sleep_seconds = (begin_datetime - current_time).total_seconds()
print("waiting for competition to begin... remaining time:", sleep_seconds)
await asyncio.sleep(sleep_seconds)
generate_user_index_data.run()
generate_wargame_count_data.run()
initialize_competition()
channel = bot.get_channel(discordbot_data.TARGET_CHANNEL_ID)
await channel.send("**Competition has begun!**")
await get_leaderboard()
current_time = datetime.now(korea_timezone)
if current_time < end_datetime:
sleep_seconds = (end_datetime - current_time).total_seconds()
print("waiting for competition to end... remaining time:", sleep_seconds)
await asyncio.sleep(sleep_seconds)
channel = bot.get_channel(discordbot_data.TARGET_CHANNEL_ID)
await channel.send("**Competition has finished!**")
data = await get_leaderboard()
file = open("final_wargame_count_data.json", "w")
file.write(json.dumps(data, indent=2, ensure_ascii=False))
file.close()
async def setup():
asyncio.create_task(time_scheduler())
@bot.event
async def on_ready():
channel = bot.get_channel(discordbot_data.TARGET_CHANNEL_ID)
await channel.send(f"I'm ready! \n usage: type '!leaderboard' on this channel\ncompetition starts at: {begin_datetime}")
@bot.event
async def on_message(message):
if message.channel.id == discordbot_data.TARGET_CHANNEL_ID:
await bot.process_commands(message)
def get_leaderboard_str_from_sorted_score_dict(sorted_score_dict:dict, wargame_count_dict:dict) -> str:
message = ""
message += "```\n"
message += "Leaderboard\n"
datetime_str_format = '%Y-%m-%d %H:%M:%S'
current_time = datetime.now(korea_timezone)
if current_time <begin_datetime:
message += "competition starts at: " + begin_datetime.strftime(datetime_str_format) + "\n"
message += "competition ends at: " + end_datetime.strftime(datetime_str_format) + "\n"
message += "current time: "+current_time.strftime(datetime_str_format)+ "\n"
elif current_time < end_datetime:
message += "competition started at: " + begin_datetime.strftime(datetime_str_format) + "\n"
message += "competition ends at: " + end_datetime.strftime(datetime_str_format) + "\n"
message += "current time: "+current_time.strftime(datetime_str_format)+ "\n"
else:
message += "competition started at: " + begin_datetime.strftime(datetime_str_format) + "\n"
message += "competition ended at: " + end_datetime.strftime(datetime_str_format) + "\n"
message += "The competition has finished!\n"
message += "\n"
rank = 0
last_score = -1
tie_count = 1
for username, score in sorted_score_dict.items():
if score != last_score:
rank += tie_count
tie_count = 1
last_score = score
else:
tie_count += 1
message += f"{rank}. {username}: {score} \n"
wargame_count_list = wargame_count_dict[user_index_dict[username]]
initial_wargame_count_list = initial_wargame_count_dict[username]
solved_any = False
for i in range(10):
if(wargame_count_list[i] - initial_wargame_count_list[i]!=0):
if not solved_any:
solved_any = True
message += " ∟ "
else:
message +=", "
message += f"Lv{i+1}: {wargame_count_list[i] - initial_wargame_count_list[i]}"
if solved_any:
message += "\n"
message+="```"
return message
@bot.command("leaderboard")
async def leaderboard(ctx):
await get_leaderboard()
async def get_leaderboard():
wargame_count = generate_wargame_count_data.get_score_list_by_user_index_list(user_index_list)
score_list = list(map(get_score_from_wargame_count_data, wargame_count))
score_dict = {}
for i in range(len(username_list)):
score_dict[username_list[i]] = score_list[i] - initial_score_dict[username_list[i]]
sorted_score_dict = dict(sorted(score_dict.items(), key=lambda item: item[1], reverse=True))
print(sorted_score_dict)
wargame_count_dict = {}
for i in range(len(user_index_list)):
wargame_count_dict[user_index_list[i]] = wargame_count[i]
message = get_leaderboard_str_from_sorted_score_dict(sorted_score_dict, wargame_count_dict)
channel = bot.get_channel(discordbot_data.TARGET_CHANNEL_ID)
await channel.send(message)
return wargame_count
def initialize_competition():
global username_list
global user_index_list
global user_index_dict
global initial_score_dict
global initial_wargame_count_dict
# get user index list
user_index_json_file = open("user_index.json", 'r')
user_index_dict = json.load(user_index_json_file)
user_index_json_file.close()
username_list = list(user_index_dict.keys())
user_index_list = list(user_index_dict.values())
# get initial score
initial_wargame_count_file = open("initial_wargame_count_data.json", 'r')
initial_wargame_count_dict = json.load(initial_wargame_count_file)
initial_wargame_count_file.close()
initial_score_dict = {}
for username, score_list in initial_wargame_count_dict.items():
initial_score_dict[username] = get_score_from_wargame_count_data(score_list)
if __name__ == "__main__":
initialize_competition()
bot.setup_hook = setup
bot.run(discordbot_data.DISCORD_TOKEN)