This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Accessory.py
93 lines (72 loc) · 2.57 KB
/
Accessory.py
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
import discord.errors
import config
bot = config.bot
# rename "prefixes" table to "accessories"
class Accessory:
def __init__(self, guild_id, prefixes):
self.guild_id = guild_id
self.prefixes = prefixes
@staticmethod
async def open(guild):
async with config.connection.transaction():
await config.connection.execute(
"INSERT INTO accessories VALUES ($1, $2);",
guild.id,
["$"] # default prefix
)
@staticmethod
async def get(guild):
async with config.connection.transaction():
values = tuple(await config.connection.fetchrow(
"SELECT * FROM accessories WHERE guild_id = $1;",
guild.id
))
if not values:
await Accessory.open(guild)
async with config.connection.transaction():
values = tuple(await config.connection.fetchrow(
"SELECT * FROM accessories WHERE guild_id = $1;",
guild.id
))
return Accessory(*values)
async def store(self):
async with config.connection.transaction():
await config.connection.execute(
"UPDATE accessories "
"SET prefixes = $2 "
"WHERE guild_id = $1;",
self.guild_id,
self.prefixes
)
@staticmethod
async def set_prefixes(guild, prefixes_list=None):
if not prefixes_list:
# sets default prefix ($)
prefixes_list = ["$"]
accessory = await Accessory.get(guild)
accessory.prefixes = prefixes_list
bot_user = bot.user
bot_member = guild.get_member(bot.user.id)
try:
await bot_member.edit(nick=f"{bot_user.name} | {prefixes_list[0]}")
except discord.errors.Forbidden:
pass
await accessory.store()
@staticmethod
async def remove(guild):
async with config.connection.transaction():
await config.connection.execute(
"DELETE FROM accessories WHERE guild_id = $1;",
guild.id
)
@staticmethod
async def ensure_integrity():
guilds = bot.guilds
for guild in guilds:
async with config.connection.transaction():
record = await config.connection.fetchrow(
"SELECT * FROM accessories WHERE guild_id = $1;",
guild.id
)
if not record:
await Accessory.set_prefixes(guild)