async def add_starboard_emoji( bot: commands.Bot, starboard_id: int, guild: discord.Guild, emoji: Union[discord.Emoji, str] ) -> None: check_sbemoji = \ """SELECT * FROM sbemojis WHERE name=$1 AND starboard_id=$2""" get_all_sbemojis = \ """SELECT * FROM sbemojis WHERE starboard_id=$1""" check_starboard = \ """SELECT * FROM starboards WHERE id=$1""" if not isinstance(emoji, discord.Emoji): if not functions.is_emoji(emoji): raise errors.InvalidArgument( "I don't recognize that emoji. If it is a custom " "emoji, it has to be in this server." ) emoji_name = str(emoji.id) if isinstance( emoji, discord.Emoji) else str(emoji) emoji_id = emoji.id if isinstance( emoji, discord.Emoji) else None limit = await functions.get_limit(bot, 'emojis', guild.id) conn = bot.db.conn async with bot.db.lock: async with conn.transaction(): sql_starboard = await conn.fetchrow( check_starboard, starboard_id ) if sql_starboard is None: raise errors.DoesNotExist("That is not a starboard!") all_sbemojis = await conn.fetch( get_all_sbemojis, starboard_id ) if len(all_sbemojis) + 1 > limit: raise errors.NoPremiumError( "You have reached your limit for emojis " "on this starboard.\nSee the last page of " "`sb!tutorial` for more info." ) sbemoji = await conn.fetchrow( check_sbemoji, emoji_name, starboard_id ) if sbemoji is not None: raise errors.AlreadyExists( "That is already a starboard emoji!" ) await bot.db.q.create_sbemoji.fetch( emoji_id, starboard_id, emoji_name, False )
async def add_starboard(bot: commands.Bot, channel: discord.TextChannel): check_starboard = \ """SELECT * FROM starboards WHERE id=$1""" check_aschannel = \ """SELECT * FROM aschannels WHERE id=$1""" get_starboards = \ """SELECT * FROM starboards WHERE guild_id=$1""" guild = channel.guild perms = channel.permissions_for(guild.me) if not perms.read_messages: raise errors.BotNeedsPerms( "I need the `READ MESSAGES` permission in that channel.") elif not perms.read_message_history: raise errors.BotNeedsPerms( "I need the `READ MESSAGE HISTORY` permission in that channel.") elif not perms.send_messages: raise errors.BotNeedsPerms( "I need the `SEND MESSAGES` permission in that channel.") elif not perms.embed_links: raise errors.BotNeedsPerms( "I need the `EMBED LINKS` permission in that channel.") elif not perms.add_reactions: raise errors.BotNeedsPerms( "I need the `ADD REACTIONS` permission in that channel.") limit = await functions.get_limit(bot.db, 'starboards', guild) conn = bot.db.conn async with bot.db.lock: async with conn.transaction(): await functions.check_or_create_existence(bot.db, conn, bot, channel.guild.id) all_starboards = await conn.fetch(get_starboards, guild.id) if len(all_starboards) + 1 > limit: raise errors.NoPremiumError( "You have reached your limit for starboards on this server" "\nTo add more starboards, the owner of this server must " "become a patron.") sql_starboard = await conn.fetchrow(check_starboard, channel.id) if sql_starboard is not None: raise errors.AlreadyExists("That is already a starboard!") sql_aschannel = await conn.fetchrow(check_aschannel, channel.id) if sql_aschannel is not None: raise errors.AlreadyExists( "That channel is already an AutoStar channel!\n" "A channel can't be both an AutoStar channel " "and a starboard.") await bot.db.q.create_starboard.fetch(channel.id, guild.id) await add_starboard_emoji(bot, channel.id, channel.guild, 'тнР')
async def add_aschannel(bot: commands.Bot, channel: discord.TextChannel): check_aschannel = \ """SELECT * FROM aschannels WHERE id=$1""" check_starboard = \ """SELECT * FROM starboards WHERE id=$1""" get_aschannels = \ """SELECT * FROM aschannels WHERE guild_id=$1""" guild = channel.guild perms = channel.permissions_for(guild.me) limit = await functions.get_limit(bot.db, 'aschannels', guild) conn = bot.db.conn if not perms.read_messages: raise errors.BotNeedsPerms( "I need the 'READ MESSAGES' permission in that channel.") elif not perms.manage_messages: raise errors.BotNeedsPerms( "I need the `MANAGE MESSAGES` permission in that channel.") elif not perms.add_reactions: raise errors.BotNeedsPerms( "I need the `ADD REACTIONS` permission in that channel.") async with bot.db.lock: async with conn.transaction(): await functions.check_or_create_existence(bot.db, conn, bot, guild_id=guild.id) all_aschannels = await conn.fetch(get_aschannels, guild.id) if len(all_aschannels) >= limit: raise errors.NoPremiumError( "You have reached your limit for AutoStar Channels" " in this server.\nTo add more AutoStar channels, " "the server owner must become a patron.") sql_aschannel = await conn.fetchrow(check_aschannel, channel.id) if sql_aschannel is not None: raise errors.AlreadyExists( "That is already an AutoStar Channel!") sql_starboard = await conn.fetchrow(check_starboard, channel.id) if sql_starboard is not None: raise errors.AlreadyExists( "That channel is already a starboard!\n" "A channel can't be both a starboard and an " "AutoStar channel.") await bot.db.q.create_aschannel.fetch(channel.id, guild.id)
async def add_xp_role(bot: commands.Bot, role: discord.Role, req_xp: int) -> None: if not await functions.can_manage_role(bot, role): raise discord.InvalidArgument("I can't manage that role, " "probably because that role " "is higher than my top role.") limit = await functions.get_limit(bot, 'xproles', role.guild.id) if limit is False: raise errors.NoPremiumError("Non-premium guilds do not have " "access to XP Roles. See the last " "page of `sb!tutorial` for more info.") conn = bot.db.conn async with bot.db.lock: async with conn.transaction(): current_num = await conn.fetchval( """SELECT COUNT(*) FROM xproles WHERE guild_id=$1""", role.guild.id) if current_num + 1 > limit: raise errors.NoPremiumError( "You have reached your limit for XP Roles.") if await is_pr(bot, role.id): raise discord.InvalidArgument("A role cannot be both a Position Role " "and an XP Role.") if not 0 <= req_xp < 100000: raise discord.errors.InvalidArgument( "Required XP must be greater than or " "equal to 0 and less than 100,000") create_xp_role = \ """INSERT INTO xproles (id, guild_id, req_xp) VALUES ($1, $2, $3)""" async with bot.db.lock: async with conn.transaction(): await conn.execute(create_xp_role, role.id, role.guild.id, req_xp)
async def add_pos_role(bot: commands.Bot, role: discord.Role, max_users: int) -> None: add_role = \ """INSERT INTO posroles (id, guild_id, max_users) VALUES ($1, $2, $3)""" if not await functions.can_manage_role(bot, role): raise discord.InvalidArgument("I can't manage that role, probably " "because it is above my highest role.") limit = await functions.get_limit(bot, 'posroles', role.guild.id) if limit is False: raise errors.NoPremiumError( "Non-premium guilds do not have access " "to position-based Role Awards. See the " "last page of `sb!tutorial` for more info.") conn = bot.db.conn async with bot.db.lock: async with conn.transaction(): current_num = await conn.fetchval( """SELECT COUNT (*) FROM posroles WHERE guild_id=$1""", role.guild.id) if current_num + 1 > limit: raise errors.NoPremiumError( "You have reached your limit for Position Roles") if await is_xpr(bot, role.id): raise discord.InvalidArgument("A role cannot be both an XP Role and " "a Position Role.") async with bot.db.lock: async with conn.transaction(): await conn.execute(add_role, role.id, role.guild.id, max_users)
async def predicate(ctx): endsat = await functions.get_prem_endsat(ctx.bot, ctx.guild.id) if endsat is None: raise errors.NoPremiumError( "Only premium guilds can run this command.") return True
async def add_aschannel( bot: commands.Bot, channel: discord.TextChannel ) -> None: check_aschannel = \ """SELECT * FROM aschannels WHERE id=$1""" check_starboard = \ """SELECT * FROM starboards WHERE id=$1""" get_aschannels = \ """SELECT * FROM aschannels WHERE guild_id=$1""" # just in case we messed something up earlier and it's already in there if channel.id not in bot.db.as_cache: bot.db.as_cache.add(channel.id) guild = channel.guild perms = channel.permissions_for(guild.me) limit = await functions.get_limit( bot, 'aschannels', guild.id ) conn = bot.db.conn if not perms.read_messages: raise errors.BotNeedsPerms( "I need the 'READ MESSAGES' permission in that channel." ) elif not perms.manage_messages: raise errors.BotNeedsPerms( "I need the `MANAGE MESSAGES` permission in that channel." ) elif not perms.add_reactions: raise errors.BotNeedsPerms( "I need the `ADD REACTIONS` permission in that channel." ) await functions.check_or_create_existence( bot, guild_id=guild.id ) async with bot.db.lock: async with conn.transaction(): all_aschannels = await conn.fetch( get_aschannels, guild.id ) if len(all_aschannels) >= limit: raise errors.NoPremiumError( "You have reached your limit for AutoStar Channels" " in this server.\nSee the last page of `sb!tutorial` " "for more info." ) sql_aschannel = await conn.fetchrow( check_aschannel, channel.id ) if sql_aschannel is not None: raise errors.AlreadyExists( "That is already an AutoStar Channel!" ) sql_starboard = await conn.fetchrow( check_starboard, channel.id ) if sql_starboard is not None: raise errors.AlreadyExists( "That channel is already a starboard!\n" "A channel can't be both a starboard and an " "AutoStar channel." ) await bot.db.q.create_aschannel.fetch( channel.id, guild.id )