summaryrefslogtreecommitdiff
path: root/pindelegate/pindelegate.py
blob: 2b94bf42b957da44ab78fdc6d5e86838345469b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import discord
from redbot.core import Config
from redbot.core import checks
from redbot.core import commands


class PinDelegate(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.config = Config.get_conf(
            self, identifier="551742410770612234|6772870d-1739-4ada-a2c5-1821b4f3a618"
        )
        self.config.register_channel(pin_capable_members={})

    @commands.command()
    @checks.admin_or_permissions(administrator=True)
    async def pindelegate(self, ctx, user: discord.Member):
        async with self.config.channel(
            ctx.channel
        ).pin_capable_members() as pin_capable_members:
            pin_capable_members[user.id] = True
        await ctx.reply(
            f"User {user.name} ({user.id}) is now pin-capable in this channel."
        )

    @commands.command()
    @checks.admin_or_permissions(administrator=True)
    async def pinundelegate(self, ctx, user: discord.Member):
        async with self.config.channel(
            ctx.channel
        ).pin_capable_members() as pin_capable_members:
            try:
                del pin_capable_members[str(user.id)]
            except KeyError:
                await ctx.reply(
                    f"User {user.name} ({user.id}) was already not pin-capable in this channel."
                )
                return
        await ctx.reply(
            f"User {user.name} ({user.id}) removed from pin-capable users in this channel."
        )

    async def is_pin_capable(self, channel, member_id):
        try:
            await self.config.channel(channel).pin_capable_members.get_raw(
                str(member_id)
            )
        except KeyError:
            return False
        return True

    @commands.command()
    async def pin(self, ctx):
        if await self.is_pin_capable(ctx.channel, ctx.author.id):
            await ctx.message.reference.resolved.pin()

    @commands.command()
    async def unpin(self, ctx):
        replied_to_message = ctx.message.reference.resolved
        if await self.is_pin_capable(ctx.channel, ctx.author.id):
            if replied_to_message.pinned:
                await replied_to_message.unpin()
                await ctx.reply("Unpinned message!")
            else:
                await ctx.reply("That message was already not pinned.")