mirror of
https://github.com/Sheldan/Sissi.git
synced 2026-01-01 15:28:25 +00:00
[SIS-9] adding migration scripts for custom commands and economy
This commit is contained in:
42
tools/custom-command-migration/command_importer.py
Normal file
42
tools/custom-command-migration/command_importer.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from sqlalchemy.sql import text
|
||||
|
||||
def import_commands(server_id, commands, connection):
|
||||
user_ids = [command.author_id for command in commands]
|
||||
create_users(server_id, user_ids, connection)
|
||||
command_id = 2
|
||||
for command in commands:
|
||||
statement = text("""INSERT INTO custom_command(server_id, id, creator_user_in_server_id, additional_message, name)
|
||||
VALUES(:server_id, :id,
|
||||
(select user_in_server_id from user_in_server where user_id = :author_id and server_id = :server_id),
|
||||
:additional_message, :name)""")
|
||||
connection.execute(statement,
|
||||
{'server_id': server_id, 'author_id': command.author_id,
|
||||
'name': command.command, 'additional_message': command.response,
|
||||
'id': command_id})
|
||||
command_id += 1
|
||||
print(f'Creating command for {command.command}')
|
||||
|
||||
|
||||
def create_users(server_id: int, user_ids, con):
|
||||
created_users = {}
|
||||
for user_id in user_ids:
|
||||
if not does_user_exist(user_id, con) and user_id not in created_users:
|
||||
create_user(user_id, con)
|
||||
create_user_in_server(user_id, server_id, con)
|
||||
created_users[user_id] = 1
|
||||
|
||||
|
||||
def does_user_exist(user_id, con):
|
||||
statement = text("""SELECT count(1) FROM auser where id = :id""")
|
||||
return con.execute(statement, {'id': user_id}).fetchone()[0] == 1
|
||||
|
||||
|
||||
def create_user(user_id, con):
|
||||
statement = text("""INSERT INTO auser(id) VALUES(:id)""")
|
||||
print(f'Creating user {user_id}')
|
||||
con.execute(statement, {'id': user_id})
|
||||
|
||||
|
||||
def create_user_in_server(user_id, server_id, con):
|
||||
statement = text("""INSERT INTO user_in_server(server_id, user_id) VALUES(:server_id, :user_id) returning user_in_server_id""")
|
||||
return con.execute(statement, {'user_id': user_id, 'server_id': server_id}).fetchone()[0]
|
||||
5
tools/custom-command-migration/dto.py
Normal file
5
tools/custom-command-migration/dto.py
Normal file
@@ -0,0 +1,5 @@
|
||||
class LegacyCommand:
|
||||
command = ''
|
||||
response = ''
|
||||
author_id = 0
|
||||
|
||||
24
tools/custom-command-migration/legacy_loader.py
Normal file
24
tools/custom-command-migration/legacy_loader.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import json
|
||||
|
||||
from dto import LegacyCommand
|
||||
|
||||
|
||||
def load_all_commands():
|
||||
with open('settings.json') as warnings:
|
||||
lines = warnings.read()
|
||||
command_obj = json.loads(lines)
|
||||
custom_commands = command_obj['414589031223512']['GUILD']['297910194841583616']['commands']
|
||||
all_command_dtos = []
|
||||
for command in custom_commands:
|
||||
custom_command = custom_commands[command]
|
||||
if custom_command is not None and 'response' in custom_command:
|
||||
if len(custom_command['response']) > 2048 or isinstance(custom_command['response'], list):
|
||||
continue
|
||||
command_dto = LegacyCommand()
|
||||
command_dto.command = command
|
||||
command_dto.author_id = custom_command['author']['id']
|
||||
command_dto.response = custom_command['response']
|
||||
all_command_dtos.append(command_dto)
|
||||
print(f'loaded {len(all_command_dtos)} commands.')
|
||||
return all_command_dtos
|
||||
|
||||
20
tools/custom-command-migration/main.py
Normal file
20
tools/custom-command-migration/main.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
import sqlalchemy as db
|
||||
|
||||
from legacy_loader import load_all_commands
|
||||
from command_importer import import_commands
|
||||
|
||||
server_id = os.getenv('SERVER_ID')
|
||||
|
||||
all_warnings = load_all_commands()
|
||||
db_host = os.getenv('DB_HOST')
|
||||
db_port = os.getenv('DB_PORT')
|
||||
db_database = os.getenv('DB_NAME')
|
||||
db_user = os.getenv('DB_USER')
|
||||
db_password = os.getenv('DB_PASS')
|
||||
|
||||
engine = db.create_engine('postgresql://%s:%s@%s:%s/%s' % (db_user, db_password, db_host, db_port, db_database))
|
||||
|
||||
with engine.connect() as con:
|
||||
with con.begin():
|
||||
import_commands(server_id, all_warnings, con)
|
||||
37
tools/economy-migration/credit_importer.py
Normal file
37
tools/economy-migration/credit_importer.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from sqlalchemy.sql import text
|
||||
|
||||
|
||||
def import_credits(server_id, credits, connection):
|
||||
user_ids = [credit.user_id for credit in credits]
|
||||
create_users(server_id, user_ids, connection)
|
||||
for credit in credits:
|
||||
statement = text("""INSERT INTO economy_user(server_id, last_pay_day, last_slots,
|
||||
id, credits)
|
||||
VALUES(:server_id, now(), now(),
|
||||
(select user_in_server_id from user_in_server where user_id = :user_id and server_id = :server_id), :credits)""")
|
||||
connection.execute(statement, {'server_id': server_id, 'user_id': credit.user_id, 'credits': credit.credits})
|
||||
print(f'Creating economy user for {credit.user_id}')
|
||||
|
||||
|
||||
def create_users(server_id: int, user_ids, con):
|
||||
created_users = {}
|
||||
for user_id in user_ids:
|
||||
if not does_user_exist(user_id, con) and user_id not in created_users:
|
||||
create_user(user_id, con)
|
||||
create_user_in_server(user_id, server_id, con)
|
||||
created_users[user_id] = 1
|
||||
|
||||
|
||||
def does_user_exist(user_id, con):
|
||||
statement = text("""SELECT count(1) FROM auser where id = :id""")
|
||||
return con.execute(statement, {'id': user_id}).fetchone()[0] == 1
|
||||
|
||||
|
||||
def create_user(user_id, con):
|
||||
statement = text("""INSERT INTO auser(id) VALUES(:id)""")
|
||||
print(f'Creating user {user_id}')
|
||||
con.execute(statement, {'id': user_id})
|
||||
|
||||
def create_user_in_server(user_id, server_id, con):
|
||||
statement = text("""INSERT INTO user_in_server(server_id, user_id) VALUES(:server_id, :user_id) returning user_in_server_id""")
|
||||
return con.execute(statement, {'user_id': user_id, 'server_id': server_id}).fetchone()[0]
|
||||
3
tools/economy-migration/dto.py
Normal file
3
tools/economy-migration/dto.py
Normal file
@@ -0,0 +1,3 @@
|
||||
class LegacyCredits:
|
||||
user_id = 0
|
||||
credits = 0
|
||||
20
tools/economy-migration/legacy_loader.py
Normal file
20
tools/economy-migration/legacy_loader.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import json
|
||||
|
||||
from dto import LegacyCredits
|
||||
|
||||
def load_all_credits():
|
||||
with open('settings.json') as warnings:
|
||||
lines = warnings.read()
|
||||
credit_obj = json.loads(lines)
|
||||
user_list = credit_obj['384734293238749']['MEMBER']['297910194841583616']
|
||||
all_credit_dtos = []
|
||||
for user in user_list:
|
||||
user_obj = user_list[user]
|
||||
dto = LegacyCredits()
|
||||
dto.credits = user_obj['balance']
|
||||
dto.user_id = user
|
||||
all_credit_dtos.append(dto)
|
||||
|
||||
print(f'loaded {len(all_credit_dtos)} credit entries.')
|
||||
return all_credit_dtos
|
||||
|
||||
20
tools/economy-migration/main.py
Normal file
20
tools/economy-migration/main.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
import sqlalchemy as db
|
||||
|
||||
from legacy_loader import load_all_credits
|
||||
from credit_importer import import_credits
|
||||
|
||||
server_id = os.getenv('SERVER_ID')
|
||||
|
||||
all_warnings = load_all_credits()
|
||||
db_host = os.getenv('DB_HOST')
|
||||
db_port = os.getenv('DB_PORT')
|
||||
db_database = os.getenv('DB_NAME')
|
||||
db_user = os.getenv('DB_USER')
|
||||
db_password = os.getenv('DB_PASS')
|
||||
|
||||
engine = db.create_engine('postgresql://%s:%s@%s:%s/%s' % (db_user, db_password, db_host, db_port, db_database))
|
||||
|
||||
with engine.connect() as con:
|
||||
with con.begin():
|
||||
import_credits(server_id, all_warnings, con)
|
||||
Reference in New Issue
Block a user