-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.py
126 lines (102 loc) · 4.08 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
# https://github.com/Chris-Johnston/CSSBot_Py
# using the development version from the rewrite branch
# https://github.com/Rapptz/discord.py/tree/rewrite see the readme file for more information
# python3.6 -m pip install -r requirements.txt
# docs
# http://discordpy.readthedocs.io/en/rewrite/
# http://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html
# also may need to install libffi-dev, python-dev/python3.6-dev with apt-get
# or equivalent editor
import discord
from discord.ext import commands
import asyncio
# configuration files
import configparser
import sys, traceback
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.azure import metrics_exporter
log_format = "%(levelname)s %(filename)s:%(lineno)d %(funcName)s %(message)s"
logging.basicConfig(format=log_format) # have to set log level for each logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# load configuration to get around hard coded tokens
config = configparser.ConfigParser()
with open('config.ini') as config_file:
config.read_file(config_file)
if config.has_option('Configuration', 'azure_log'):
logger.addHandler(AzureLogHandler(
connection_string=config["Configuration"]["azure_log"]
))
# the default metrics exporter will include
# stats like memory, CPU, etc
exporter = metrics_exporter.new_metrics_exporter(
connection_string=config["Configuration"]["azure_log"]
)
# startup stuff
print('discordpy')
print(discord.__version__)
intents = discord.Intents.all()
# intents.invites = True
logger.info(f"intents: {intents} invites? {intents.invites}")
client = commands.Bot(command_prefix='>>', description='https://github.com/Chris-Johnston/CssBot-Py', case_insensitive=True, intents=intents)
# this is where extensions are added by default
default_extensions = ['cogs.basic',
'cogs.courseInfo',
'cogs.number_utils',
'cogs.hardware_utils',
'cogs.analytics',
'cogs.gpa',
'cogs.manpage',
'cogs.markov',
'cogs.hyphen',
'cogs.crob',
'cogs.starboard',
'cogs.advent',
'cogs.drinking_game',
'cogs.garden',
'cogs.aroundtheworld',
'cogs.fifth_glyph',
'cogs.cat',
'cogs.invite_checker',
'cogs.death_to_stickers',
'cogs.nofuckinziti',
'cogs.nofreakintutors',
'cogs.poke',
# 'cogs.spooky']
]
if __name__ == '__main__':
for extension in default_extensions:
try:
client.load_extension(extension)
except Exception as e:
logger.error(f"Failed to load extension {extension}.", exc_info=e)
print(f'Failed to load extension {extension}.', file=sys.stderr)
traceback.print_exc()
@client.event
async def on_ready():
logger.warn("Ready event handler")
# print some stuff when the bot goes online
print(f'Logged in {client.user.name} - {client.user.id}\nVersion {discord.__version__}')
await client.change_presence(activity=discord.Game(name='Try >>help'))
@client.event
async def on_connect():
logger.info("Connected.")
@client.event
async def on_resumed():
logger.warn("Session resumed.")
@client.event
async def on_disconnect():
logger.warn("Disconnected.")
@client.event
async def on_error(event, *args, **kwargs):
logger.error(f"Event {event} errored.", exc_info=sys.exec_info())
@client.event
async def on_guild_available(guild):
logger.info(f"Guild {guild.id} ({guild.name}) available.")
@client.event
async def on_guild_unavailable(guild):
logger.info(f"Guild {guild.id} ({guild.name}) unavailable.")
# now actually connect the bot
client.run(config.get(section='Configuration', option='connection_token'),
bot=True, reconnect=True)