feat(profiles): Add community profiles.
This commit is contained in:
@@ -4,7 +4,9 @@ from typing import Optional, overload
|
||||
from datetime import timedelta
|
||||
|
||||
import discord
|
||||
from discord import app_commands as appcmds
|
||||
from discord.ext import commands as cmds
|
||||
from twitchAPI.type import AuthScope
|
||||
import twitchio
|
||||
from twitchio.ext import commands
|
||||
from twitchio import User
|
||||
@@ -18,6 +20,7 @@ from utils.lib import utc_now
|
||||
from . import logger
|
||||
from .data import ProfileData
|
||||
from .profile import UserProfile
|
||||
from .community import Community
|
||||
|
||||
|
||||
class ProfileCog(LionCog):
|
||||
@@ -89,7 +92,7 @@ class ProfileCog(LionCog):
|
||||
results.append(f"Migrated {len(twitch_rows)} attached twitch account(s).")
|
||||
|
||||
# And then mark the old profile as migrated
|
||||
await source_profile.update(migrate=target_profile.profileid)
|
||||
await source_profile.update(migrated=target_profile.profileid)
|
||||
results.append("Marking old profile as migrated.. finished!")
|
||||
return results
|
||||
|
||||
@@ -97,37 +100,107 @@ class ProfileCog(LionCog):
|
||||
"""
|
||||
Fetch a UserProfile by the given id.
|
||||
"""
|
||||
return await UserProfile.fetch_profile(self.bot, profile_id=profile_id)
|
||||
return await UserProfile.fetch(self.bot, profile_id=profile_id)
|
||||
|
||||
async def fetch_profile_discord(self, user: discord.Member | discord.User) -> UserProfile:
|
||||
"""
|
||||
Fetch or create a UserProfile from the provided discord account.
|
||||
"""
|
||||
profile = await UserProfile.fetch_from_discordid(user.id)
|
||||
profile = await UserProfile.fetch_from_discordid(self.bot, user.id)
|
||||
if profile is None:
|
||||
profile = await UserProfile.create_from_discord(user)
|
||||
profile = await UserProfile.create_from_discord(self.bot, user)
|
||||
return profile
|
||||
|
||||
async def fetch_profile_twitch(self, user: twitchio.User) -> UserProfile:
|
||||
"""
|
||||
Fetch or create a UserProfile from the provided twitch account.
|
||||
"""
|
||||
profile = await UserProfile.fetch_from_twitchid(user.id)
|
||||
profile = await UserProfile.fetch_from_twitchid(self.bot, user.id)
|
||||
if profile is None:
|
||||
profile = await UserProfile.create_from_twitch(user)
|
||||
profile = await UserProfile.create_from_twitch(self.bot, user)
|
||||
return profile
|
||||
|
||||
async def fetch_community_discord(self, guildid: int, create=True):
|
||||
...
|
||||
# Community API
|
||||
def add_community_migrator(self, migrator, name=None):
|
||||
name = name or migrator.__name__
|
||||
self._comm_migrators[name or migrator.__name__] = migrator
|
||||
|
||||
async def fetch_community_twitch(self, guildid: int, create=True):
|
||||
...
|
||||
logger.info(
|
||||
f"Added community migrator {name}: {migrator}"
|
||||
)
|
||||
return migrator
|
||||
|
||||
async def fetch_community(self, communityid: int):
|
||||
...
|
||||
def del_community_migrator(self, name: str):
|
||||
migrator = self._comm_migrators.pop(name, None)
|
||||
|
||||
logger.info(
|
||||
f"Removed community migrator {name}: {migrator}"
|
||||
)
|
||||
|
||||
@log_wrap(action="community migration")
|
||||
async def migrate_community(self, source_comm, target_comm) -> list[str]:
|
||||
logger.info(
|
||||
f"Beginning community migration from {source_comm!r} to {target_comm!r}"
|
||||
)
|
||||
results = []
|
||||
# Wrap this in a transaction so if something goes wrong with migration,
|
||||
# we roll back safely (although this may mess up caches)
|
||||
async with self.bot.db.connection() as conn:
|
||||
self.bot.db.conn = conn
|
||||
async with conn.transaction():
|
||||
for name, migrator in self._comm_migrators.items():
|
||||
try:
|
||||
result = await migrator(source_comm, target_comm)
|
||||
if result:
|
||||
results.append(result)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
f"Unexpected exception running community migrator {name} "
|
||||
f"migrating {source_comm!r} to {target_comm!r}."
|
||||
)
|
||||
raise
|
||||
|
||||
# Move all Discord and Twitch community preferences over to the new profile
|
||||
discord_rows = await self.data.DiscordCommunityRow.table.update_where(
|
||||
profileid=source_comm.communityid
|
||||
).set(communityid=target_comm.communityid)
|
||||
results.append(f"Migrated {len(discord_rows)} attached discord guilds.")
|
||||
|
||||
twitch_rows = await self.data.TwitchCommunityRow.table.update_where(
|
||||
communityid=source_comm.communityid
|
||||
).set(communityid=target_comm.communityid)
|
||||
results.append(f"Migrated {len(twitch_rows)} attached twitch channel(s).")
|
||||
|
||||
# And then mark the old community as migrated
|
||||
await source_comm.update(migrated=target_comm.communityid)
|
||||
results.append("Marking old community as migrated.. finished!")
|
||||
return results
|
||||
|
||||
async def fetch_community_by_id(self, community_id: int) -> Community:
|
||||
"""
|
||||
Fetch a Community by the given id.
|
||||
"""
|
||||
return await Community.fetch(self.bot, community_id=community_id)
|
||||
|
||||
async def fetch_community_discord(self, guild: discord.Guild) -> Community:
|
||||
"""
|
||||
Fetch or create a Community from the provided discord guild.
|
||||
"""
|
||||
comm = await Community.fetch_from_discordid(self.bot, guild.id)
|
||||
if comm is None:
|
||||
comm = await Community.create_from_discord(self.bot, guild)
|
||||
return comm
|
||||
|
||||
async def fetch_community_twitch(self, user: twitchio.User) -> Community:
|
||||
"""
|
||||
Fetch or create a Community from the provided twitch account.
|
||||
"""
|
||||
community = await Community.fetch_from_twitchid(self.bot, user.id)
|
||||
if community is None:
|
||||
community = await Community.create_from_twitch(self.bot, user)
|
||||
return community
|
||||
|
||||
# ----- Profile Commands -----
|
||||
|
||||
@cmds.hybrid_group(
|
||||
name='profiles',
|
||||
description="Base comand group for user profiles."
|
||||
@@ -170,6 +243,7 @@ class ProfileCog(LionCog):
|
||||
f"User {authrow} obtained from Twitch authentication does not exist."
|
||||
)
|
||||
await ctx.error_reply("Sorry, something went wrong. Please try again later!")
|
||||
return
|
||||
|
||||
user = results[0]
|
||||
|
||||
@@ -189,7 +263,7 @@ class ProfileCog(LionCog):
|
||||
# Attach the discord row to the profile
|
||||
await source_profile.attach_discord(ctx.author)
|
||||
await message.edit(
|
||||
content=f"Successfully connect to Twitch profile **{user.name}**! There was no profile data to merge."
|
||||
content=f"Successfully connected to Twitch profile **{user.name}**! There was no profile data to merge."
|
||||
)
|
||||
elif source_profile is None and author_profile is None:
|
||||
profile = await UserProfile.create_from_discord(self.bot, ctx.author)
|
||||
@@ -217,6 +291,114 @@ class ProfileCog(LionCog):
|
||||
content = '\n'.join((
|
||||
"## Connecting Twitch account and merging profiles...",
|
||||
*results,
|
||||
"**Successfully linked account and merge profile data!**"
|
||||
"**Successfully linked account and merged profile data!**"
|
||||
))
|
||||
await message.edit(content=content)
|
||||
|
||||
# ----- Community Commands -----
|
||||
@cmds.hybrid_group(
|
||||
name='community',
|
||||
description="Base comand group for community profiles."
|
||||
)
|
||||
async def community_grp(self, ctx: LionContext):
|
||||
...
|
||||
|
||||
@community_grp.group(
|
||||
name='link',
|
||||
description="Base command group for linking communities"
|
||||
)
|
||||
async def community_link_grp(self, ctx: LionContext):
|
||||
...
|
||||
|
||||
@community_link_grp.command(
|
||||
name='twitch',
|
||||
description="Link a twitch account to this community."
|
||||
)
|
||||
@appcmds.guild_only()
|
||||
@appcmds.default_permissions(manage_guild=True)
|
||||
async def comm_link_twitch_cmd(self, ctx: LionContext):
|
||||
if not ctx.interaction:
|
||||
return
|
||||
assert ctx.guild is not None
|
||||
|
||||
await ctx.interaction.response.defer(ephemeral=True)
|
||||
|
||||
if not ctx.author.guild_permissions.manage_guild:
|
||||
await ctx.error_reply("You need the `MANAGE_GUILD` permission to link this guild to a community.")
|
||||
return
|
||||
|
||||
# Ask the user to go through auth to get their userid
|
||||
auth_cog = self.bot.get_cog('TwitchAuthCog')
|
||||
flow = await auth_cog.start_auth(
|
||||
scopes=[
|
||||
AuthScope.CHAT_EDIT,
|
||||
AuthScope.CHAT_READ,
|
||||
AuthScope.MODERATION_READ,
|
||||
AuthScope.CHANNEL_BOT,
|
||||
]
|
||||
)
|
||||
message = await ctx.reply(
|
||||
f"Please [click here]({flow.auth.return_auth_url()}) to link your Twitch channel to this server."
|
||||
)
|
||||
authrow = await flow.run()
|
||||
await message.edit(
|
||||
content="Authentication Complete! Beginning community profile merge..."
|
||||
)
|
||||
|
||||
results = await self.crocbot.fetch_users(ids=[authrow.userid])
|
||||
if not results:
|
||||
logger.error(
|
||||
f"User {authrow} obtained from Twitch authentication does not exist."
|
||||
)
|
||||
await ctx.error_reply("Sorry, something went wrong. Please try again later!")
|
||||
return
|
||||
|
||||
user = results[0]
|
||||
|
||||
# Retrieve author's profile if it exists
|
||||
guild_comm = await Community.fetch_from_discordid(self.bot, ctx.guild.id)
|
||||
|
||||
# Check if the twitch-side user has a profile
|
||||
twitch_comm = await Community.fetch_from_twitchid(self.bot, user.id)
|
||||
|
||||
if guild_comm and twitch_comm is None:
|
||||
# All we need to do is attach the twitch row
|
||||
await guild_comm.attach_twitch(user)
|
||||
await message.edit(
|
||||
content=f"Successfully linked Twitch channel **{user.name}**! There was no community data to merge."
|
||||
)
|
||||
elif twitch_comm and guild_comm is None:
|
||||
# Attach the discord row to the profile
|
||||
await twitch_comm.attach_discord(ctx.guild)
|
||||
await message.edit(
|
||||
content=f"Successfully connected to Twitch channel **{user.name}**!"
|
||||
)
|
||||
elif twitch_comm is None and guild_comm is None:
|
||||
profile = await Community.create_from_discord(self.bot, ctx.guild)
|
||||
await profile.attach_twitch(user)
|
||||
|
||||
await message.edit(
|
||||
content=f"Created a new community for this server and linked Twitch account **{user.name}**."
|
||||
)
|
||||
elif guild_comm.communityid == twitch_comm.communityid:
|
||||
await message.edit(
|
||||
content=f"This server is already linked to the Twitch channel **{user.name}**!"
|
||||
)
|
||||
else:
|
||||
# Migrate the existing profile data to the new profiles
|
||||
try:
|
||||
results = await self.migrate_community(twitch_comm, guild_comm)
|
||||
except Exception:
|
||||
await ctx.error_reply(
|
||||
"An issue was encountered while merging your community profiles!\n"
|
||||
"Migration rolled back, no data has been lost.\n"
|
||||
"The developer has been notified. Please try again later!"
|
||||
)
|
||||
raise
|
||||
|
||||
content = '\n'.join((
|
||||
"## Connecting Twitch account and merging community profiles...",
|
||||
*results,
|
||||
"**Successfully linked account and merged community data!**"
|
||||
))
|
||||
await message.edit(content=content)
|
||||
|
||||
Reference in New Issue
Block a user