Skip to content

Commit

Permalink
added user data transfer method and command
Browse files Browse the repository at this point in the history
  • Loading branch information
99oblivius committed Sep 20, 2024
1 parent 7441a73 commit 7f045a4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/cogs/settings/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ async def autocomplete_platform_id(self, interaction: nextcord.Interaction, plat
return ["Invalid Steam ID format"]
else:
return ["Unsupported platform for ID validation"]

@settings.subcommand(name="transfer_user_data", description="Transfer all of a discord user's data to another discord account")
async def settings_transfer_user(self, interaction: nextcord.Interaction, old_user_id, new_user_id):
settings = await self.bot.store.get_settings(interaction.guild.id)
try:
await self.bot.store.transfer_user(interaction.guild.id, int(old_user_id), int(new_user_id))
except Exception as e:
await log_moderation(interaction, settings.log_channel, "User data transfer", f"User <@{old_user_id}> FAILED to move to <@{new_user_id}>.")
return await interaction.response.send_message(f"There was a failure in the transfer:\n{repr(e)}", ephemeral=True)

await interaction.response.send_message(f"User <@{old_user_id}> was moved to <@{new_user_id}> with success!", ephemeral=True)
await log_moderation(interaction, settings.log_channel, "User data transfer", f"User <@{old_user_id}> was moved to <@{new_user_id}> with success!")

def setup(bot):
bot.add_cog(Settings(bot))
bot.add_cog(Settings(bot))
54 changes: 54 additions & 0 deletions src/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,60 @@ async def set_user_block(self, guild_id: int, user_id: int, expiration: datetime

await session.commit()

@log_db_operation
async def transfer_user(self, guild_id: int, old_user_id: int, new_user_id: int) -> bool:
async with self._session_maker() as session:
async with session.begin():
old_user = await session.execute(
select(MMBotUsers).where(
MMBotUsers.guild_id == guild_id,
MMBotUsers.user_id == old_user_id))
new_user = await session.execute(
select(MMBotUsers).where(
MMBotUsers.guild_id == guild_id,
MMBotUsers.user_id == new_user_id))

if not old_user.scalar_one_or_none() or not new_user.scalar_one_or_none():
raise ValueError("Both old and new users must exist in the database")

tables_columns = [
(UserPlatformMappings, ['user_id']),
(MMBotQueueUsers, ['user_id']),
(MMBotBlockedUsers, ['user_id', 'blocked_by']),
(MMBotWarnedUsers, ['user_id', 'moderator_id']),
(MMBotUserMatchStats, ['user_id']),
(MMBotUserAbandons, ['user_id']),
(MMBotMatchPlayers, ['user_id']),
(MMBotUserBans, ['user_id']),
(MMBotUserMapPicks, ['user_id']),
(MMBotUserSidePicks, ['user_id']),
(MMBotUserNotifications, ['user_id'])
]

for table, columns in tables_columns:
for column in columns:
await session.execute(
update(table)
.where(
table.guild_id == guild_id,
getattr(table, column) == old_user_id)
.values(**{column: new_user_id}))

await session.execute(
delete(MMBotUserSummaryStats)
.where(
MMBotUserSummaryStats.guild_id == guild_id,
MMBotUserSummaryStats.user_id == new_user_id))

await session.execute(
update(MMBotUserSummaryStats)
.where(
MMBotUserSummaryStats.guild_id == guild_id,
MMBotUserSummaryStats.user_id == old_user_id)
.values(user_id=new_user_id))

log.info(f"Transferred user data from {old_user_id} to {new_user_id} in guild {guild_id}")


############
# ABANDONS #
Expand Down

0 comments on commit 7f045a4

Please sign in to comment.