GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( ba71d1...e367f3 )
by Git
01:41 queued 11s
created

owner.Owner.get_guilds()   A

Complexity

Conditions 5

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nop 4
dl 0
loc 14
rs 9.3333
c 0
b 0
f 0
1
import discord
2
from aiohttp import request
3
from discord.ext import commands
4
import database as db
5
from functions import update_db
6
import io
7
import json
8
import contextlib
9
import textwrap
10
11
12
class Owner(commands.Cog):
13
    """A private cog which only works for me."""
14
15
    def __init__(self, bot):
16
        self.bot = bot
17
18
 
19
    def cog_check(self, ctx):
20
        return ctx.author.id == 791950104680071188
21
22
    @commands.command(aliases=["eval"])
23
    async def e(self, ctx, *, code: str = None):
24
        if code is None:
25
            return await ctx.send(
26
                "Define the code too, what is supposed to execute?"
27
            )
28
29
        code = code.lstrip("```python").rstrip("\n```").lstrip("\n")
30
31
        local_vars = {
32
            "discord": discord,
33
            "commands": commands,
34
            "bot": self.bot,
35
            "ctx": ctx,
36
        }
37
        stdout = io.StringIO()
38
39
        try:
40
            with contextlib.redirect_stdout(stdout):
41
                exec(
42
                    f"async def func():\n{textwrap.indent(code, '    ')}",
43
                    local_vars
44
                )
45
46
                obj = await local_vars["func"]()
47
                result = f"{stdout.getvalue()}"
48
49
        except Exception as e:
50
            result = e
51
52
        if len(str(result)) >= 2000:
53
            result = result[:1900]
54
            await ctx.send(
55
                "Result larger than 2000 characters, "
56
                "returned 1900 characters only."
57
            )
58
59
        await ctx.send(f"```python\n{result}```")
60
61
    @commands.command()
62
    async def get_guilds(self, ctx, *, user: discord.User = None):
63
        if user is None:
64
            return await ctx.send(
65
                "You need to define the user to find in which guilds they are!"
66
            )
67
68
        data = {}
69
        for guild in self.bot.guilds:
70
            for member in guild.members:
71
                if member == user:
72
                    data.update({guild.name: guild.id})
73
        await ctx.send(
74
            f"**{user}** found in __{len(data)}__ guilds\n```json\n{data}```"
75
        )
76
77
    @commands.command()
78
    async def get_members(self, ctx, *, guild: discord.Guild = None):
79
        if guild is None:
80
            return await ctx.send("You need to define the guild too")
81
82
        members = ""
83
        for member in guild.members:
84
            members += f"`{member}` - "
85
            if len(members) > 1500:
86
                members += "**\nMessage was too long so this is not complete**"
87
                break
88
89
        await ctx.send(members)
90
91
    @commands.command()
92
    async def get_doc(self, ctx, doc_name=None, *, guild: discord.Guild = None):
93
        if doc_name is None or guild is None:
94
            return await ctx.send(
95
                "You need to define both document name and guild name/id"
96
            )
97
98
        try:
99
            plugin_db = getattr(db, doc_name.upper())
100
101
        except Exception:
102
            return await ctx.send(
103
                f"No document with name **{doc_name.upper()}**"
104
            )
105
106
        doc = await plugin_db.find_one({"_id": guild.id})
107
108
        await ctx.send(
109
            f"**{doc_name.upper()}** Document for **{guild.name}**\n"
110
            f"```json\n{doc}```"
111
        )
112
113
    @commands.command()
114
    async def backup_db(self, ctx):
115
        headers = {
116
            "X-Master-Key": "$2b$10$sHW.6D.jlcsj.XuCzJcytOdqPpcZQKNhVZaOgJhEGia1P5ZlCGEUq", 
117
            "Content-Type": "application/json"
118
                }
119
        count = 0
120
        # Just plugin document for now
121
        async for i in db.PLUGINS.find({}):
122
            async with request(
123
                    "POST",
124
                    "https://api.jsonbin.io/v3/b",
125
                    data=json.dumps(i),
126
                    headers=headers
127
                    ):
128
                count += 1
129
        
130
        await ctx.send(f"Backed up {count} plugin documents.")
131
132
133
    @commands.command()
134
    async def update_db(self, ctx):
135
        await update_db([guild.id for guild in self.bot.guilds])
136
137
    @commands.command()
138
    async def clean_db(self, ctx):
139
        guild_ids = [guild.id for guild in self.bot.guilds]
140
141
        async for i in db.AUTO_MOD.find({}):
142
            if i["_id"] not in guild_ids:
143
                await db.AUTO_MOD.delete_one(i)
144
                print("AUTO_MOD", i["_id"])
145
146
        print("\n")
147
        async for i in db.CHATBOT.find({}):
148
            if i["_id"] not in guild_ids:
149
                await db.CHATBOT.delete_one(i)
150
                print("CHATBOT", i["_id"])
151
152
        print("\n")
153
        async for i in db.PERMISSIONS.find({}):
154
            if i["_id"] not in guild_ids:
155
                await db.PERMISSIONS.delete_one(i)
156
                print("PERMISSIONS", i["_id"])
157
158
        print("\n")
159
        async for i in db.PLUGINS.find({}):
160
            if i["_id"] not in guild_ids:
161
                await db.PLUGINS.delete_one(i)
162
                print("PLUGINS", i["_id"])
163
164
        print("\n")
165
        async for i in db.PREFIXES.find({}):
166
            if i["_id"] not in guild_ids:
167
                await db.PREFIXES.delete_one(i)
168
                print("PREFIXES", i["_id"])
169
170
        print("\n")
171
        async for i in db.REACTION_ROLES.find({}):
172
            if i["_id"] not in guild_ids:
173
                await db.REACTION_ROLES.delete_one(i)
174
                print("REACTION_ROLES", i["_id"])
175
176
        print("\n")
177
        async for i in db.VERIFY.find({}):
178
            if i["_id"] not in guild_ids:
179
                await db.VERIFY.delete_one(i)
180
                print("VERIFY", i["_id"])
181
182
        print("\n")
183
        async for i in db.WELCOME.find({}):
184
            if i["_id"] not in guild_ids:
185
                await db.WELCOME.delete_one(i)
186
                print("WELCOME", i["_id"])
187
188
189
def setup(bot):
190
    bot.add_cog(Owner(bot))
191