-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
198 lines (148 loc) · 6.19 KB
/
Copy pathapp.py
File metadata and controls
198 lines (148 loc) · 6.19 KB
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
import nest_asyncio
nest_asyncio.apply()
from discord.ext import commands
import discord
import json
import asyncio
from lib.commands import cmds
from lib.triggers import tr
import lib.functions as fn
import lib.commands as cm
import lib.database as db
import lib.cache as cache
class Bot(commands.AutoShardedBot):
def __init__(self):
print(" > Preparing the bot")
with open("settings.json", "r") as file:
self.settings = fn.Classify(json.load(file))
self.devs = self.settings.devs
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
self.shards_ready = 0
self.bot_ready = False
self.prepping = False
super().__init__(command_prefix=fn.getprefix, intents=intents,
shard_count=self.settings.shards)
self.ready_shards_list = [False] * self.shard_count
self.ftime = fn.ftime()
# fn.VotingHandler(self)
self.add_check(self.predicate)
db.setup()
print("\n > Loaded the database")
for csv in [tr, cmds]:
csv.load()
def ready(self):
return self.bot_ready
def are_shards_ready(self):
if all(self.ready_shards_list):
self.shards_ready = self.shard_count
return True
return False
async def prep(self):
self.prepping = True
print(f"\n > All shards ready, finishing preperations")
self.remove_command("help")
for cog in fn.getcogs():
if cog not in ["botlists.py"]:
print(f"\nLoading {cog}...", end="")
try: await self.reload_extension("cogs." + cog[:-3])
except: await self.load_extension("cogs." + cog[:-3])
finally: print("Done", end="")
print("\n\n > Loaded cogs\n")
await db.checkguilds(self.guilds)
self.premium = await self.get_premium()
self.cache = cache.Cache(self.settings.devs, self.premium)
for command in cm.commands:
self.cache.cooldowns.add(command, tuple(cm.commands[command][3:5]))
for command in cm.devcmds:
self.cache.cooldowns.add(command, (0, 0))
print(" > Finished setting up cooldowns")
await self.change_presence(status=discord.Status.online,
activity=discord.Game(name="'FBot help'"))
self.ftime.set()
print(f" > Session started at {self.ftime.start}\n")
self.bot_ready = True
self.prepping = False
print(f" > Bot is ready")
self.dispatch("bot_ready")
async def on_shard_connect(self, shard_id):
if shard_id == 0:
print(f" > Shard 0 CONNECTED, updating status to 'Loading up...'")
await self.change_presence(status=discord.Status.online,
activity=discord.Game(name="Loading up..."))
async def on_shard_ready(self, shard_id):
self.shards_ready += 1
print(f" > Shard {shard_id} READY, {self.shards_ready}/{self.shard_count} online")
self.ready_shards_list[shard_id] = True
if self.are_shards_ready() and not self.prepping:
asyncio.create_task(self.prep())
async def on_shard_resumed(self, shard_id):
self.shards_ready += 1
print(f" > Shard {shard_id} RESUMED, {self.shards_ready}/{self.shard_count} online")
self.ready_shards_list[shard_id] = True
if self.are_shards_ready() and not self.prepping and not self.bot_ready:
asyncio.create_task(self.prep())
async def on_shard_disconnect(self, shard_id):
self.shards_ready -= 1
self.bot_ready = False
print(f" > Shard {shard_id} DISCONNECTED, {self.shards_ready}/{self.shard_count} online")
self.ready_shards_list[shard_id] = False
async def get_premium(self):
guild = self.get_guild(self.settings.server)
role = guild.get_role(self.settings.roles.premium)
premium = set()
for member in role.members:
premium.add(member.id)
return premium
#async def on_member_update(self, before, after):
# if before.roles == after.roles:
# return
# for role in after.roles:
# if role.id == self.settings.roles.premium:
# self.premium.add(after.id)
# return
# self.premium.remove(after.id)
def get_colour(self, user_id):
if user_id in self.premium:
pass
return 0xf42f42
def embed(self, user, title, *desc, url=""):
colour = self.get_colour(user.id)
desc = "\n".join(desc)
return discord.Embed(title=title, description=desc, colour=colour, url=url)
def predicate(self, ctx):
user = ctx.author.id
command = ctx.command.name
if not self.ready():
return
if command in cm.devcmds:
if user not in self.devs:
raise commands.NotOwner()
if str(ctx.channel.type) != "private":
bot_perms = ctx.channel.permissions_for(ctx.guild.get_member(self.user.id))
valid, perms = [], {}
for perm in cm.perms[command]:
if not perm.startswith("("):
bot_perm = getattr(bot_perms, perm)
else: bot_perm = True
valid.append(bot_perm)
perms[perm] = bot_perm
if not all(valid):
page = "**Missing Permissions**\n\n"
for perm in perms:
if perm.startswith("("):
perms[perm] = getattr(bot_perms, perm[1:-1])
page += f"{fn.emojis[perms[perm]]} ~ {fn.formatperm(perm)}\n"
raise commands.CheckFailure(message=page)
else:
if cm.commands[command][5] == "*Yes*":
raise commands.NoPrivateMessage()
cooldown = self.cache.cooldowns.get(user, command)
if cooldown:
self.stats.commands_ratelimited += 1
raise commands.CommandOnCooldown(commands.Cooldown(1, cooldown), cooldown, commands.BucketType.user)
self.stats.commands_processed += 1
return True
bot = Bot()
bot.run(bot.settings.tokens.bot)#, log_level=50)