-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (89 loc) · 3.8 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
import discord
from discord.ext import commands
import logging
from logging.handlers import TimedRotatingFileHandler
import os
# spin up the bot
# configure logger
handler = TimedRotatingFileHandler("log", when="midnight", interval=1)
handler.suffix = "%Y_%m_%d"
logging.basicConfig(level=logging.INFO, handlers=[handler])
logging.info("Starting.")
# retrieve discord bot key
file = open('key', mode='r')
key = file.read().rstrip()
file.close()
logging.info("Key retrieved.")
# retrieve connection string
file = open('connection_string', mode='r')
connection_string = file.read().rstrip()
os.environ['CONNECTION_STRING'] = connection_string
connection_string = str()
file.close()
logging.info("Connection string set, booting up.")
# set up extensions, assign command prefix
startup_extensions = ['UserCommands', 'PatronCommands', 'AdminCommands']
bot = commands.Bot(command_prefix='!')
menacing_emoji = None
# load extensions
for extension in startup_extensions:
try:
bot.load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {0}\n{1}'.format(extension, exc))
logging.error("Failed to load extension{0}\n{1}".format(extension, exc))
quit()
# async methods
@bot.event
async def on_ready():
logging.info('Logged in as ' + str(bot.user.name) + ' with id: ' + str(bot.user.id))
logging.info('--------------------------------------------------------------------------')
logging.info('Performing startup checks.')
guilds = bot.guilds
if len(guilds) > 1:
logging.error('Bot in in multiple servers. I can only be in one right now.')
print('I\'m in multiple servers. I can only be in one right now.')
quit()
# the interns-assemble channel already exists, so i can just grab that id once i get there, but
# the intern-log channel might not when i deploy this, so let's have it create that channel and category
need_to_create = True
for channel in guilds[0].channels:
if 'INTERN_LOG_CHANNEL_ID' in os.environ:
need_to_create = False
break
if channel.name == 'interns-assemble':
os.environ['MOD_CHANNEL_ID'] = str(channel.id)
if channel.name == 'intern-log':
for subchannel in channel.channels:
if subchannel.name == 'intern-log-messages':
os.environ['INTERN_LOG_CHANNEL_ID'] = str(subchannel.id)
if need_to_create:
intern = None
god = None
for role in guilds[0].roles:
if role.name == 'interns':
intern = role
elif role.name == 'gods':
god = role
overwrites = {
guilds[0].default_role: discord.PermissionOverwrite(read_messages=False),
guilds[0].me: discord.PermissionOverwrite(read_messages=True, send_messages=True),
intern: discord.PermissionOverwrite(read_messages=True, send_messages=False),
god: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
category = await guilds[0].create_category('intern-log', overwrites=overwrites)
channel = await guilds[0].create_text_channel('intern-log', overwrites=overwrites, category=category)
await channel.send('Hello! This is the start of my logging channel!')
for emoji in bot.guilds[0].emojis:
if emoji.name == 'menacing':
global menacing_emoji
menacing_emoji = emoji
break
await guilds[0].get_channel(int(os.environ['INTERN_LOG_CHANNEL_ID'])).send('Cyber intern online!')
@bot.event
async def on_message(message):
if 'approaching me?' in message.clean_content.lower() and menacing_emoji is not None:
await message.add_reaction(menacing_emoji)
await bot.process_commands(message)
bot.run(key)