summaryrefslogtreecommitdiff
path: root/utils.py
blob: 56018c58c16cf45c91817a8e4a29f85433bd9c68 (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
import os

import psycopg2
from psycopg2 import extras
import discord
import json
import requests


async def get_colour(colour):
    colours = {"default": 0,
               "teal": 0x1abc9c,
               "dark teal": 0x11806a,
               "green": 0x2ecc71,
               "dark green": 0x1f8b4c,
               "blue": 0x3498db,
               "dark blue": 0x206694,
               "purple": 0x9b59b6,
               "dark purple": 0x71368a,
               "magenta": 0xe91e63,
               "dark magenta": 0xad1457,
               "gold": 0xf1c40f,
               "dark gold": 0xc27c0e,
               "orange": 0xe67e22,
               "dark orange": 0xa84300,
               "red": 0xe74c3c,
               "dark red": 0x992d22,
               "lighter grey": 0x95a5a6,
               "dark grey": 0x607d8b,
               "light grey": 0x979c9f,
               "darker grey": 0x546e7a,
               "blurple": 0x7289da,
               "greyple": 0x99aab5,
               "black": 0x000000}
    return colours[colour]


async def sql(sql: str, params: tuple):
    con = psycopg2.connect(database=os.environ.get('db_name'),
                           user=os.environ.get('db_user'),
                           password=os.environ.get('db_pass'),
                           host=os.environ.get('db_host'),
                           port=os.environ.get('db_port'),
                           options=f'-c search_path={os.environ.get("db_schema")}')
    cur = con.cursor(cursor_factory=extras.DictCursor)

    cur.execute(sql, params)
    con.commit()

    sql_out = []
    if sql.startswith("SELECT"):
        columns = [str(column[0]).lower() for column in cur.description]
        rows = cur.fetchall()
        for row in rows:
            sql_row = dict(zip(columns, row))
            sql_out.append(sql_row)

    print(f"SQL Out: {sql_out}")

    cur.close()
    con.close()
    return sql_out


async def notice(title, message, colour="blue", footer=None):
    embed_colour = await get_colour(colour)
    emb = discord.Embed(title=title,
                        description=message,
                        colour=embed_colour)
    if footer:
        emb.set_footer(text=footer)

    return emb


async def embed(ctx, title, message, thumbnail=None, colour="blue", footer="", url="", image=None):
    embed_colour = await get_colour(colour)
    emb = discord.Embed(title=title, description=message, url=url, colour=embed_colour)
    emb.set_footer(text=f"Requested by {ctx.author}. {footer}", icon_url=ctx.author.display_avatar)

    if image:
        emb.set_image(url=image)

    if thumbnail:
        emb.set_thumbnail(url=thumbnail)

    return emb


async def author(emb, name="", url=""):
    emb.set_author(name=name, icon_url=url)
    return emb


async def footer(emb, text, url):
    emb.set_footer(text=text, icon_url=url)
    return emb


async def field(emb, name, value, inline=False):
    emb.add_field(name=name, value=value, inline=inline)
    return emb


async def log(guild_id, bot, msg=None, emb=None):
    guild = bot.get_guild(guild_id)
    guild_channels = guild.channels
    # print(guild.channels)
    for channel in guild_channels:
        try:
            if str(channel.type) == "text" and channel.name.lower() == "synthy-log":
                if msg is not None and emb is None:
                    await channel.send(content=msg)
                elif msg is None and emb is not None:
                    await channel.send(embed=emb)
                elif msg is not None and emb is not None:
                    await channel.send(content=msg, embed=emb)
        except:
            print('Cannot write to log channel')


async def get_request(url):
    obj_request_get = requests.get(url)
    if obj_request_get.status_code == 200:
        obj_data = json.loads(obj_request_get.text)
        return obj_data
    else:
        return None
# async def debuglog(username, module, log_msg):
#     config = configparser.ConfigParser()
#     with open(f"./cogs/cogs_{username}.json") as fp:
#         config = json.load(fp)
#         if module in config["debug"]["active"]:
#             sql = await sql("""INSERT INTO `logs` (`module`, `description`) VALUES ("%s", "%s")""", (module, log_msg))