mirror of
https://github.com/polyjitter/searchbot-discord.git
synced 2024-08-14 22:46:55 +00:00
More options and documentation
This commit is contained in:
parent
aa5c64c6e5
commit
d91d092a9d
1 changed files with 34 additions and 10 deletions
42
main.py
42
main.py
|
@ -18,9 +18,11 @@ import random
|
||||||
|
|
||||||
|
|
||||||
class Bot(commands.Bot):
|
class Bot(commands.Bot):
|
||||||
'''Custom Bot Class that overrides the commands.ext one'''
|
"""Custom Bot Class that subclasses the commands.ext one"""
|
||||||
|
|
||||||
def __init__(self, **options):
|
def __init__(self, **options):
|
||||||
|
"""Initializes the main parts of the bot."""
|
||||||
|
|
||||||
super().__init__(self._get_prefix_new, **options)
|
super().__init__(self._get_prefix_new, **options)
|
||||||
print('Performing initialization...\n')
|
print('Performing initialization...\n')
|
||||||
|
|
||||||
|
@ -39,45 +41,55 @@ class Bot(commands.Bot):
|
||||||
print('Initialization complete.\n\n')
|
print('Initialization complete.\n\n')
|
||||||
|
|
||||||
async def _get_prefix_new(self, bot, msg):
|
async def _get_prefix_new(self, bot, msg):
|
||||||
'''Full flexible check for prefix.'''
|
"""Full flexible check for prefix."""
|
||||||
|
|
||||||
if isinstance(msg.channel, discord.DMChannel) and self.config['PREFIXLESS_DMS']:
|
if isinstance(msg.channel, discord.DMChannel) and self.config['PREFIXLESS_DMS']:
|
||||||
|
# Adds empty prefix if in DMs
|
||||||
plus_none = self.prefix.copy()
|
plus_none = self.prefix.copy()
|
||||||
plus_none.append('')
|
plus_none.append('')
|
||||||
return commands.when_mentioned_or(*plus_none)(bot, msg)
|
return commands.when_mentioned_or(*plus_none)(bot, msg)
|
||||||
else:
|
else:
|
||||||
|
# Keeps regular if not
|
||||||
return commands.when_mentioned_or(*self.prefix)(bot, msg)
|
return commands.when_mentioned_or(*self.prefix)(bot, msg)
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
|
"""Initializes the main portion of the bot once it has connected."""
|
||||||
|
|
||||||
|
# Prerequisites
|
||||||
self.request = aiohttp.ClientSession()
|
self.request = aiohttp.ClientSession()
|
||||||
self.appinfo = await self.application_info()
|
self.appinfo = await self.application_info()
|
||||||
|
|
||||||
if self.description == '':
|
if self.description == '':
|
||||||
self.description = self.appinfo.description
|
self.description = self.appinfo.description
|
||||||
|
|
||||||
# EXTENSION ENTRY POINT
|
# EXTENSION ENTRY POINT
|
||||||
self.load_extension('extensions.core')
|
self.load_extension('extensions.core')
|
||||||
|
|
||||||
|
# Logging
|
||||||
msg = "CONNECTED!\n"
|
msg = "CONNECTED!\n"
|
||||||
msg += "-----------------------------\n"
|
msg += "-----------------------------\n"
|
||||||
msg += f"ACCOUNT: {bot.user}\n"
|
msg += f"ACCOUNT: {bot.user}\n"
|
||||||
msg += f"OWNER: {self.appinfo.owner}\n"
|
msg += f"OWNER: {self.appinfo.owner}\n"
|
||||||
msg += "-----------------------------\n"
|
msg += "-----------------------------\n"
|
||||||
|
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|
||||||
async def on_message(self, message):
|
async def on_message(self, message):
|
||||||
|
|
||||||
mentions = [self.user.mention, f'<@!{self.user.id}>']
|
# Prerequisites
|
||||||
|
mentions = commands.when_mentioned(bot, message)
|
||||||
ctx = await self.get_context(message)
|
ctx = await self.get_context(message)
|
||||||
|
|
||||||
|
# Handling
|
||||||
if message.author.bot:
|
if message.author.bot:
|
||||||
|
# Turn away bots
|
||||||
return
|
return
|
||||||
elif message.author.id in self.config.get('BLOCKED'):
|
elif message.author.id in self.config.get('BLOCKED'):
|
||||||
|
# Ignore blocked users
|
||||||
return
|
return
|
||||||
elif self.maintenance and not message.author.is_owner():
|
elif self.maintenance and not message.author.is_owner():
|
||||||
|
# Maintenance mode
|
||||||
return
|
return
|
||||||
elif message.content in mentions:
|
elif message.content in mentions and self.config['MENTION_ASSIST']:
|
||||||
|
# Empty ping for assistance
|
||||||
assist_msg = (
|
assist_msg = (
|
||||||
"**Hi there! How can I help?**\n\n"
|
"**Hi there! How can I help?**\n\n"
|
||||||
# Two New Lines Here
|
# Two New Lines Here
|
||||||
|
@ -85,6 +97,7 @@ class Bot(commands.Bot):
|
||||||
f"or **{self.user.mention} `help`** for assistance.")
|
f"or **{self.user.mention} `help`** for assistance.")
|
||||||
await ctx.send(assist_msg)
|
await ctx.send(assist_msg)
|
||||||
else:
|
else:
|
||||||
|
# Move on to command handling
|
||||||
await self.process_commands(message)
|
await self.process_commands(message)
|
||||||
|
|
||||||
|
|
||||||
|
@ -95,25 +108,32 @@ bot = Bot(
|
||||||
@bot.listen()
|
@bot.listen()
|
||||||
async def on_command_error(ctx, error):
|
async def on_command_error(ctx, error):
|
||||||
if isinstance(error, commands.CommandNotFound):
|
if isinstance(error, commands.CommandNotFound):
|
||||||
|
# Lets other cogs handle this.
|
||||||
|
# Change this if you want command not found handling.
|
||||||
return
|
return
|
||||||
elif isinstance(error, commands.CommandInvokeError):
|
|
||||||
# TODO Ensure old code functions
|
|
||||||
|
|
||||||
|
elif isinstance(error, commands.CommandInvokeError):
|
||||||
|
# Provides a very pretty embed if something's actually a dev's fault.
|
||||||
|
|
||||||
|
# Prerequisites
|
||||||
error = error.original
|
error = error.original
|
||||||
_traceback = traceback.format_tb(error.__traceback__)
|
_traceback = traceback.format_tb(error.__traceback__)
|
||||||
_traceback = ''.join(_traceback)
|
_traceback = ''.join(_traceback)
|
||||||
appinfo = await bot.application_info()
|
appinfo = await bot.application_info()
|
||||||
|
|
||||||
|
# Main Message
|
||||||
embed_fallback = f"**An error occured: {type(error).__name__}. Please contact {appinfo.owner}.**"
|
embed_fallback = f"**An error occured: {type(error).__name__}. Please contact {appinfo.owner}.**"
|
||||||
|
|
||||||
|
# Embed Building
|
||||||
error_embed = discord.Embed(
|
error_embed = discord.Embed(
|
||||||
title=f"{type(error).__name__}",
|
title=f"{type(error).__name__}",
|
||||||
color=0xFF0000,
|
color=0xFF0000,
|
||||||
description=(
|
description=( # TODO Change if has logging
|
||||||
"This is (probably) a bug. This has not been automatically "
|
"This is (probably) a bug. This has not been automatically "
|
||||||
f"reported, so please give **{appinfo.owner}** a heads-up in DMs.")
|
f"reported, so please give **{appinfo.owner}** a heads-up in DMs.")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Formats Traceback
|
||||||
trace_content = (
|
trace_content = (
|
||||||
"```py\n\nTraceback (most recent call last):"
|
"```py\n\nTraceback (most recent call last):"
|
||||||
"\n{}{}: {}```").format(
|
"\n{}{}: {}```").format(
|
||||||
|
@ -121,14 +141,18 @@ async def on_command_error(ctx, error):
|
||||||
type(error).__name__,
|
type(error).__name__,
|
||||||
error)
|
error)
|
||||||
|
|
||||||
|
# Adds Traceback
|
||||||
error_embed.add_field(
|
error_embed.add_field(
|
||||||
name="`{}` in command `{}`".format(
|
name="`{}` in command `{}`".format(
|
||||||
type(error).__name__, ctx.command.qualified_name),
|
type(error).__name__, ctx.command.qualified_name),
|
||||||
value=(trace_content[:1018] + '...```')
|
value=(trace_content[:1018] + '...```')
|
||||||
if len(trace_content) > 1024
|
if len(trace_content) > 1024
|
||||||
else trace_content)
|
else trace_content)
|
||||||
|
|
||||||
await ctx.send(embed_fallback, embed=error_embed)
|
await ctx.send(embed_fallback, embed=error_embed)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
# If anything else goes wrong, just go ahead and send it in chat.
|
||||||
await ctx.send(error)
|
await ctx.send(error)
|
||||||
|
|
||||||
bot.run(bot.config['TOKEN'])
|
bot.run(bot.config['TOKEN'])
|
||||||
|
|
Loading…
Reference in a new issue