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

Commit 7ca6f8d

Browse files
committed
Add database migration script
1 parent af0f98c commit 7ca6f8d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ When `strict_access` is set to `True`, the bot will disallow users from using th
1313
## Other functionality
1414
The bot can be configured to periodically purge old users without roles. See `cleanup` in the config.
1515

16-
The bot can periodically derole users that have passed the last day of their purchased Fanbox subscription. See `auto_derole` in the config.
16+
The bot can periodically derole users that have passed the last day of their purchased Fanbox subscription. See `auto_derole` in the config. The last day is appropraitely calculated such that a user will get a month of access time, cumulatively, from the last transaction that occurs in a specific month; Example: If a user had transactions on 6/15, 7/1, and 8/1, then the last day of their subscription is approximately 9/15.
17+
18+
`auto_derole` cannot differentiate between roles or what specific plan the user was subscribed to, so if you have roles based on different plans, they will not be switched between (either the user is subscribed or not subscribed). This limitation is the same as `allow_fallback`.
19+
20+
If you are using an older version of the bot without `auto_derole`, and want to update to a newer version with the feature, then all of your users will be deroled immediately when you run the bot, unless you migrate the database first. Typically this is not an issue, as users can simply send their Pixiv ID to the bot to get the role again. If you want to perform a migration without lots of deroles occurring, run `dbmig.py` first (it can take a while to run), then start the bot with `auto_derole` enabled.
1721

1822
## Admin commands
1923
Admin commands are prefixed with `!`, for example `!reset`

dbmig.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
import main
3+
import json
4+
import pickle
5+
6+
async def run():
7+
config = main.load_config(main.config_file)
8+
client = main.FanboxClient(config.session_cookies, config.session_headers)
9+
db = await main.open_database()
10+
with open('registry.dat', 'rb') as f:
11+
reg = pickle.load(f)
12+
for discord_id, pixiv_ids in reg['discord_ids'].items():
13+
for pixiv_id in pixiv_ids:
14+
user_data = await client.get_user(pixiv_id)
15+
if user_data is None:
16+
continue
17+
print(f'user {discord_id} {pixiv_id}')
18+
if main.is_user_transaction_subscribed(user_data):
19+
print('subscribed')
20+
async with db.cursor() as cur:
21+
await cur.execute('replace into member_pixiv values(?, ?)', (discord_id, pixiv_id))
22+
await cur.execute('replace into user_data values(?, ?)', (pixiv_id, json.dumps(user_data)))
23+
await db.commit()
24+
break
25+
await db.close()
26+
27+
asyncio.run(run())

0 commit comments

Comments
 (0)