pokemap and a few changes

This commit is contained in:
kita 2024-02-10 10:58:37 +06:00
parent 4538d55337
commit 565f3b292b
4 changed files with 52 additions and 39 deletions

View file

@ -12,7 +12,7 @@ This bot is currently used for the SuperTuxKart Discord, but you can self host i
- [x] Player searching - [x] Player searching
- [x] Friends list - [x] Friends list
- [x] Server list - [x] Server list
- [ ] PokeMap - [x] PokeMap
- [ ] Addon querying - [ ] Addon querying
- [x] Ranking info of a player - [x] Ranking info of a player

52
bot.py
View file

@ -22,7 +22,8 @@ extensions = (
"cogs.online", "cogs.online",
"cogs.playertrack", "cogs.playertrack",
"cogs.core", "cogs.core",
"cogs.misc" "cogs.misc",
"cogs.pokemap"
) )
@ -58,7 +59,7 @@ class Lina(commands.Bot):
async def stkPostReq(self, target, args): async def stkPostReq(self, target, args):
"""Helper function to send a POST request to STK servers.""" """Helper function to send a POST request to STK servers."""
assert self.session is not None assert self.session is not None
log.info( log.debug(
"Sending %s to %s", "Sending %s to %s",
args.replace(str(self.stk_token), "[REDACTED]").replace(constants.STK_PASSWORD, "[REDACTED]"), args.replace(str(self.stk_token), "[REDACTED]").replace(constants.STK_PASSWORD, "[REDACTED]"),
str(self.session._base_url) + target str(self.session._base_url) + target
@ -89,26 +90,19 @@ class Lina(commands.Bot):
async def authSTK(self): async def authSTK(self):
"""Authenticate to STK""" """Authenticate to STK"""
log.info(f"Trying to authenticate STK account {constants.STK_USERNAME}") log.info(f"Trying to authenticate STK account {constants.STK_USERNAME}")
try:
loginPayload = await self.stkPostReq( loginPayload = await self.stkPostReq(
"/api/v2/user/connect", "/api/v2/user/connect",
f"username={constants.STK_USERNAME}&" f"username={constants.STK_USERNAME}&"
f"password={constants.STK_PASSWORD}&" f"password={constants.STK_PASSWORD}&"
"save-session=true" "save-session=true"
) )
except Exception:
log.exception("Unable to authenticate due to error. The bot will now shut down.")
return await self.close()
if loginPayload.attrib["success"] == "no":
log.critical(f"Unable to login! {loginPayload.attrib['info']}")
log.critical("The bot will now shut down.")
return await self.close()
self.stk_userid = loginPayload.attrib["userid"] self.stk_userid = loginPayload.attrib["userid"]
self.stk_token = loginPayload.attrib["token"] self.stk_token = loginPayload.attrib["token"]
log.info(f"STK user {loginPayload.attrib['username']} logged in successfully.") log.info(f"STK user {loginPayload.attrib['username']} logged in successfully.")
if not self.stkPoll.is_running():
self.stkPoll.start()
@tasks.loop(minutes=1) @tasks.loop(minutes=1)
async def stkPoll(self): async def stkPoll(self):
@ -120,7 +114,7 @@ class Lina(commands.Bot):
) )
except STKRequestError as e: except STKRequestError as e:
if str(e) in "Session not valid. Please sign in.": if str(e) in "Session not valid. Please sign in.":
log.error("Session invalidated. Reauthenticating...") log.warning("Session was invalidated. Reauthenticating...")
await self.authSTK() await self.authSTK()
else: else:
log.error("Poll request failed: %s", e) log.error("Poll request failed: %s", e)
@ -169,6 +163,16 @@ class Lina(commands.Bot):
color=self.accent_color color=self.accent_color
), ephemeral=True) ), ephemeral=True)
async def afterStkAuth(self):
for extension in extensions:
log.debug("Loading extension %s", extension)
try:
await self.load_extension(extension)
except Exception:
log.exception(f"Unable to load extension {extension}.")
else:
log.debug("Successfully loaded extension %s.", extension)
async def setup_hook(self): async def setup_hook(self):
self.tree.error(self.on_app_command_error) self.tree.error(self.on_app_command_error)
@ -182,23 +186,34 @@ class Lina(commands.Bot):
} }
) )
await self.authSTK()
for extension in extensions:
try: try:
await self.load_extension(extension) await self.authSTK()
except Exception: except Exception as e:
log.exception(f"Unable to load extension {extension}.") log.exception("STK account authentication failed. "
"See below for details.",
exc_info=e)
await self.close()
else:
await self.afterStkAuth()
async def close(self): async def close(self):
"""Shut down lina""" """Shut down lina"""
log.info("lina is shutting down...") log.info("lina is shutting down...")
if hasattr(self, 'session'): if hasattr(self, 'session'):
# it's important to check if the session is closed in case
# something went wrong before the session is set up
if not self.session.closed:
try: try:
if self.stk_userid and self.stk_token:
# send a client quit request so that internal online
# counter is deducted and mark the STK account offline
await self.stkPostReq("/api/v2/user/client-quit", await self.stkPostReq("/api/v2/user/client-quit",
f"userid={self.stk_userid}&" f"userid={self.stk_userid}&"
f"token={self.stk_token}") f"token={self.stk_token}")
else:
log.warning("userid and token is absent. "
"will not send client quit request.")
finally: finally:
await self.session.close() await self.session.close()
@ -221,4 +236,3 @@ class Lina(commands.Bot):
async def on_ready(self): async def on_ready(self):
log.info(f"Bot {self.user} ({self.user.id}) is ready!") log.info(f"Bot {self.user} ({self.user.id}) is ready!")
self.stkPoll.start()

View file

@ -115,8 +115,7 @@ class Online(commands.Cog):
self.cachedSTKUsers[_["id"]] = _["username"] self.cachedSTKUsers[_["id"]] = _["username"]
log.info("Finished populating player cache.") log.info("Finished caching %d STK users.", len(data))
@tasks.loop(hours=2) @tasks.loop(hours=2)
async def syncAddons(self): async def syncAddons(self):

View file

@ -113,7 +113,7 @@ class PlayerTrack(commands.Cog):
if _id in srvCreated: if _id in srvCreated:
log.info("New server created: %s (%s) with id %d and address %s:%d" % ( log.debug("New server created: %s (%s) with id %d and address %s:%d" % (
tree[0][i][0].attrib['name'], tree[0][i][0].attrib['name'],
tree[0][i][0].attrib['country_code'], tree[0][i][0].attrib['country_code'],
int(tree[0][i][0].attrib['id']), int(tree[0][i][0].attrib['id']),
@ -142,7 +142,7 @@ class PlayerTrack(commands.Cog):
if _id in srvDeleted: if _id in srvDeleted:
log.info("Server deleted: %s (%s) with id %d and address %s:%d" % ( log.debug("Server deleted: %s (%s) with id %d and address %s:%d" % (
self.lastserverlist[0][i][0].attrib['name'], self.lastserverlist[0][i][0].attrib['name'],
self.lastserverlist[0][i][0].attrib['country_code'], self.lastserverlist[0][i][0].attrib['country_code'],
int(self.lastserverlist[0][i][0].attrib['id']), int(self.lastserverlist[0][i][0].attrib['id']),
@ -223,13 +223,13 @@ class PlayerTrack(commands.Cog):
if serverCTrack != oldserverCTrack: if serverCTrack != oldserverCTrack:
if not oldserverCTrack: if not oldserverCTrack:
log.info("Stub: Game started at %s %s - %s" % ( log.debug("Stub: Game started at %s %s - %s" % (
serverInfo.attrib['name'], serverInfo.attrib['name'],
serverInfo.attrib['id'], serverInfo.attrib['id'],
serverCTrack serverCTrack
)) ))
elif not serverCTrack: elif not serverCTrack:
log.info( log.debug(
"Stub: Game ended at %s %s" % ( "Stub: Game ended at %s %s" % (
serverInfo.attrib['name'], serverInfo.attrib['name'],
serverInfo.attrib['id'] serverInfo.attrib['id']