forked from scoliono/secreth_discordbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (62 loc) · 2.01 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "James Shiffer"
import logging as log
import asyncio
import discord
import sys
from Config import TOKEN, PREFIX
import Commands
# Enable logging
log.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=log.INFO,
filename='logs/logging.log')
logger = log.getLogger(__name__)
ch = log.StreamHandler(sys.stdout)
ch.setFormatter(log.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
ch.setLevel(log.INFO)
logger.addHandler(ch)
games = {}
# The bot client.
bot = discord.Client()
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"' % (update, error))
@bot.event
async def on_ready():
"""Called once the bot has logged in."""
log.info('Logged in as name "{}" with ID "{}".'.format(bot.user.name, bot.user.id))
# Begin updating status every 5 seconds
await update_status()
@bot.event
async def on_message(msg):
"""Handles messages sent to the bot."""
if isinstance(msg.channel, discord.channel.DMChannel) or msg.channel.name != "secret-hitler":
return
msg_content = msg.content.strip()
# Bot won't take commands from itself
if msg.author.id == bot.user.id:
return
# Take off prefix, split command and arguments
cmd, *args = msg_content.split()
cmd = cmd[len(PREFIX):].lower().strip()
try:
cmd_func = getattr(Commands, 'command_' + cmd)
except AttributeError:
log.debug('Invalid command "' + cmd + '"')
return
log.debug('"command_' + cmd + '" called')
await cmd_func(bot, msg, args)
async def update_status():
while True:
await bot.change_presence(
status=discord.Status.online,
activity=discord.Game("{}help | {} servers".format(PREFIX, len(bot.guilds)))
)
await asyncio.sleep(5)
def main():
"""Starts the bot."""
print("Script started.")
# Log in
bot.run(TOKEN)
if __name__ == '__main__':
main()