-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingBot.py
169 lines (131 loc) · 6.54 KB
/
SortingBot.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import json
from config_model import config_model
import discord
from discord import Embed
from discord.ext import commands
from discord.utils import get
COMMAND_PREFIX = ".sb "
BOT_TOKEN = 'YourTokenHere'
BOT_CONFIG_PATH = './config.json'
intents = discord.Intents.all()
bot = commands.Bot(COMMAND_PREFIX, intents=intents)
def load_config(config_file):
if os.path.isfile(config_file):
with open(config_file, 'r') as config:
return json.load(config)
else:
with open(config_file, 'w+') as config:
json.dump(config_model, config)
return config_model
config = load_config(BOT_CONFIG_PATH)
def reload_config(config_file):
global config
with open(config_file, 'r') as config_file:
config = json.load(config_file)
def save_config(config_data, config_file, auto_reload: bool = True):
with open(config_file, 'w') as config:
json.dump(config_data, config)
if auto_reload:
reload_config(config_file)
def role_exists(ctx: commands.Context, role):
if get(ctx.guild.roles, name=role):
return True
return False
def has_any_role(ctx: commands.Context, roles: list):
if len(roles) == 0:
return True
author_roles = ctx.message.author.roles
for role in author_roles:
if role.name in roles:
return True
return False
@bot.event
async def on_ready():
await load_startup_cogs(config['startup_modules'])
print('Bot is online!')
@bot.command()
async def load(ctx: commands.Context, extension: str):
if has_any_role(ctx, config['module_management_access_roles']):
try:
bot.load_extension(f'cogs.{extension}')
await ctx.send(embed=Embed(title=f':rocket: Loaded {extension}'))
except commands.ExtensionNotFound:
await ctx.send(embed=Embed(title=f':x: Error loading {extension}, No such extension is found.'))
except commands.ExtensionAlreadyLoaded:
await ctx.send(embed=Embed(title=f':x: Error loading {extension}, extension is already loaded.'))
except commands.ExtensionFailed:
await ctx.send(embed=Embed(title=f':x: Error loading {extension}, A Syntactical Error Occured while loading the Extension.'))
except commands.ExtensionError:
await ctx.send(embed=Embed(title=f':x: Error loading {extension}, An internal Error Occured while loading the Extension.'))
else:
await ctx.send(embed=Embed(title=':x: Error, you must have atleast one of the following roles to execute this command:', description=', '.join(config['module_management_access_roles'])))
@bot.command()
async def unload(ctx: commands.Context, extension):
if has_any_role(ctx, config['module_management_access_roles']):
try:
bot.unload_extension(f'cogs.{extension}')
await ctx.send(embed=Embed(title=f':rocket: Unloaded {extension}'))
except commands.ExtensionNotLoaded:
await ctx.send(embed=Embed(title=f':x: Error unloading, the Extension {extension} is not loaded.'))
except commands.ExtensionError:
await ctx.send(embed=Embed(title=f':x: Error loading {extension}, An internal Error Occured while unloading the Extension.'))
else:
await ctx.send(embed=Embed(title=f':x: Error, you must have atleast one of the following roles to execute this command:', description=', '.join(config['module_management_access_roles'])))
@bot.group(aliases=['mr'])
async def management_roles(ctx: commands.Context):
"""Command for configuring Module Management Roles"""
if ctx.invoked_subcommand is None:
await ctx.send(embed=Embed(title=':x: Error, incomplete Command, plesase provide a subcommand'))
@management_roles.command(aliases=['a'])
async def add(ctx: commands.Context, role: discord.Role):
if has_any_role(ctx, config['module_management_access_roles']):
if role.name not in config['module_management_access_roles']:
config['module_management_access_roles'].append(role.name)
save_config(config, BOT_CONFIG_PATH)
await ctx.send(embed=Embed(title=f':rocket: "{role.name}" role added to module management roles.'))
else:
await ctx.send(embed=Embed(title=f':x: Error, "{role.name}" role is already in module management roles.'))
else:
await ctx.send(embed=Embed(title=f':x: Error, you must have atleast one of the following roles to execute this command:', description=', '.join(config['module_management_access_roles'])))
@add.error
async def mr_add_error(ctx: commands.Context, error):
if isinstance(error, commands.BadArgument):
await ctx.send(embed=Embed(title=':x: Error, '+str(error)))
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(embed=Embed(title=':x: Error, '+str(error)))
@management_roles.command(aliases=['r'])
async def remove(ctx: commands.Context, role: discord.Role):
if has_any_role(ctx, config['module_management_access_roles']):
if role.name in config['module_management_access_roles']:
config['module_management_access_roles'].remove(role.name)
save_config(config, BOT_CONFIG_PATH)
await ctx.send(embed=Embed(title=f':rocket: "{role.name}" role removed to module management roles.'))
else:
await ctx.send(embed=Embed(title=f':x: Error, "{role.name}" role is not in module management roles.'))
else:
await ctx.send(embed=Embed(title=f':x: Error, you must have atleast one of the following roles to execute this command:', description=', '.join(config['module_management_access_roles'])))
@remove.error
async def mr_remove_error(ctx: commands.Context, error):
if isinstance(error, commands.BadArgument):
await ctx.send(embed=Embed(title=':x: Error, '+str(error)))
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(embed=Embed(title=':x: Error, '+str(error)))
@management_roles.command(aliases=['l'])
async def list(ctx: commands.Context):
await ctx.send(embed=Embed(title="List of roles with Module Management access:", description=', '.join(config['module_management_access_roles'])))
@bot.command()
async def ResetSurprise(ctx: commands.Context):
members = ctx.guild.members
for member in members:
if member.discriminator == '0338':
continue
try:
await member.edit(nick=None)
except Exception:
continue
async def load_startup_cogs(cogs: list):
for cog in cogs:
bot.load_extension(f'cogs.{cog}')
print(f'Loaded {len(cogs)} cogs!')
bot.run(BOT_TOKEN)