-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
276 lines (217 loc) · 8.48 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
import os
import keep_alive
import re
import traceback
import json
import discord
from discord.ext import commands
from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions
from dotenv import load_dotenv
import requests
channels = {}
width = 30
def readJSON():
global channels
with open('channels.json', 'r') as infile:
channels = json.load(infile)
def writeJSON():
with open('channels.json', 'w') as outfile:
json.dump(channels, outfile)
def get_trailing_number(s):
m = re.search(r'\d+$', s)
return int(m.group()) if m else None
def getLeaderboard(ctx):
code = channels[str(ctx.guild.id)][0]['code']
year = channels[str(ctx.guild.id)][0]['year']
session = channels[str(ctx.guild.id)][0]['session']
try:
link = 'https://adventofcode.com/' + str(
year) + '/leaderboard/private/view/' + str(code) + '.json'
x = requests.get(link,
cookies={
"_ga": "GA1.2.1941296263.1606786137",
"_gid": "GA1.2.641639940.1606786137",
"session": session,
"_gat": "1"
})
data = x.json()
members = data['members']
mydata = sorted(members,
key=lambda x:
(members[x]['local_score'], members[x]['stars']),
reverse=True)
names = []
for i in mydata:
if not str(data['members'][i]['name']) == "None":
name = str(data['members'][i]['name']).replace("_", "˾")
stars = str(data['members'][i]['stars'])
score = str(data['members'][i]['local_score'])
dots1 = " " * (channels[str(ctx.guild.id)][0]['width'] -
len(str(data['members'][i]['name'])) - 5)
else:
name = str("Anonymous(#" +
str(data['members'][i]['id']) + ")").replace(
"_", "˾")
stars = str(data['members'][i]['stars'])
score = str(data['members'][i]['local_score'])
dots1 = " " * (channels[str(ctx.guild.id)][0]['width'] - len(
str("Anonymous(#" + str(data['members'][i]['id']) + ")")) -
5)
names.append(score + ". " + (" " * (4 - len(score))) + name +
" " + dots1 + " | " + stars)
return "\n".join(names[:20])
except Exception as e:
print(traceback.format_exc())
return 'Leaderboard unavailable'
load_dotenv()
TOKEN = os.getenv('TOKEN')
readJSON()
default_prefix = "="
def getPrefix(bot, message):
readJSON()
return channels[str(message.guild.id)][0]['prefix']
client = commands.Bot(command_prefix=getPrefix)
client.remove_command("help")
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
game = discord.Game("with the API")
await client.change_presence(status=discord.Status.online, activity=game)
@client.event
async def on_guild_join(guild):
channels[guild.id] = []
channels[guild.id].append({
'prefix': default_prefix,
'channel': '',
'code': '',
'year': 2020,
'width': width
})
writeJSON()
@client.command()
async def ping(ctx):
channel = client.get_channel(int(channels[str(
ctx.guild.id)][0]['channel']))
if ctx.message.channel == channel:
await ctx.channel.send("Pong")
@client.command()
@has_permissions(manage_roles=True, ban_members=True)
async def prefix(ctx, message):
global client
channels[str(ctx.guild.id)][0]['prefix'] = message
writeJSON()
client.command_prefix = getPrefix
await ctx.channel.send('Prefix changed to: ``' + message + "``")
@client.command()
@has_permissions(manage_roles=True, ban_members=True)
async def setChannel(ctx, message):
channels[str(ctx.guild.id)][0]['channel'] = message
writeJSON()
await ctx.channel.send('Channel set to: ``' + message + "``")
@client.command()
@has_permissions(manage_roles=True, ban_members=True)
async def setCode(ctx, message):
channels[str(ctx.guild.id)][0]['code'] = message
writeJSON()
await ctx.channel.send('Code set to: ``' + message + "``")
@client.command()
@has_permissions(manage_roles=True, ban_members=True)
async def setWidth(ctx, message):
channels[str(ctx.guild.id)][0]['width'] = int(message)
writeJSON()
await ctx.channel.send('Width set to: ``' + str(message) + "``")
@client.command()
async def setYear(ctx, message):
channels[str(ctx.guild.id)][0]['year'] = int(message)
writeJSON()
await ctx.channel.send('Year set to: ``' + str(message) + "``")
@client.command()
@has_permissions(manage_roles=True, ban_members=True)
async def setSession(ctx, message):
channels[str(ctx.guild.id)][0]['session'] = str(message)
writeJSON()
await ctx.channel.send('Session set to: ``' + str(message) + "``")
@client.command()
async def showSettings(ctx):
name = "Advent of Code"
description = "Settings"
embed = discord.Embed(title=name,
description=description,
color=discord.Color.blue())
message = ' Setting' + (
" " * (10 - len('Setting'))) + '| Value\n===================\n'
for i in channels[str(ctx.message.guild.id)]:
count = 0
for x in i:
message += str(count) + ". " + str(x) + (
" " * (10 - len(str(x)))) + "| " + str(i[x]) + "\n"
count += 1
embed.add_field(name='Settings', value="```md\n" + message + "```")
await ctx.send(embed=embed)
helpWidth = 12
com = [
'help', 'prefix {prefix}', 'setChannel {id}', 'setCode {code}',
'setWidth {width}', 'setYear {year}', 'setSession {id}', 'ldr'
]
desc = [
'Generates this help box', 'Changes the prefix of the bot',
'Sets the channel to display the board',
'Sets the code of the board (before -)', 'Sets the width of the board',
'Sets the year of the leaderboard', 'Sets the session id',
'Displays the leaderboard'
]
commandsList = []
for i in range(len(com)):
commandsList.append(com[i] + (' ' *
(len('Command' +
(" " * helpWidth)) - len(com[i]))) +
desc[i])
@client.command()
async def help(ctx):
name = "Advent of Code"
description = "List of commands"
embed = discord.Embed(title=name,
description=description,
color=discord.Color.blue())
helpTitle = 'Command' + (" " *
helpWidth) + "| Description" + (" " *
(helpWidth * 2))
dots = "=" * len(helpTitle)
commandsHeader = helpTitle + "\n" + dots + "\n"
commandList = "\n".join(commandsList)
embed.add_field(name="Help",
value='```md\n' + str(commandsHeader) + str(commandList) +
'```',
inline=True)
await ctx.send(embed=embed)
@client.command()
async def ldr(ctx):
channel = client.get_channel(int(channels[str(
ctx.guild.id)][0]['channel']))
if ctx.message.channel == channel:
leaderboard = getLeaderboard(ctx)
if leaderboard == "Leaderboard unavailable":
leaderboard = ""
name = "Advent of Code"
code = channels[str(ctx.guild.id)][0]['code']
year = channels[str(ctx.guild.id)][0]['year']
description = "Year: " + str(year) + " | Table: " + str(code)
dots = " " * (channels[str(ctx.guild.id)][0]['width'] -
len(str("Scr. Name")))
ldTitle = "Scr. Name " + dots + " |Stars"
link = 'https://adventofcode.com/' + str(
year) + '/leaderboard/private/view/' + str(code)
embed = discord.Embed(title=name,
description=description,
color=discord.Color.blue(),
url=link)
embed.add_field(name="Leaderboard Top 20",
value='```md\n' + ldTitle + '\n' +
('=' * len(ldTitle)) + '\n' + leaderboard + '```',
inline=True)
embed.set_author(name="Visit the bot website",
url="https://AdventDiscordbot.mchrisgm.repl.co")
await channel.send(embed=embed)
keep_alive.keep_alive()
client.run(TOKEN)