-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
137 lines (121 loc) · 4.86 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
127
128
129
130
131
132
133
134
135
136
137
# Bot cog template - Dynamic cog loading template.
# Copyright (C) 2018 - Valentijn "noirscape" V.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import yaml
import discord
from discord.ext import commands
import os
'''Bot framework that can dynamically load and unload cogs.'''
config = yaml.safe_load(open('config.yml'))
secure = yaml.safe_load(open('secure.yml'))
bot = commands.Bot(command_prefix=commands.when_mentioned_or(
config['prefix']),
description='')
bot.config = config
bot.loaded_cogs = []
bot.unloaded_cogs = []
def initLogging():
logformat = "%(asctime)s %(name)s:%(levelname)s:%(message)s"
logging.basicConfig(level=logging.INFO, format=logformat,
datefmt='%Y-%m-%d %H:%M:%S')
logging.getLogger("discord").setLevel(logging.WARNING)
return logging.getLogger("musicbot2")
def check_if_dirs_exist():
'''Function that creates the "cogs" directory if it doesn't exist already'''
os.makedirs('cogs', exist_ok=True)
def load_autoload_cogs():
'''
Loads all .py files in the cogs subdirectory that are in the config file as "autoload_cogs" as cogs into the bot.
If your cogs need to reside in subfolders (ie. for config files) create a wrapper file in the cogs
directory to load the cog.
'''
for entry in os.listdir('cogs'):
if entry.endswith('.py') and os.path.isfile('cogs/{}'.format(entry)) and entry[:-3] in config['autoload_cogs']:
try:
bot.load_extension("cogs.{}".format(entry[:-3]))
bot.loaded_cogs.append(entry[:-3])
except Exception as e:
print(e)
else:
print('Successfully loaded cog {}'.format(entry))
def get_names_of_unloaded_cogs():
'''
Creates an easy loadable list of cogs.
If your cogs need to reside in subfolders (ie. for config files) create a wrapper file in the auto_cogs
directory to load the cog.
'''
for entry in os.listdir('cogs'):
if entry.endswith('.py') and os.path.isfile('cogs/{}'.format(entry)) and entry[:-3] not in bot.loaded_cogs:
bot.unloaded_cogs.append(entry[:-3])
check_if_dirs_exist()
load_autoload_cogs()
get_names_of_unloaded_cogs()
@bot.command()
@commands.is_owner()
async def list_cogs(ctx):
'''Lists all cogs and their status of loading.'''
cog_list = commands.Paginator(prefix='', suffix='')
cog_list.add_line('**✅ Successfully loaded:**')
for cog in bot.loaded_cogs:
cog_list.add_line('- ' + cog)
cog_list.add_line('**❌ Not loaded:**')
for cog in bot.unloaded_cogs:
cog_list.add_line('- ' + cog)
for page in cog_list.pages:
await ctx.send(page)
@bot.command()
@commands.is_owner()
async def load(ctx, cog):
'''Try and load the selected cog.'''
if cog not in bot.unloaded_cogs:
await ctx.send('⚠ WARNING: Cog appears not to be found in the available cogs list. Will try loading anyway.')
if cog in bot.loaded_cogs:
return await ctx.send('Cog already loaded.')
try:
bot.load_extension('cogs.{}'.format(cog))
except Exception as e:
await ctx.send('**💢 Could not load cog: An exception was raised. For your convenience, the exception will be printed below:**')
await ctx.send('```{}\n{}```'.format(type(e).__name__, e))
else:
bot.loaded_cogs.append(cog)
bot.unloaded_cogs.remove(cog)
await ctx.send('✅ Cog successfully loaded.')
@bot.command()
@commands.is_owner()
async def unload(ctx, cog):
if cog not in bot.loaded_cogs:
return await ctx.send('💢 Cog not loaded.')
bot.unload_extension('cogs.{}'.format((cog)))
bot.loaded_cogs.remove(cog)
bot.unloaded_cogs.append(cog)
await ctx.send('✅ Cog successfully unloaded.')
@bot.command()
async def about(ctx):
"""About Soundhax 2"""
embed = discord.Embed(title="Soundhax 2")
embed.set_author(name="noirscape, based on a module by thecommondude")
embed.set_thumbnail(url="https://loli.catgirlsin.space/ea/9c684fd8063001e455a24c5d7d0507d755ce7f.png ")
embed.url = "https://github.com/noirscape/MusicBot-2"
embed.description = "A music bot."
await ctx.send(embed=embed)
@bot.event
async def on_ready():
print('----------')
print('Logged in as:')
print(bot.user.name)
print(bot.user.id)
print('----------')
bot.logger = initLogging()
bot.run(secure["token"])