forked from DiscordGIR/GIRRewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (128 loc) · 5.88 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
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
import asyncio
import os
import traceback
import discord
from discord.ext import commands
from discord import app_commands
from discord.app_commands import AppCommandError, Command, ContextMenu, CommandInvokeError, TransformerError
from extensions import initial_extensions
from utils import cfg, db, logger, GIRContext, BanCache, IssueCache, Tasks, RuleCache, init_client_session, scam_cache
from utils.framework import PermissionsFailure, gatekeeper, find_triggered_filters
from cogs.commands.context_commands import setup_context_commands
from typing import Union
from data.services.user_service import user_service
# Remove warning from songs cog
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
intents = discord.Intents.all()
mentions = discord.AllowedMentions(everyone=False, users=True, roles=False)
class Bot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ban_cache = BanCache(self)
self.issue_cache = IssueCache(self)
self.rule_cache = RuleCache(self)
# force the config object and database connection to be loaded
if cfg and db and gatekeeper:
logger.info("Presetup phase completed! Connecting to Discord...")
async def setup_hook(self):
bot.remove_command("help")
for extension in initial_extensions:
await self.load_extension(extension)
setup_context_commands(self)
self.tasks = Tasks(self)
await init_client_session()
class MyTree(app_commands.CommandTree):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def interaction_check(self, interaction: discord.Interaction):
if interaction.user.bot:
return False
if gatekeeper.has(interaction.user.guild, interaction.user, 6):
return True
command = interaction.command
if isinstance(interaction.command, discord.app_commands.ContextMenu):
return True
if command is None or interaction.type != discord.InteractionType.application_command:
return True
if command.parent is not None:
command_name = f"{command.parent.name} {command.name}"
else:
command_name = command.name
db_user = user_service.get_user(interaction.user.id)
if db_user.command_bans.get(command_name):
ctx = GIRContext(interaction)
await ctx.send_error("You are not allowed to use that command!", whisper=True)
return False
options = interaction.data.get("options")
if options is None or not options:
return True
message_content = ""
for option in options:
if option.get("type") == 1:
for sub_option in option.get("options"):
message_content += str(sub_option.get("value")) + " "
else:
message_content += str(option.get("value")) + " "
triggered_words = find_triggered_filters(
message_content, interaction.user)
if triggered_words:
ctx = GIRContext(interaction)
await ctx.send_error("Your interaction contained a filtered word. Aborting!", whisper=True)
return
return True
bot = Bot(command_prefix='!', intents=intents, allowed_mentions=mentions, tree_cls=MyTree)
@bot.tree.error
async def app_command_error(interaction: discord.Interaction, error: AppCommandError):
ctx = GIRContext(interaction)
ctx.whisper = True
if isinstance(error, CommandInvokeError):
error = error.original
if isinstance(error, discord.errors.NotFound):
await ctx.channel.send(embed=discord.Embed(color=discord.Color.red(), title=":(\nYour command ran into a problem.", description=f"Sorry {interaction.user.mention}, it looks like I took too long to respond to you! If I didn't do what you wanted in time, please try again."), delete_after=7)
return
if (isinstance(error, commands.MissingRequiredArgument)
or isinstance(error, PermissionsFailure)
or isinstance(error, TransformerError)
or isinstance(error, commands.BadArgument)
or isinstance(error, commands.BadUnionArgument)
or isinstance(error, commands.MissingPermissions)
or isinstance(error, commands.BotMissingPermissions)
or isinstance(error, commands.MaxConcurrencyReached)
or isinstance(error, commands.NoPrivateMessage)):
await ctx.send_error(error, followup=True, whisper=True, delete_after=5)
else:
try:
raise error
except:
tb = traceback.format_exc()
logger.error(tb)
if len(tb.split('\n')) > 8:
tb = '\n'.join(tb.split('\n')[-8:])
tb_formatted = tb
if len(tb_formatted) > 1000:
tb_formatted = "...\n" + tb_formatted[-1000:]
await ctx.send_error(description=f"`{error}`\n```{tb_formatted}```", followup=True, whisper=True, delete_after=5)
@bot.event
async def on_ready():
print("""
88
""
,adPPYb,d8 88 8b,dPPYba,
a8" `Y88 88 88P' "Y8
8b 88 88 88
"8a, ,d88 88 88
`"YbbdP"Y8 88 88
aa, ,88
"Y8bbdP" \n""")
logger.info(
f'Logged in as: {bot.user.name} - {bot.user.id} ({discord.__version__})')
logger.info(f'Successfully logged in and booted...!')
await bot.ban_cache.fetch_ban_cache()
await bot.issue_cache.fetch_issue_cache()
await bot.rule_cache.fetch_rule_cache()
await scam_cache.fetch_scam_cache()
async def main():
async with bot:
await bot.start(os.environ.get("GIR_TOKEN"), reconnect=True)
asyncio.run(main())