forked from linkian209/karaoke-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
karaoke_bot.py
111 lines (94 loc) · 2.74 KB
/
karaoke_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
108
109
110
111
#!/usr/bin/python
import aiohttp
import discord
import logging.config
import getopt
import sys
import os
import signal
import datetime
import configparser
from discord.ext import commands
from karaoke_bot_funcs import *
# Extensions for the bot
startup_extensions = ["modules.karaoke_bot_commands",
"modules.karaoke_bot_events",
"modules.karaoke_bot_random"]
# Main Function
def main():
# Set defaults
command_prefix = 'g'
print('Karaoke-Bot Starting up...\nReading Config...')
# First parse config
config = configparser.ConfigParser()
config.read('karaoke_bot.conf')
for section in config.sections():
if section == 'General':
for item in config[section]:
if item == 'command_prefix':
command_prefix = config[section][item]
else:
print('Unrecognized item: {}'.format(item))
usage()
sys.exit(2)
# Command line arguments trump config
print('Parsing Command Line Options...')
# Get started
try:
flags = 'ht:c:'
long_flags = ['help','command-prefix=']
opts, args = getopt.getopt(sys.argv[1:], flags, long_flags)
# Whomp, got an error
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
# Parse these args
# Loop through options
for o,a in opts:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-t', '--token'):
token = a
elif o in ('-c', '--command-prefix'):
command_prefix = a
else:
print('{} is not a valid argument'.format(a))
usage()
sys.exit(2)
if token is None:
usage()
sys.exit(2)
# Set up database
print('Configuring Database...')
if not os.path.isfile('karaokebot.sqlt'):
print('Database does not exist! Creating...')
configure_database()
# Set up logging
print('Setting Up Loggers...')
logger = logging.config.fileConfig('logging.conf')
# Setup bot commands
print('Configuring Bot...')
bot = commands.Bot(connector=aiohttp.TCPConnector(verify_ssl=False),
command_prefix=command_prefix,
activity=discord.Game('{}help'.format(command_prefix)))
print(' - Command Prefix: {}'.format(command_prefix))
# Load in bot extensions
for extension in startup_extensions:
try:
bot.load_extension(extension)
print(' - Loaded extension {}'.format(extension))
except Exception as e:
error = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, error))
# Create event loop and run!
print('Starting Bot!')
bot.run(os.getenv("TOKEN"))
# Once we get a signal to quit, logout then exit!
bot.logout()
print('Exiting!')
sys.exit(0)
# Start script
if __name__ == "__main__":
main()