Browse Source

removed all the bad code and like did most of the formatting. Just needs the function to actually process the choice and the invoke of the commands and stuff.

tags/v1.8.0
Roxie Gibson 6 years ago
parent
commit
87b7971dd6
1 changed files with 59 additions and 357 deletions
  1. +59
    -357
      roxbot/settings/settings.py

+ 59
- 357
roxbot/settings/settings.py View File



# TODO: Display the settings your changing in the menu as yu change them. # TODO: Display the settings your changing in the menu as yu change them.


class Menu():
def __init__(self, name, *params, **options):
class Menu:
def __init__(self, name, settings, *params):
self.name = name self.name = name
self.params = params self.params = params
if options is None:
options = {"type": "values"}

self.formatted_params = self._parse_params(settings, self.name)
self.title = "'Roxbot Settings: {}'\n".format(self.name) self.title = "'Roxbot Settings: {}'\n".format(self.name)

self.content = self._format_content(self.title, self.params, "```python", "```") self.content = self._format_content(self.title, self.params, "```python", "```")


@staticmethod @staticmethod
choices = "\n" choices = "\n"
for x, setting in enumerate(params): for x, setting in enumerate(params):
if setting != "convert": if setting != "convert":
if "Enable" in setting or "Disable" in setting:
if setting != [*params][x]: # Just in case params is dict_keys, we make a new list
choices += "[{}] {}\n".format(x + 1, setting) choices += "[{}] {}\n".format(x + 1, setting)
else: else:
choices += "[{}] Edit '{}'\n".format(x+1, setting) choices += "[{}] Edit '{}'\n".format(x+1, setting)
def _parse_params(settings, name): def _parse_params(settings, name):
params = [*settings.keys()] params = [*settings.keys()]
params_copy = settings.copy().keys() params_copy = settings.copy().keys()
# Enable/Disable Parse
for param in params_copy: for param in params_copy:
if settings["convert"].get(param) == "bool": if settings["convert"].get(param) == "bool":
# Enable/Disable Parse
if param == "enabled": if param == "enabled":
enable_options = ["Enable '{}'".format(name), "Disable '{}'".format(name)]
options = ["Enable '{}'".format(name), "Disable '{}'".format(name)]
else: else:
enable_options = ["Enable '{}'".format(param), "Disable '{}'".format(param)]
options = ["Enable '{}'".format(param), "Disable '{}'".format(param)]
params.remove(param) params.remove(param)
params = [*enable_options, *params]

params = [*options, *params]
elif isinstance(settings.get(param), list):
# Add and Remove Parse
options = ["Add {}".format(param), "Remove {}".format(param)]
params.remove(param)
params = [*params, *options]
elif isinstance(settings.get(param), int) or isinstance(settings.get(param), str):
# Set parse
options = "Set {}".format(param)
params.remove(param)
params = [*params, options]
return params return params


@classmethod @classmethod
def nsfw(cls, guild): def nsfw(cls, guild):
name = "NSFW" name = "NSFW"
params = guild_settings.get(guild).nsfw.keys()
return cls(name, *params)
settings = guild_settings.get(guild).nsfw
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def self_assign(cls, guild): def self_assign(cls, guild):
name = "Self Assign" name = "Self Assign"
params = guild_settings.get(guild).self_assign.keys()
return cls(name, *params)
settings = guild_settings.get(guild).self_assign
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def twitch(cls, guild): def twitch(cls, guild):
name = "Twitch" name = "Twitch"
params = guild_settings.get(guild).twitch.keys()
return cls(name, *params)
settings = guild_settings.get(guild).twitch
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def perm_roles(cls, guild): def perm_roles(cls, guild):
name = "Perm Roles" name = "Perm Roles"
params = guild_settings.get(guild).perm_roles.keys()
return cls(name, *params)
settings = guild_settings.get(guild).perm_roles
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def logging(cls, guild): def logging(cls, guild):
name = "Logging" name = "Logging"
params = guild_settings.get(guild).logging.keys()
return cls(name, *params)
settings = guild_settings.get(guild).logging
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def voice(cls, guild): def voice(cls, guild):
name = "Voice" name = "Voice"
params = guild_settings.get(guild).voice.keys()
return cls(name, *params)
settings = guild_settings.get(guild).voice
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def greets(cls, guild): def greets(cls, guild):
name = "Greets" name = "Greets"
params = guild_settings.get(guild).greets.keys()
return cls(name, *params)
settings = guild_settings.get(guild).greets
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def goodbyes(cls, guild): def goodbyes(cls, guild):
params = guild_settings.get(guild).goodbyes.keys()
return cls("Goodbyes", *params)
name = "Goodbyes"
settings = guild_settings.get(guild).goodbyes
params = settings.keys()
return cls(name, settings, *params)


@classmethod @classmethod
def join_leave(cls): def join_leave(cls):
name = "JoinLeave" name = "JoinLeave"
params = ["goodbyes", "greets"] params = ["goodbyes", "greets"]
options = {"type": "menu"}
return cls(name, *params, **options)
return cls(name, *params)


@classmethod @classmethod
def base(cls, params): def base(cls, params):
name = "Base Menu" name = "Base Menu"
params = params params = params
options = {"type": "menu"}
return cls(name, *params, **options)
return cls(name, *params)




class Settings: class Settings:
@commands.command(aliases=["printsettingsraw"]) @commands.command(aliases=["printsettingsraw"])
@checks.is_admin_or_mod() @checks.is_admin_or_mod()
async def printsettings(self, ctx, option=None): async def printsettings(self, ctx, option=None):
"OWNER OR ADMIN ONLY: Prints the servers settings file."
"""OWNER OR ADMIN ONLY: Prints the servers settings file."""
# TODO: Use paginator to make the output here not break all the time. # TODO: Use paginator to make the output here not break all the time.
config = guild_settings.get(ctx.guild) config = guild_settings.get(ctx.guild)
settings = dict(config.settings.copy()) # Make a copy of settings so we don't change the actual settings. settings = dict(config.settings.copy()) # Make a copy of settings so we don't change the actual settings.
em.add_field(name="custom_commands", value="For Custom Commands, use the custom list command.", inline=False) em.add_field(name="custom_commands", value="For Custom Commands, use the custom list command.", inline=False)
return await ctx.send(embed=em) return await ctx.send(embed=em)


def _make_settings_menu(self, ctx):
x = 0
output = "'Roxbot Settings:' #Note: Some of this options aren't finish and don't work.\n—————————————————————————————\n"
settings = []
for setting in self.guild_settings:
# is_anal has its own command for now but should be put into this menu when 2.0 hits.
if setting in ["warnings", "custom_commands", "is_anal"]:
pass
elif setting == "gss" and ctx.guild.id != 393764974444675073:
pass
else:
output += "[{}] Edit '{}' settings\n".format(x, setting)
x += 1
settings.append(setting)
output += "[{}] Exit\n".format(0)
x += 1
settings.append("exit")
return "```python\n" + output + "```", x, settings

@commands.group(case_insensitive=True) @commands.group(case_insensitive=True)
@checks.is_admin_or_mod() @checks.is_admin_or_mod()
async def settings(self, ctx): async def settings(self, ctx):
self.guild_settings = guild_settings.get(ctx.guild) self.guild_settings = guild_settings.get(ctx.guild)
if ctx.invoked_subcommand is None:
output, count, settings = self._make_settings_menu(ctx)
msg = await ctx.send(output)
def author_reply(m):
return m.author.id == ctx.author.id and ctx.channel.id == m.channel.id
try:
reply = await self.bot.wait_for("message", check=author_reply, timeout=40)
if 0 > int(reply.content) > count:
return await ctx.send("Option out of range. Exiting...")
else:
option = int(reply.content)
if settings[option] == "logging":
return await ctx.invoke(self.logging, msg=msg)
elif settings[option] == "gss":
return await ctx.invoke(self.gss, msg=msg)
elif settings[option] == "self_assign":
return await ctx.invoke(self.selfassign, msg=msg)
elif settings[option] == "is_anal":
return await ctx.invoke(self.serverisanal, msg=msg)
elif settings[option] == "twitch":
return await ctx.invoke(self.twitch, msg=msg)
elif settings[option] == "nsfw":
return await ctx.invoke(self.nsfw, msg=msg)
elif settings[option] == "perm_roles":
return await ctx.invoke(self.permrole, msg=msg)
elif settings[option] == "voice":
return await ctx.invoke(self.voice, msg=msg)
elif settings[option] == "greets":
return await ctx.invoke(self.joinleave, changes="greets", msg=msg)
elif settings[option] == "goodbyes":
return await ctx.invoke(self.joinleave, changes="goodbyes", msg=msg)
else:
await msg.delete()
return await ctx.send("Exiting...")
except ValueError:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")
except asyncio.TimeoutError:
await msg.delete()
raise commands.CommandError("Menu timed out. Exiting...")


@settings.command(aliases=["log"]) @settings.command(aliases=["log"])
async def logging(self, ctx, selection=None, *, changes=None, msg=None):
async def logging(self, ctx, selection=None, *, changes=None):
"""Edits the logging settings. """Edits the logging settings.


Options: Options:
enable/disable: Enable/disables logging. enable/disable: Enable/disables logging.
channel: sets the channel. channel: sets the channel.
""" """
# TODO: Optimise the menu system to be dynamic at some point
if selection is None:
output = """
```python
'Roxbot Settings: Logging'
—————————————————————————————
[0] Enable Logging
[1] Disable Logging
[2] Set Logging Channel
```
"""
if msg is None:
msg = await ctx.send(output)
else:
msg = await msg.edit(content=output)

def menu_check(m):
return ctx.author == m.author and ctx.channel == m.channel

try:
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
if response.content == "0":
selection = "enable"
elif response.content == "1":
selection = "disable"
elif response.content == "2":
selection = "channel"
output = """
```python
'Roxbot Settings: Logging Channel'
—————————————————————————————
What channel should the Logging Channel be set to?
```
"""
msg = await msg.edit(content=output)
res = await self.bot.wait_for("message", timeout=40, check=menu_check)
channel = self.get_channel(ctx, res.content)
if channel is False:
raise commands.BadArgument("Channel {} not found. Exiting...".format(res.content))
await msg.delete()
else:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")
except asyncio.TimeoutError:
await msg.delete()
raise commands.CommandError("Menu timed out. Exiting...")

selection = selection.lower() selection = selection.lower()
settings = guild_settings.get(ctx.guild) settings = guild_settings.get(ctx.guild)


return self.guild_settings.update(settings.logging, "logging") return self.guild_settings.update(settings.logging, "logging")


@settings.command(aliases=["sa"]) @settings.command(aliases=["sa"])
async def selfassign(self, ctx, selection=None, *, changes=None, msg=None):
async def selfassign(self, ctx, selection=None, *, changes=None):
"""Edits settings for self assign cog. """Edits settings for self assign cog.


Options: Options:
enable/disable: Enable/disables the cog. enable/disable: Enable/disables the cog.
addrole/removerole: adds or removes a role that can be self assigned in the server. addrole/removerole: adds or removes a role that can be self assigned in the server.
""" """
if selection is None:
output = """
```python
'Roxbot Settings: Self Assign'
—————————————————————————————
[1] Enable Self Assign
[2] Disable Self Assign
[3] Add a role to the Self Assign list
[4] Remove a role to the Self Assign list
[5] List all roles that can be self-assigned
```
"""
if msg is None:
msg = await ctx.send(output)
else:
msg = await msg.edit(content=output)

def menu_check(m):
return ctx.author == m.author and ctx.channel == m.channel

try:
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
if response.content == "1":
selection = "enable"
elif response.content == "2":
selection = "disable"
elif response.content == "3":
selection = "addrole"
output = """
```python
'Roxbot Settings: Self Assign - Add Role'
—————————————————————————————
What role do you want to make self-assignable?
```"""
elif response.content == "4":
selection = "removerole"
output = """
```python
'Roxbot Settings: Self Assign - Remove Role'
—————————————————————————————
What role do you want remove from the self-assignable list?
```"""
elif response.content == "5":
return await ctx.invoke(self.printsettings, option="self_assign")
else:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")

if selection in ["removerole", "addrole"]:
await msg.edit(content=output)
res = await self.bot.wait_for("message", timeout=40, check=menu_check)
role = discord.utils.get(ctx.guild.roles, name=res.content)
if role is None:
raise commands.BadArgument("Role {} not found. Exiting...".format(res.content))
await msg.delete()
except asyncio.TimeoutError:
await msg.delete()
raise commands.CommandError("Menu timed out. Exiting...")


else:
selection = selection.lower()
role = discord.utils.find(lambda u: u.name == changes, ctx.message.guild.roles)
selection = selection.lower()
role = discord.utils.find(lambda u: u.name == changes, ctx.message.guild.roles)


self_assign = self.guild_settings.self_assign self_assign = self.guild_settings.self_assign


return self.guild_settings.update(self_assign, "self_assign") return self.guild_settings.update(self_assign, "self_assign")


@settings.command(aliases=["jl"]) @settings.command(aliases=["jl"])
async def joinleave(self, ctx, selection=None, *, changes=None, msg=None):
async def joinleave(self, ctx, selection=None, *, changes=None):
"""Edits settings for joinleave cog. """Edits settings for joinleave cog.


Options: Options:
greetschannel/goodbyeschannel: Sets the channels for either option. Must be a ID or mention. greetschannel/goodbyeschannel: Sets the channels for either option. Must be a ID or mention.
custommessage: specifies a custom message for the greet messages. custommessage: specifies a custom message for the greet messages.
""" """
if selection is None:
def menu_check(m):
return ctx.author == m.author and ctx.channel == m.channel

if changes is None:
try:
output = """
```python
'Roxbot Settings: JoinLeave'
—————————————————————————————
[1] Edit Greets
[2] Edit Goodbyes
```
"""
if msg is None:
msg = await ctx.send(output)
else:
await msg.edit(content=output)
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
if response.content == "1":
changes = "greets"
elif response.content == "2":
changes = "goodbyes"
else:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")
except asyncio.TimeoutError:
await msg.delete()
raise commands.CommandError("Menu timed out. Exiting...")

if changes == "greets":
output = """
```python
'Roxbot Settings: JoinLeave - Greets'
—————————————————————————————
[1] Enable Greets
[2] Disable Greets
[3] Set Greets channel
[4] Add custom Greets message
```
"""
else: # The only other option is goodbyes due to how this command is structured.
output = """
```python
'Roxbot Settings: JoinLeave - Goodbyes'
—————————————————————————————
[1] Enable Goodbyes
[2] Disable Goodbyes
[3] Set Goodbyes channel
```
"""
await msg.edit(output)
try:
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
if response.content == "1":
selection = "enable"
elif response.content == "2":
selection = "disable"
elif response.content == "3" and changes == "greets":
selection = "greetschannel"
elif response.content == "3" and changes == "goodbyes":
selection = "goodbyeschannel"
elif response.content == "4" and changes == "greets":
selection = "custommessage"
else:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")
if response.content == "3":
output = """
```python
'Roxbot Settings: JoinLeave - {0}'
—————————————————————————————
What channel do you want to set as the {0} channel?
```
""".format(changes.title())
await msg.edit(content=output)
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
channel = self.get_channel(ctx, response.content)
elif response.content == "4":
output = """
```python
'Roxbot Settings: JoinLeave - Greets'
—————————————————————————————
What channel do you want to set as the custom greets message?
```
"""
await msg.edit(content=output)
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
changes = response.content
else:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")
except asyncio.TimeoutError:
await msg.delete()
raise commands.CommandError("Menu timed out. Exiting...")


selection = selection.lower() selection = selection.lower()
channel = self.get_channel(ctx, changes) channel = self.get_channel(ctx, changes)
return self.guild_settings.update(goodbyes, "goodbyes") return self.guild_settings.update(goodbyes, "goodbyes")


@settings.command() @settings.command()
async def twitch(self, ctx, selection=None, *, changes=None, msg=None):
async def twitch(self, ctx, selection=None, *, changes=None):
"""Edits settings for self assign cog. """Edits settings for self assign cog.


Options: Options:
return self.guild_settings.update(twitch, "twitch") return self.guild_settings.update(twitch, "twitch")


@settings.command(aliases=["perms"]) @settings.command(aliases=["perms"])
async def permrole(self, ctx, selection=None, *, changes=None, msg=None):
async def permrole(self, ctx, selection=None, *, changes=None):
"""Edits settings for permission roles. """Edits settings for permission roles.


Options: Options:
Example: Example:
;settings permrole addadmin Admin ;settings permrole addadmin Admin
""" """
if selection is None:
output = """
```python
'Roxbot Settings: Perm Roles'
—————————————————————————————
[1] Add Admin Role
[2] Remove Admin Role
[3] Add Mod Role
[4] Remove Mod Role
```
"""
if msg is None:
msg = await ctx.send(output)
else:
await msg.edit(content=output)

def menu_check(m):
return ctx.author == m.author and ctx.channel == m.channel

try:
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
if response.content == "1":
selection = "addadmin"
elif response.content == "2":
selection = "removeadmin"
elif response.content == "3":
selection = "addmod"
elif response.content == "4":
selection = "removemod"
else:
await msg.delete()
raise commands.BadArgument("Invalid index given for menu. Exiting...")
if response.content in ["1", "3"]:
output = """
```python
'Roxbot Settings: Perm Roles'
—————————————————————————————
What role do you want to add?
```
"""
await msg.edit(content=output)
response = await self.bot.wait_for("message", timeout=40, check=menu_check)
role = discord.utils.get(ctx.guild.roles, name=response.content)
if role is None:
raise commands.BadArgument("Role {} not found. Exiting...".format(response.content))
await msg.delete()
except asyncio.TimeoutError:
await msg.delete()
raise commands.CommandError("Menu timed out. Exiting...")


selection = selection.lower() selection = selection.lower()
role = discord.utils.find(lambda u: u.name == changes, ctx.message.guild.roles) role = discord.utils.find(lambda u: u.name == changes, ctx.message.guild.roles)
return self.guild_settings.update(perm_roles, "perm_roles") return self.guild_settings.update(perm_roles, "perm_roles")


@settings.command() @settings.command()
async def gss(self, ctx, selection=None, *, changes=None, msg=None):
async def gss(self, ctx, selection=None, *, changes=None):
"""Custom Cog for the GaySoundsShitposts Discord Server.""" """Custom Cog for the GaySoundsShitposts Discord Server."""
# TODO: Menu # TODO: Menu
selection = selection.lower() selection = selection.lower()
return await ctx.send("No valid option given.") return await ctx.send("No valid option given.")
return self.guild_settings.update(gss, "gss") return self.guild_settings.update(gss, "gss")



@settings.command() @settings.command()
async def nsfw(self, ctx, selection=None, *, changes=None, msg=None):
async def nsfw(self, ctx, selection=None, *, changes=None):
"""Edits settings for the nsfw cog and other nsfw commands. """Edits settings for the nsfw cog and other nsfw commands.
If nsfw is enabled and nsfw channels are added, the bot will only allow nsfw commands in the specified channels. If nsfw is enabled and nsfw channels are added, the bot will only allow nsfw commands in the specified channels.


selection = selection.lower() selection = selection.lower()
nsfw = self.guild_settings.nsfw nsfw = self.guild_settings.nsfw



if selection == "enable": if selection == "enable":
nsfw["enabled"] = 1 nsfw["enabled"] = 1
await ctx.send("'nsfw' was enabled!") await ctx.send("'nsfw' was enabled!")
return self.guild_settings.update(nsfw, "nsfw") return self.guild_settings.update(nsfw, "nsfw")


@settings.command() @settings.command()
async def voice(self, ctx, setting=None, change=None, msg=None):
async def voice(self, ctx, setting=None, change=None):
"""Edits settings for the voice cog. """Edits settings for the voice cog.
Options: Options:
enable/disable: Enable/disables specified change. enable/disable: Enable/disables specified change.
return await ctx.send("Not a valid change.") return await ctx.send("Not a valid change.")
elif setting == "skipratio": elif setting == "skipratio":
change = float(change) change = float(change)
if change < 1 and change > 0:
if 1 > change > 0:
voice["skip_ratio"] = change voice["skip_ratio"] = change
elif change > 0 and change <= 100:
elif 0 < change <= 100:
change = change/10 change = change/10
voice["skip_ratio"] = change voice["skip_ratio"] = change
else: else:




def setup(bot_client): def setup(bot_client):
bot_client.add_cog(Settings(bot_client))
bot_client.add_cog(Settings(bot_client))

Loading…
Cancel
Save