diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index 9317429..80e9c83 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -26,6 +26,7 @@ from litecord.common.guilds import ( create_guild_channel, delete_guild, create_guild_settings, + add_member, ) from ..auth import token_check @@ -49,20 +50,6 @@ DEFAULT_EVERYONE_PERMS = 104324161 bp = Blueprint("guilds", __name__) -async def add_member(guild_id: int, user_id: int): - """Add a user to a guild.""" - await app.db.execute( - """ - INSERT INTO members (user_id, guild_id) - VALUES ($1, $2) - """, - user_id, - guild_id, - ) - - await create_guild_settings(guild_id, user_id) - - async def guild_create_roles_prep(guild_id: int, roles: list): """Create roles in preparation in guild create.""" # by reaching this point in the code that means @@ -159,7 +146,7 @@ async def create_guild(): j.get("explicit_content_filter", 0), ) - await add_member(guild_id, user_id) + await add_member(guild_id, user_id, basic=True) # create the default @everyone role (everyone has it by default, # so we don't insert that in the table) diff --git a/litecord/blueprints/invites.py b/litecord/blueprints/invites.py index ea82ece..01f4c67 100644 --- a/litecord/blueprints/invites.py +++ b/litecord/blueprints/invites.py @@ -38,7 +38,7 @@ from litecord.blueprints.checks import ( ) from litecord.blueprints.dm_channels import gdm_is_member, gdm_add_recipient -from litecord.common.guilds import create_guild_settings +from litecord.common.guilds import add_member log = Logger(__name__) bp = Blueprint("invites", __name__) @@ -94,7 +94,7 @@ async def invite_precheck(user_id: int, guild_id: int): ) if banned is not None: - raise InvalidInvite("You are banned.") + raise InvalidInvite(40007) async def invite_precheck_gdm(user_id: int, channel_id: int): @@ -121,58 +121,6 @@ async def _inv_check_age(inv: dict): raise InvalidInvite("Too many uses") -async def _guild_add_member(guild_id: int, user_id: int): - """Add a user to a guild. - - Dispatches: - - GUILD_MEMBER_ADD to all members. - - lazy guild events for member add. - - subscribes the peer to the guild. - - dispatches a GUILD_CREATE to the peer. - """ - - # TODO: system message for member join - await app.db.execute( - """ - INSERT INTO members (user_id, guild_id) - VALUES ($1, $2) - """, - user_id, - guild_id, - ) - - await create_guild_settings(guild_id, user_id) - - # add the @everyone role to the invited member - await app.db.execute( - """ - INSERT INTO member_roles (user_id, guild_id, role_id) - VALUES ($1, $2, $3) - """, - user_id, - guild_id, - guild_id, - ) - - # tell current members a new member came up - member = await app.storage.get_member_data_one(guild_id, user_id) - await app.dispatcher.dispatch_guild( - guild_id, "GUILD_MEMBER_ADD", {**member, **{"guild_id": str(guild_id)}} - ) - - # update member lists for the new member - await app.dispatcher.dispatch("lazy_guild", guild_id, "new_member", user_id) - - # subscribe new member to guild, so they get events n stuff - await app.dispatcher.sub("guild", guild_id, user_id) - - # tell the new member that theres the guild it just joined. - # we use dispatch_user_guild so that we send the GUILD_CREATE - # just to the shards that are actually tied to it. - guild = await app.storage.get_guild_full(guild_id, user_id, 250) - await app.dispatcher.dispatch_user_guild(user_id, guild_id, "GUILD_CREATE", guild) - - async def use_invite(user_id, invite_code): """Try using an invite""" inv = await app.db.fetchrow( @@ -186,7 +134,7 @@ async def use_invite(user_id, invite_code): ) if inv is None: - raise UnknownInvite("Unknown invite") + raise UnknownInvite(10006) await _inv_check_age(inv) @@ -200,7 +148,7 @@ async def use_invite(user_id, invite_code): await gdm_add_recipient(channel_id, user_id) else: await invite_precheck(user_id, guild_id) - await _guild_add_member(guild_id, user_id) + await add_member(guild_id, user_id) await app.db.execute( """ diff --git a/litecord/common/guilds.py b/litecord/common/guilds.py index d7d1e25..1b63322 100644 --- a/litecord/common/guilds.py +++ b/litecord/common/guilds.py @@ -232,3 +232,54 @@ async def create_guild_settings(guild_id: int, user_id: int): guild_id, m_notifs, ) + + +async def add_member(guild_id: int, user_id: int, *, basic=False): + """Add a user to a guild. + + If `basic` is set to true, side-effects from member adding won't be + propagated. + """ + await app.db.execute( + """ + INSERT INTO members (user_id, guild_id) + VALUES ($1, $2) + """, + user_id, + guild_id, + ) + + await create_guild_settings(guild_id, user_id) + + if basic: + return + + # TODO: system message for member join + + await app.db.execute( + """ + INSERT INTO member_roles (user_id, guild_id, role_id) + VALUES ($1, $2, $3) + """, + user_id, + guild_id, + guild_id, + ) + + # tell current members a new member came up + member = await app.storage.get_member_data_one(guild_id, user_id) + await app.dispatcher.dispatch_guild( + guild_id, "GUILD_MEMBER_ADD", {**member, **{"guild_id": str(guild_id)}} + ) + + # update member lists for the new member + await app.dispatcher.dispatch("lazy_guild", guild_id, "new_member", user_id) + + # subscribe new member to guild, so they get events n stuff + await app.dispatcher.sub("guild", guild_id, user_id) + + # tell the new member that theres the guild it just joined. + # we use dispatch_user_guild so that we send the GUILD_CREATE + # just to the shards that are actually tied to it. + guild = await app.storage.get_guild_full(guild_id, user_id, 250) + await app.dispatcher.dispatch_user_guild(user_id, guild_id, "GUILD_CREATE", guild) diff --git a/litecord/errors.py b/litecord/errors.py index 51c4d14..0820f51 100644 --- a/litecord/errors.py +++ b/litecord/errors.py @@ -43,6 +43,7 @@ ERR_MSG_MAP = { 30013: "Maximum number of guild channels reached (500)", 30016: "Maximum number of invites reached (1000)", 40001: "Unauthorized", + 40007: "The user is banned from this guild", 50001: "Missing access", 50002: "Invalid account type", 50003: "Cannot execute action on a DM channel",