Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit af0f98c

Browse files
committed
Check when Fanbox API reports 401 Unauthorized
1 parent 060aa02 commit af0f98c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

main.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ async def run():
4040
periodic_tasks.append(task)
4141
return task
4242

43-
def get_payload(response, check_error=True):
44-
if check_error and response.is_error:
45-
raise Exception('Fanbox API error', response, response.text)
46-
return json.loads(response.text)['body']
47-
4843
class RateLimiter():
4944
def __init__(self, rate_limit_seconds):
5045
self.limit_lock = asyncio.Lock()
@@ -67,8 +62,11 @@ def __init__(self, cookies, headers) -> None:
6762
async def get_user(self, user_id):
6863
response = await self.rate_limiter.limit(self.client.get('legacy/manage/supporter/user', params={'userId': user_id}))
6964
if response.is_error:
70-
return None
71-
return get_payload(response, check_error=False)
65+
if response.status_code == 401:
66+
raise Exception('Fanbox API reports 401 Unauthorized. session_cookies in the config file has likely been invalidated and needs to be updated. Restart the bot after updating.')
67+
else:
68+
return None
69+
return json.loads(response.text)['body']
7270

7371
def map_dict(a, f):
7472
b = {}
@@ -222,7 +220,7 @@ async def main(operator_mode):
222220
client = commands.bot.Bot(command_prefix='!', intents=intents)
223221
fanbox_client = FanboxClient(config.session_cookies, config.session_headers)
224222
lock = asyncio.Lock()
225-
db = await open_database()
223+
db = None
226224

227225
async def fetch_member(discord_id):
228226
try:
@@ -417,6 +415,11 @@ async def test_id(ctx, id):
417415
fanbox_role = await get_fanbox_role_with_pixiv_id(id)
418416
await ctx.send(f'{fanbox_role}')
419417

418+
@client.event
419+
async def on_command_error(ctx, error):
420+
logging.error(error)
421+
await ctx.send(error)
422+
420423
@client.event
421424
async def on_ready():
422425
logging.info(f'{client.user} has connected to Discord!')
@@ -446,6 +449,7 @@ async def on_message(message):
446449
await respond(message, 'system_error')
447450

448451
try:
452+
db = await open_database()
449453
token = config.operator_token if operator_mode else config.discord_token
450454
await client.start(token, reconnect=False)
451455
except Exception as ex:

0 commit comments

Comments
 (0)