async def toggle(self, ctx: commands.Context, toggle: str): guild = session.query(Guild).filter( Guild.guild_id == ctx.guild.id).first() if not guild: guild = Guild(guild_id=ctx.guild.id) session.add(guild) session.commit() if toggle == "username": guild.set_username = not guild.set_username session.commit() if guild.set_username: for member in ctx.guild.members: if discord_user := session.query(DiscordUser).filter( DiscordUser.user_id == member.id).first(): await self.bot.update_guild_user(guild, discord_user) await ctx.send( f"<{EMOJIS['CHECK']}> Reddit usernames enabled for this server!" ) else: await ctx.send( f"<{EMOJIS['CHECK']}> Reddit usernames disabled for this server!" )
async def update_guild_user(self, guild: Guild, discord_user: DiscordUser): g = bot.get_guild(guild.guild_id) if g is None: return m = g.get_member(discord_user.user_id) if m is None: return verified_accounts = [acc for acc in discord_user.reddit_accounts if acc.verified] if guild.set_role: role = g.get_role(guild.role) if not role: role = await g.create_role(name="Verified Redditor", colour=discord.Colour(0).from_rgb(254, 63, 24), mentionable=True, reason="Verified Redditors get this role by the bot.") guild.role = role.id session.commit() if verified_accounts: await m.add_roles(role) else: await m.remove_roles(role) if guild.set_username: for account in verified_accounts: try: await m.edit(nick=f"/u/{account.username}") except discord.errors.Forbidden: await self.send_error(f"❗ I don't have the necessary permissions to change {m.mention}'s nickname.") finally: break
async def nick(self, ctx: commands.Context, *, new_nick: str = ""): guild = session.query(Guild).filter( Guild.guild_id == ctx.guild.id).first() if not guild: guild = Guild(guild_id=ctx.guild.id) session.add(guild) session.commit() if not guild.custom_nick: await ctx.send( f"<{EMOJIS['XMARK']}> This server has disabled custom nicknames!" ) return discord_user = session.query(DiscordUser).filter( DiscordUser.user_id == ctx.author.id).first() verified = [acc.username for acc in discord_user.reddit_accounts] if new_nick != "": end = f"({'unverified' if len(verified) <= 0 else '/u/' + verified[0]})" new_nick = f"{new_nick[:32 - len(end) - 1]} {end}" else: new_nick = f"/u/{verified[0]}" if len( verified) > 0 else ctx.author.username try: await ctx.author.edit(nick=new_nick) except discord.errors.Forbidden: await ctx.send( f"<{EMOJIS['XMARK']}> I don't have the permissions to edit your nickname!" )
async def on_guild_join(self, g: discord.Guild): guild = session.query(Guild).filter(Guild.guild_id == g.id).first() if not guild: guild = Guild(guild_id=g.id) session.add(guild) session.commit()
def get_db_guild(self, guild_id: int) -> Guild: guild = session.query(Guild).filter(Guild.guild_id == guild_id).first() if not guild: categories = session.query(Category).filter( Category.default == true()).all() guild = Guild(guild_id=guild_id, categories=categories) session.add(guild) session.commit() return guild
async def setguildsub(self, ctx: commands.Context, sub: str): guild = session.query(Guild).filter( Guild.guild_id == ctx.guild.id).first() if not guild: guild = Guild(guild_id=ctx.guild.id) session.add(guild) session.commit() try: s = await self.bot.reddit.subreddit(sub) except BaseException: await ctx.send( "❗ /r/{} doesn't exist or isn't visible to me!".format(sub)) else: guild.subreddit = sub session.commit() await ctx.send( f"<{EMOJIS['CHECK']}> Successfully set /r/{sub} as this guild's subreddit!" )
async def unverify(self, ctx: commands.Context, user: str): if reddit_user := session.query(RedditUser).filter( and_( func.lower(RedditUser.username) == user, RedditUser.discord_user == ctx.author.id)).first(): session.delete(reddit_user) session.commit() guild = session.query(Guild).filter( Guild.guild_id == ctx.guild.id).first() if not guild: guild = Guild(guild_id=ctx.guild.id) session.add(guild) session.commit() if discord_user := session.query(DiscordUser).filter( DiscordUser.user_id == ctx.author.id).first(): await self.bot.update_guild_user(guild, discord_user)
async def status(self, ctx: commands.Context): guild = session.query(Guild).filter( Guild.guild_id == ctx.guild.id).first() if not guild: guild = Guild(guild_id=ctx.guild.id) session.add(guild) session.commit() set_role = "on" if guild.set_role else "off" set_username = "******" if guild.set_username else "off" set_nickname = "on" if guild.custom_nick else "off" role_name = ctx.guild.get_role(guild.role).name if guild.role else None await ctx.channel.send( f"Adding '{role_name}' role: {set_role}\n" + f"Changing nickname to /u/username: {set_username}\n" + f"Allowing custom nicknames: {set_nickname}")