Geoffrey-Bot/GeoffreyBot/geoffrey_mod_cmds.py

108 lines
4.0 KiB
Python

from discord.ext import commands
from GeoffreyBot.DiscordHelperFunctions import *
from GeoffreyBot.util import run_command, HandledError
class GeoffreyModCommands(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.api_url = bot.api_url
self.api_token = bot.api_token
# Check if the user has permission to use mod commands
async def cog_check(self, ctx):
if not hasattr(ctx.author, "roles"):
return False
mod_ranks = self.bot.mod_ranks
for role in ctx.author.roles:
for mod_rank in mod_ranks:
if role.id == mod_rank:
return True
return False
@commands.command(pass_context=True)
async def mod_delete(self, ctx, discord_uuid, *args):
"""
{}mod_delete <Location Name>
"""
name = get_name(args)
errors = {
"LocationLookUpError": "That user does not have a location by the name **{}**.".format(name)
}
location = await run_command(ctx, self.api_url, self.api_token, "POST", "delete", errors=errors, name=name,
discord_uuid=discord_uuid)
await ctx.send("{}, **{}** has been deleted from Geoffrey".format(ctx.message.author.mention, location))
@commands.command(pass_context=True)
async def mod_delete_item(self, ctx, discord_uuid, item_name: str, *args):
"""
{}mod_delete_item <Item Name> <Shop Name>
The Shop Name parameter is optional if you only have one location.
"""
shop_name = get_name(args)
errors = {
"LocationLookUpError": "no shop can be found by that name for the user.",
"EntryNameNotUniqueError": "Please specify the location name".format(item_name)
}
shop = await run_command(ctx, self.api_url, self.api_token, "POST", "delete_item", errors=errors,
item=item_name,
shop_name=shop_name, discord_uuid=discord_uuid)
await ctx.send("{}, **{}** has been deleted from {}.".format(
ctx.message.author.mention, item_name, shop["name"]))
@commands.command(pass_conext=True)
async def mod_edit_name(self, ctx, discord_uuid, new_name: str, old_name: str):
"""
{}mod_edit_name <New Name> <Old Name>
If the name has spaces in it, it must be wrapped in quotes. eg "Cool Shop 123"
"""
errors = {
"EntryNameNotUniqueError": "a location is already called **{}**".format(old_name),
"LocationLookUpError": "That location can not be found**{}**".format(
old_name)
}
location = await run_command(ctx, self.api_url, self.api_token, "POST", "edit_name", errors=errors,
loc_name=old_name,
new_name=new_name, discord_uuid=discord_uuid)
await ctx.send("{}, **{}** has been renamed to **{}**.".format(ctx.message.author.mention, old_name,
location["name"]))
@commands.command(pass_context=True)
async def mod_remove_resident(self, ctx, discord_uuid, resident_name, *args):
"""
{}remove_resident <Resident Name> <Town Name>
The Town Name is optional if you only have one town.
"""
town_name = get_name(args)
errors = {
"ResidentNotFoundError": "{} is not in that town".format(resident_name),
"LocationLookUpError": "Town not found.".format(town_name)
}
location = await run_command(ctx, self.api_url, self.api_token, "POST", "remove_resident", errors=errors,
resident_name=resident_name,
town_name=town_name, discord_uuid=discord_uuid)
await ctx.send('{}, **{}** has been remove as a resident of **{}**'.format(
ctx.message.author.mention, resident_name, location["name"]))
def setup(bot):
bot.add_cog(GeoffreyModCommands(bot))