-
Notifications
You must be signed in to change notification settings - Fork 8
/
bot.py
executable file
·142 lines (109 loc) · 4.22 KB
/
bot.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import logging
import re
import sys
from pathlib import Path
import aiohttp
import discord
from discord import app_commands
from discord.ext import commands
import config
cogs = [
x.stem
for x in Path('alexBot/cogs').glob('*.py')
if x.stem
not in [
"__init__",
]
]
# cogs = ['reminders', 'errors'] # used to test single cog at a time
log = logging.getLogger('alexBot')
log.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
handler.setFormatter(
logging.Formatter('[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
)
log.addHandler(handler)
for logPath in ['discord', 'websockets', 'aiosqlite']:
z = logging.getLogger(logPath)
z.setLevel(logging.INFO)
for logPath in ['sqlalchemy', 'discord.gateway']:
z = logging.getLogger(logPath)
z.setLevel(logging.ERROR)
LINKWRAPPERREGEX = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[#-_]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)', re.I)
intents = discord.Intents.all()
allowed_mentions = discord.AllowedMentions.none()
class Bot(commands.Bot):
def __init__(self, **kwargs):
super().__init__(command_prefix=config.prefix, intents=intents, allowed_mentions=allowed_mentions, **kwargs)
self.session = None
self.config = config
self.owner = None
self.setup_hook = self.cogSetup
self.minecraft = True
self.handler = handler
self.voiceCommandsGroup = app_commands.Group(
name="voice", description="Voice related commands", guild_only=True
)
self.tree.add_command(self.voiceCommandsGroup)
async def on_ready(self):
log.info(f'Logged on as {self.user} ({self.user.id}). prefix is {config.prefix}')
self.owner = (await self.application_info()).owner
log.info(f'owner is {self.owner} ({self.owner.id})')
self.session = aiohttp.ClientSession()
async def cogSetup(self):
await self.load_extension('jishaku')
for cog in cogs:
try:
await self.load_extension(f"alexBot.cogs.{cog}")
log.info(f'loaded {cog}')
except (commands.ExtensionNotFound
, commands.ExtensionAlreadyLoaded
, commands.NoEntryPointError
, commands.ExtensionFailed) as e:
log.error(f'Could not load extension {cog} due to {e.__class__.__name__}: {e}')
log.exception(e)
@staticmethod
def clean_mentions(content: str) -> str:
content = content.replace('`', '\'')
content = content.replace('@', '@\u200b')
content = content.replace('&', '&\u200b')
content = content.replace('<#', '<#\u200b')
return content
@staticmethod
def clean_formatting(content: str) -> str:
content = content.replace('_', '\\_')
content = content.replace('*', '\\*')
content = content.replace('`', '\\`')
return content
@staticmethod
def clean_links(content: str) -> str:
content = LINKWRAPPERREGEX.sub(r'<\1>', content)
return content
def clean_clean(self, content):
content = self.clean_mentions(content)
content = self.clean_formatting(content)
content = self.clean_links(content)
return content
async def on_message(self, message: discord.Message):
if message.author.bot:
return
else:
await self.process_commands(message)
async def on_command(self, ctx):
# thanks dogbot ur a good
content = ctx.message.content
content = self.clean_mentions(content)
author = ctx.message.author
guild = ctx.guild
checks = [c.__qualname__.split('.')[0] for c in ctx.command.checks]
location = '[DM]' if isinstance(ctx.channel, discord.DMChannel) else f'[Guild {guild.name} {guild.id}]'
log.info('%s [cmd] %s(%d) "%s" checks=%s', location, author, author.id, content, ','.join(checks) or '(none)')
bot = Bot()
# if we're running on windows:
if sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
bot.run(config.discord_token, log_handler=None)