-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
71 lines (51 loc) · 2.07 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
import discord
from discord.ext import commands
import dotenv
import os
import sys
import traceback
import logging
from tortoise import Tortoise
from iracing_cog.db_helpers import generate_schemas
dotenv.load_dotenv()
log = logging.getLogger('iracing_bot')
log.setLevel(logging.DEBUG)
description = "iRacing Bot to compare yourself to your friends and keep up to date on weekly combos"
intents = discord.Intents.default()
intents.members = True
def get_prefix(bot, message):
"""A callable Prefix for our bot. This could be edited to allow per server prefixes."""
# Notice how you can use spaces in prefixes. Try to keep them simple though.
prefixes = ['!']
# Check to see if we are outside of a guild. e.g DM's etc.
if not message.guild:
# Only allow ? to be used in DMs
return '?'
# If we are in a guild, we allow for the user to mention us or use any of the prefixes in our list.
return commands.when_mentioned_or(*prefixes)(bot, message)
bot = commands.Bot(command_prefix=get_prefix, description=description, intents=intents)
@bot.event
async def on_ready():
await generate_schemas()
try:
bot.load_extension('iracing_cog.iracing')
except Exception as e:
print(f'Failed to load iracing_cog.', file=sys.stderr)
traceback.print_exc()
print('We have logged in as {0.user}'.format(bot))
@bot.event
async def on_message(message):
if message.author == bot.user:
return
message_name = message.content[1:].split(' ')[0] # ignore the ! and get the first word
command_names = list(map(lambda c: c.name, list(bot.commands)))
if message_name not in command_names:
return
await bot.process_commands(message)
@bot.event
async def on_command_error(ctx, exception):
traceback.print_exc()
log.warning(f'command failed: {ctx.message.content} with exception: {exception}')
await ctx.send('Whoops! Looks like something went wrong. '
'Use `!help` to learn about each command or wait and try again soon.')
bot.run(os.getenv('BOT_TOKEN'), bot=True, reconnect=True)