-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
237 lines (169 loc) · 9.39 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
import discord
import requests
import os
import datetime
import sheets_connector
intents = discord.Intents.default()
intents.message_content = True
# Load whether to update google sheet or send scores to REST server
updateSheet = os.environ["UPDATE_SHEET"].lower() == "true"
updateServer = os.environ["UPDATE_SERVER"].lower() == "true"
client = discord.Client(intents=intents)
# Start dates for various games to allow for date calculation
connectionStartDate = datetime.datetime(2023, 6, 12)
wordleStartDate = datetime.datetime(2021,6, 20)
waffleStartDate = datetime.datetime(2022, 1, 22)
deluxeStartDate = datetime.datetime(2022, 5, 30)
zorseStartDate = datetime.datetime(2024, 10, 8)
connectionsColourMap = {
"🟨🟨🟨🟨": "yellow",
"🟩🟩🟩🟩": "green",
"🟦🟦🟦🟦": "blue",
"🟪🟪🟪🟪": "purple"
}
# Notice the bot is online
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
# Every time a message is sent, process the score
@client.event
async def on_message(message):
if message.channel.name == "daily-games":
# If sending to a REST server, send the raw data and allow the server to process as it wishes
if updateServer:
sendData = {}
sendData["key"] = os.environ["SERVER_API_KEY"]
sendData["user"] = message.author.id
if len(message.attachments) == 1:
sendData["message"] = message.attachments[0].url
else:
sendData["message"] = message.content
print(sendData)
requests.post(os.environ["SERVER_ENDPOINT"], data=sendData)
# If sending to a google sheet, processing must be done locally
if updateSheet:
# Determine if the player is me or my partner, and update variable accordingly
player = 0
if os.environ["MY_DISCORD_ID"] == str(message.author.id):
player = 1
# Process message depending on game
match message.content:
case s if s.startswith("Connections"):
connectionsList = s.split("\n")
# Manipulate the message string to isolate the puzzle number, and get
# the date from there
puzzleNumber = int(connectionsList[1].split("#")[1].replace(",", ""))
puzzleDate = connectionStartDate + datetime.timedelta(days=(puzzleNumber-1))
connectionsList = connectionsList[2:]
# Get the total number of guesses from the length of the string
guesses = len(connectionsList)
# Populate position dictionary
positions = {
"yellow": "-",
"green": "-",
"blue": "-",
"purple": "-"
}
counter = 1
# Get order of successful guesses
for row in connectionsList:
if row in connectionsColourMap:
colour = connectionsColourMap[row]
positions[colour] = str(counter)
counter += 1
# Create row to insert into spreadsheet
scoreRow = [[guesses, positions["yellow"], positions["green"], positions["blue"], positions["purple"], "Worked together"]]
# Process extracted score data
sheets_connector.update_score(sheets_connector.Game.CONNECTIONS, scoreRow, player, puzzleDate)
case s if s.startswith("Wordle"):
wordleList = s.split(" ")
# Manipulate the string to isolate the score
score = wordleList[2][0]
# Manipulate the message string to isolate the puzzle number, and get
# the date from there
puzzleNumber = int(wordleList[1].replace(",", ""))
puzzleDate = wordleStartDate + datetime.timedelta(days=(puzzleNumber-1))
# Create row to insert into spreadsheet
scoreRow = [[score, "N"]]
# Process extracted score data
sheets_connector.update_score(sheets_connector.Game.WORDLE, scoreRow, player, puzzleDate)
case s if s.startswith("https://www.nytimes.com/badges/games/mini.html"):
urlData = s.split("&")
# Manipulate the string to isolate the score
score = urlData[1].split("=")[1]
# Obtain the date of the puzzle from the message
dateData = urlData[0].split("=")[1].split("-")
crosswordStartDate = datetime.datetime(int(dateData[0]), int(dateData[1]), int(dateData[2]))
# Create row to insert into spreadsheet
scoreRow = [[f"=time(0,0,{score})", "N"]]
# Process extracted score data
sheets_connector.update_score(sheets_connector.Game.MINI, scoreRow, player, crosswordStartDate)
case s if s.startswith("#waffle"):
waffleList = s.split(" ")
# Manipulate the string to isolate the score
score = waffleList[1][0]
# Manipulate the message string to isolate the puzzle number, and get
# the date from there
puzzleNumber = int(waffleList[0].split("waffle")[1])
puzzleDate = waffleStartDate + datetime.timedelta(days=(puzzleNumber-1))
# Create row to insert into spreadsheet
scoreRow = [[score, "N"]]
# Process extracted score data
sheets_connector.update_score(sheets_connector.Game.WAFFLE, scoreRow, player, puzzleDate)
case s if s.startswith("#deluxewaffle"):
waffleList = s.split(" ")
# Manipulate the string to isolate the score
score = waffleList[1][0]
# If the puzzle was failed, obtain the final number of swaps required to
# solve the puzzle
if score == "X":
swaps = waffleList[2].split("\n")[0].replace("(", "").replace(")", "")
else:
# If the puzzle was not failed, the number of swaps is 25 - the number of stars
swaps = 25 - int(score)
# Manipulate the message string to isolate the puzzle number, and get
# the date from there
puzzleNumber = int(waffleList[0].split("waffle")[1])
puzzleDate = deluxeStartDate + datetime.timedelta(days=((puzzleNumber-1)*7))
# Create row to insert into spreadsheet
scoreRow = [[score, swaps]]
# Process extracted score data
sheets_connector.update_score(sheets_connector.Game.DELUXE, scoreRow, player, puzzleDate)
case s if s.startswith("I played https://squaredle.com"):
squaredleList = s.split("\n")
# Obtain the date of the puzzle from the message
now = datetime.datetime.now()
dateData = squaredleList[0].split(" ")[3].split("/")
puzzleDate = datetime.datetime(now.year, int(dateData[0]), int(dateData[1].replace(":", "")))
# If the player scored any bonus words, track it
try:
bonusWords = squaredleList[1].split("+")[1].split(" ")[0]
except IndexError:
bonusWords = "0"
# If the player has any extra score message, track it
try:
extraScore = squaredleList[2]
except IndexError:
extraScore = ""
# In the big squaredle, if an extra score message is not present, the
# players streak will be found on the same line. If the extra score
# is found to be the streak, set it to none
if extraScore.startswith("🔥"):
extraScore = ""
# Determine if the game is the mini squaredle or the big squaredle
if squaredleList[0].split(" ")[2].endswith("/xp"):
game = sheets_connector.Game.MINI_SQUAREDLE
else:
game = sheets_connector.Game.BIG_SQUAREDLE
# Create row to insert into spreadsheet
scoreRow = [[bonusWords, extraScore]]
# Process extracted score data
sheets_connector.update_score(game, scoreRow, player, puzzleDate)
case s if s.startswith("Zorse"):
zorseList = s.split("\n")
puzzleNum = int(zorseList[0].split("#")[1])
puzzleDate = zorseStartDate + datetime.timedelta(days=(puzzleNum-1))
puzzleHint = zorseList[1]
puzzleScore = zorseList[2]
sheets_connector.update_zorse(player, puzzleHint, puzzleScore, puzzleDate)
client.run(os.environ["DISCORD_TOKEN"])