-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
135 lines (116 loc) · 6.19 KB
/
main.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
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
# props to proxy and his way to connect to database https://github.com/artem30801/SkyboxBot/blob/master/main.py
import os
import asyncio
from typing import Optional
import motor
from beanie import Indexed, init_beanie
from naff import Client, Intents, listen, Embed, InteractionContext, AutoDefer
from utils.customchecks import *
from extentions.touk import BeanieDocuments as db, violation_settings
from naff.client.errors import NotFound
from naff.api.events.discord import GuildLeft
# import logging
# import naff
# logging.basicConfig()
# cls_log = logging.getLogger(naff.const.logger_name)
# cls_log.setLevel(logging.DEBUG)
intents = Intents.ALL
ad = AutoDefer(enabled=True, time_until_defer=1)
class CustomClient(Client):
def __init__(self):
super().__init__(
intents=intents,
sync_interactions=True,
delete_unused_application_cmds=True,
default_prefix='+',
fetch_members=True,
auto_defer=ad,
# asyncio_debug=True
)
self.db: Optional[motor.motor_asyncio.AsyncIOMotorClient] = None
self.models = list()
async def startup(self):
for filename in os.listdir('./extentions'):
if filename.endswith('.py') and not filename.startswith('--'):
self.load_extension(f'extentions.{filename[:-3]}')
print(f'grew {filename[:-3]}')
self.db = motor.motor_asyncio.AsyncIOMotorClient(os.environ['pt_mongo_url'])
await init_beanie(database=self.db.giffany, document_models=self.models)
await self.astart(os.environ['tyrone_token'])
@listen()
async def on_ready(self):
print(f"[Logged in]: {self.user}")
guild = self.get_guild(435038183231848449)
channel = guild.get_channel(932661537729024132)
await channel.send(f'[Logged in]: {self.user}')
@listen()
async def on_guild_join(self, event):
#add guild to database
if await db.prefixes.find_one({'guildid':event.guild.id}) is None:
await db.prefixes(guildid=event.guild.id, prefix='p.').insert()
guild = self.get_guild(435038183231848449)
channel = guild.get_channel(932661537729024132)
await channel.send(f'I was added to {event.guild.name}|{event.guild.id}')
if await db.automod_config.find_one({'guildid':event.guild.id}) is None:
violations = violation_settings(violation_count=None, violation_punishment=None)
await db.automod_config(guildid=event.guild.id, banned_words=violations, phishing=violations).insert()
@listen()
async def on_guild_leave(self, event: GuildLeft):
for document in self.models:
async for entry in document.find({'guildid': event.guild_id}):
await entry.delete()
async for entry in document.find({'guild_id': event.guild_id}):
await entry.delete()
print(f'Guild {event.guild_id} was removed.')
async def on_command_error(self, ctx: InteractionContext, error:Exception):
if isinstance(error, MissingPermissions):
embed = Embed(description=f":x: {ctx.author.mention} You don't have permissions to perform that action",
color=0xdd2e44)
await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, MissingRole):
regx = {'$regex':f"^{re.escape(ctx.invoked_name)}$", '$options':'i'}
roleid = await db.hasrole.find_one({"guildid":ctx.guild.id, "command":regx})
if roleid is not None:
role = ctx.guild.get_role(roleid.role)
embed = Embed(description=f":x: {ctx.author.mention} You don't have role {role.mention} that's required to use this command.",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, RoleNotFound):
embed = Embed(description=f":x: Couldn't find that role",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, UserNotFound):
embed = Embed(description=f":x: User is not a member of this server ",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, CommandOnCooldown):
embed = Embed(
description=f":x: Command **{ctx.invoked_name}** on cooldown, try again later.",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, ExtensionNotActivatedInGuild):
embed = Embed(description=f":x: Module for this command is not activated in the server.",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, CommandNotActivatedInGuild):
embed = Embed(description=f":x: Command is not activated in the server.",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
if isinstance(error, UserInBlacklist):
embed = Embed(description=f":x: {ctx.author.mention} You are not allowed to use this command",
color=0xDD2222)
return await ctx.send(embed=embed, ephemeral=True)
else:
embed = Embed(description=f":x: An error occured while trying to execute `{ctx.invoked_name}` command: ```{error}```",
color=0xDD2222)
await ctx.send(embed=embed, ephemeral=True)
if ctx.guild_id != 435038183231848449:
guild = self.get_guild(435038183231848449)
channel = guild.get_channel(932661537729024132)
invite = await ctx.channel.create_invite(reason=f'[AUTOMOD]invite created due to error occuring')
await channel.send(f"<@400713431423909889> An error occured while {ctx.author}({ctx.author.id}) tryied to execute `{ctx.invoked_name}` command in {ctx.channel.name} from `{ctx.guild.name}`: ```{error}```\n{invite}")
def add_model(self, model):
self.models.append(model)
if __name__ == "__main__":
bot = CustomClient()
asyncio.run(bot.startup())