summaryrefslogtreecommitdiff
path: root/main.py
blob: 942c774dfd6f377ebbcf1c09aad848c0347835e5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from functools import wraps
from typing import List

import discord.utils
from discord import Object
from discord.ext import commands
import discord

bot = commands.Bot(
    command_prefix='~',
    self_bot=True,
)

banned_guilds = [
    365116470339960832,
    375597071094382603,
]


def check_guild(func):
    @wraps(func)
    async def wrapper(ctx: commands.Context, *args, **kwargs):
        if ctx.guild.id in banned_guilds:
            return
        return await func(ctx, *args, **kwargs)

    return wrapper


async def _context_react(self: commands.Context, emoji):
    await self.message.add_reaction(emoji)


commands.Context.react = _context_react


def dump_perms(permissions: discord.Permissions):
    def perm_names():
        for perm, value in permissions:
            if value:
                yield perm

    return ', '.join(perm_names())


@bot.event
async def on_ready():
    print('Logged in as %s#%s' % (bot.user.name, bot.user.discriminator))


def get_with_attr(arr, **attributes):
    def _get_with_attr():
        for el in arr:
            for key, val in attributes.items():
                if isinstance(val, str):
                    if str(getattr(el, key)).lower() != val.lower():
                        break
                else:
                    if type(val)(getattr(el, key)) != val:
                        break
            else:
                yield el

    return list(_get_with_attr())


@bot.command(name="channel", pass_context=True)
@check_guild
async def channel_cmd(ctx: commands.Context, channel: discord.TextChannel = None):
    if channel is None:
        channel = ctx.channel
    category: discord.CategoryChannel = channel.category

    embed = discord.Embed(
        title='Channel: %s' % channel.name,
        description=channel.topic,
        color=discord.Color.blurple())

    embed.add_field(name="Created at", value=channel.created_at)
    embed.add_field(name="Id", value=channel.id)
    embed.add_field(name="Category", value="%s (%s)" % (category.name, category.id))
    embed.add_field(name="Position", value=channel.position)
    embed.add_field(name="Changed roles", value=', '.join(role.mention for role in channel.changed_roles))

    await ctx.send(embed=embed)
    await ctx.react('✅')


@bot.command(name="user", pass_context=True)
@check_guild
async def user_cmd(ctx: commands.Context, identifier):
    import re
    identifier = str(identifier)
    if re.match(r'^<@!?[0-9]+>$', identifier):
        identifier = re.sub('[<@!>]+', '', identifier)

    identifier = int(identifier)
    profile: discord.Profile = await bot.get_user_profile(identifier)
    discord_profile: discord.User = bot.get_user(identifier)
    em = discord.Embed(
        title='%s#%s' % (discord_profile.name, discord_profile.discriminator),
    )
    desc = ""
    if profile.staff:
        desc += "Bow in awe of the mighty staff!\n"
    if profile.hypesquad:
        desc += "They got some hype\n"
    if profile.nitro:
        desc += "I can haz animated emotez since %s\n" % profile.premium_since
    if profile.partner:
        desc += "Discord Partner. woa\n"
    desc += '\n'
    em.description = desc

    guild_str = ""
    for guild in profile.mutual_guilds:
        guild_str += guild.name + "\n"
    em.add_field(name='Mutual guilds', value=guild_str, inline=True)

    for account in profile.connected_accounts:
        em.add_field(name=account['type'], value=('☑' if account['verified'] else '') + account['name'])

    em.set_thumbnail(url=discord_profile.avatar_url)
    em.add_field(name='Joined Discord', value=discord_profile.created_at, inline=True)
    guild: discord.Guild = ctx.guild
    if guild.get_member(identifier) is not None:
        member: discord.Member = guild.get_member(identifier)
        em.colour = member.color
        em.add_field(name="Permissions", value=dump_perms(member.guild_permissions), inline=True)
        em.add_field(name="Joined guild", value=member.joined_at)

    await ctx.send(embed=em)
    await ctx.react('✅')


@bot.command(pass_context=True, name="role")
@check_guild
async def role_cmd(ctx: commands.Context):
    txt = ctx.message.content
    role = txt[txt.find(' ') + 1:]
    role_obj = get_with_attr(ctx.guild.roles, name=role) \
               + get_with_attr(ctx.guild.roles, id=role)
    if len(role_obj) == 0:
        await ctx.react('❌')
        return
    await dump_roles(ctx, role_obj, False)
    await ctx.react('✅')


@bot.command(pass_context=True)
@check_guild
async def raw(ctx: commands.Context, id, quiet: bool = True):
    channel: discord.TextChannel = ctx.channel
    async for mes in channel.history(limit=2, around=Object(id=id)):
        if str(mes.id) == id:
            escaped = mes.content.replace('```', '``\u200B`')
            if quiet:
                print(mes.content)
            else:
                await ctx.send("```\n%s\n```" % escaped)
            await ctx.react('✅')
            return
    await ctx.react('❌')


async def dump_roles(ctx: commands.Context, roles: List[discord.Role], quiet: bool):
    def description(role: discord.Role):
        return """
        Name: `%s`
        Id: %s
        Color: %s
        Permissions: %s
        """ % (
            role.name,
            role.id,
            str(role.color),
            dump_perms(role.permissions)
        )

    tmp = ""
    texts = [description(role) for role in roles]
    if quiet:
        print('\n'.join(texts))
        return
    for text in texts:
        if len(text) + len(tmp) + len('\n') < 2000:
            tmp += text + '\n'
        else:
            await ctx.send(tmp)
            tmp = ""
    if len(tmp) != 0:
        await ctx.send(tmp)


@bot.command(pass_context=True, name="roles")
@check_guild
async def roles_cmd(ctx: commands.Context, quiet: bool = False):
    guild: discord.Guild = ctx.guild
    roles: List[discord.Role] = guild.roles

    await dump_roles(ctx, roles, quiet)
    await ctx.react('✅')


if __name__ == '__main__':
    with open('token.txt') as f:
        bot.run(f.read().strip(), bot=False)