This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
107 lines (92 loc) · 3.5 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
from discord.ext import commands
import discord
from cogs.utils import checks
import datetime, re
import json, asyncio
import copy
import logging
import traceback
import sys
from collections import Counter
from utils.credentials import load_credentials
description = """
I am a bot written by treefroog to provide tapirs! \n \nThis is a list of cogs along with their associated commands:
"""
initial_extensions = [
'cogs.admin',
'cogs.meta',
'cogs.mod',
'cogs.tapir',
'cogs.ships',
'cogs.star_citizen',
'cogs.xkcd',
'cogs.repl',
'cogs.misc',
'cogs.raffle',
'cogs.weather'
]
discord_logger = logging.getLogger('discord')
discord_logger.setLevel(logging.CRITICAL)
log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.FileHandler(filename='tapir-bot.log', encoding='utf-8')
log.addHandler(handler)
help_attrs = dict(hidden=True, name='halp')
bot = commands.Bot(command_prefix=['!'], description=description, pm_help=None, help_attrs=help_attrs)
@bot.event
async def on_command_error(ctx, error):
"""some custom error stuff"""
if isinstance(error, commands.NoPrivateMessage):
await ctx.message.author.send('This command cannot be used in private messages.')
elif isinstance(error, commands.DisabledCommand):
await ctx.message.author.send('Sorry. This command is disabled and cannot be used.')
elif isinstance(error, commands.CommandInvokeError):
print('In {0.command.qualified_name}:'.format(ctx), file=sys.stderr)
traceback.print_tb(error.original.__traceback__)
print('{0.__class__.__name__}: {0}'.format(error.original), file=sys.stderr)
@bot.event
async def on_ready():
"""what happens when tapir-bot connects to the discord api"""
await bot.change_presence(status=discord.Status.idle, activity=discord.Game(name='Say !help for help!'))
print('Logged in as:')
print('Username: ' + bot.user.name)
print('ID: ' + str(bot.user.id))
if not hasattr(bot, 'uptime'):
bot.uptime = datetime.datetime.utcnow()
@bot.event
async def on_command(ctx):
"""when a command happens it logs it"""
bot.commands_used[ctx.command.name] += 1
message = ctx.message
destination = None
if isinstance(message.channel, discord.abc.PrivateChannel):
destination = 'Private Message'
else:
destination = '#{0.channel.name} ({0.guild.name})'.format(message)
log.info('{0.created_at}: {0.author.name} in {1}: {0.content}'.format(message, destination))
@bot.event
async def on_message(message):
"""Some message checking stuff"""
if message.author.bot:
return
#Lowers the command for insensitive case
content_list = message.content.split(" ")
content_list[0] = content_list[0].lower()
message.content = " ".join(content_list)
await bot.process_commands(message)
if __name__ == '__main__':
if any('debug' in arg.lower() for arg in sys.argv):
bot.command_prefix = '!'
credentials = load_credentials()
bot.client_id = credentials['client_id']
bot.commands_used = Counter()
for extension in initial_extensions:
try:
bot.load_extension(extension)
except Exception as e:
print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))
bot.run(credentials['token'])
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)