Example #1
0
async def apiDiscordRegularsCreate(cls: "PhaazebotWeb",
                                   WebRequest: ExtendedRequest) -> Response:
    """
	Default url: /api/discord/regulars/create
	"""
    Data: WebRequestContent = WebRequestContent(WebRequest)
    await Data.load()

    # get required stuff
    Create: StorageTransformer = StorageTransformer()
    Create["guild_id"] = Data.getStr("guild_id", UNDEFINED, must_be_digit=True)
    Create["member_id"] = Data.getStr("member_id",
                                      UNDEFINED,
                                      must_be_digit=True)

    # checks
    if not Create["guild_id"]:
        return await cls.Tree.Api.errors.apiMissingData(
            cls, WebRequest, msg="missing or invalid 'guild_id'")

    if not Create["member_id"]:
        return await cls.Tree.Api.errors.apiMissingData(
            cls, WebRequest, msg="missing or invalid 'member_id'")

    # get/check discord
    PhaazeDiscord: "PhaazebotDiscord" = cls.BASE.Discord
    Guild: discord.Guild = discord.utils.get(PhaazeDiscord.guilds,
                                             id=int(Create["guild_id"]))
    if not Guild:
        return await cls.Tree.Api.Discord.errors.apiDiscordGuildUnknown(
            cls, WebRequest)

    ActionMember: discord.Member = getDiscordMemberFromString(
        PhaazeDiscord, Guild, Create["member_id"])
    if not ActionMember:
        return await cls.Tree.Api.Discord.errors.apiDiscordMemberNotFound(
            cls, WebRequest, user_id=Create["member_id"])

    # check if already exists and limits
    res: list = cls.BASE.PhaazeDB.selectQuery(
        """
		SELECT
			COUNT(*) AS `all`,
			SUM(
				CASE WHEN `discord_regular`.`member_id` = %s
				THEN 1 ELSE 0 END
			) AS `match`
		FROM `discord_regular`
		WHERE `discord_regular`.`guild_id` = %s""",
        (str(ActionMember.id), str(ActionMember.guild.id)))

    if res[0]["match"]:
        return await cls.Tree.Api.Discord.Regulars.errors.apiDiscordRegularExists(
            cls, WebRequest)

    if res[0]["all"] >= cls.BASE.Limit.discord_regular_amount:
        return await cls.Tree.Api.Discord.Regulars.errors.apiDiscordRegularLimit(
            cls, WebRequest)

    # get user info
    AuthDiscord: AuthDiscordWebUser = await authDiscordWebUser(cls, WebRequest)
    if not AuthDiscord.found:
        return await cls.Tree.Api.errors.apiMissingAuthorisation(
            cls, WebRequest)

    # get member
    CheckMember: discord.Member = Guild.get_member(
        int(AuthDiscord.User.user_id))
    if not CheckMember:
        return await cls.Tree.Api.Discord.errors.apiDiscordMemberNotFound(
            cls,
            WebRequest,
            guild_id=Create["guild_id"],
            user_id=AuthDiscord.User.user_id)

    # check permissions
    if not (CheckMember.guild_permissions.administrator
            or CheckMember.guild_permissions.manage_guild):
        return await cls.Tree.Api.Discord.errors.apiDiscordMissingPermission(
            cls,
            WebRequest,
            guild_id=Create["guild_id"],
            user_id=AuthDiscord.User.user_id)

    cls.BASE.PhaazeDB.insertQuery(table="discord_regular",
                                  content={
                                      "guild_id": str(ActionMember.guild.id),
                                      "member_id": str(ActionMember.id)
                                  })

    # logging
    GuildSettings: DiscordServerSettings = await getDiscordSeverSettings(
        PhaazeDiscord, Create["guild_id"], prevent_new=True)
    log_coro: Coroutine = loggingOnRegularCreate(PhaazeDiscord,
                                                 GuildSettings,
                                                 Creator=CheckMember,
                                                 NewRegular=ActionMember)
    asyncio.ensure_future(log_coro, loop=cls.BASE.DiscordLoop)

    cls.BASE.Logger.debug(
        f"(API/Discord) Regular: {Create['guild_id']=} added new entry {Create['member_id']=}",
        require="discord:regulars")
    return cls.response(text=json.dumps(
        dict(msg="Regulars: Added new entry",
             entry=ActionMember.name,
             status=200)),
                        content_type="application/json",
                        status=200)
Example #2
0
async def apiDiscordCommandsEdit(cls:"PhaazebotWeb", WebRequest:ExtendedRequest) -> Response:
	"""
	Default url: /api/discord/commands/edit
	"""
	Data:WebRequestContent = WebRequestContent(WebRequest)
	await Data.load()

	# get required stuff
	Edit:StorageTransformer = StorageTransformer()
	Edit["command_id"] = Data.getInt("command_id", "", min_x=1)
	Edit["guild_id"] = Data.getStr("guild_id", "", must_be_digit=True)

	# checks
	if not Edit["command_id"]:
		return await cls.Tree.Api.errors.apiMissingData(cls, WebRequest, msg="missing or invalid 'command_id'")

	if not Edit["guild_id"]:
		return await cls.Tree.Api.errors.apiMissingData(cls, WebRequest, msg="missing or invalid 'guild_id'")

	# get/check discord
	PhaazeDiscord:"PhaazebotDiscord" = cls.BASE.Discord
	Guild:discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(Edit["guild_id"]))
	if not Guild:
		return await cls.Tree.Api.Discord.errors.apiDiscordGuildUnknown(cls, WebRequest)

	# check if exists
	res_commands:list = await getDiscordServerCommands(PhaazeDiscord, guild_id=Edit["guild_id"], command_id=Edit["command_id"], active=None)
	if not res_commands:
		return await cls.Tree.Api.Discord.Commands.errors.apiDiscordCommandNotExists(cls, WebRequest, command_id=Edit["command_id"])

	CurrentEditCommand:DiscordCommand = res_commands.pop(0)

	# after this point we have a existing command
	# check all possible values if it should be edited
	update:dict = dict()

	# active
	Edit["active"] = Data.getBool("active", UNDEFINED)
	if Edit["active"] != UNDEFINED:
		update["active"] = Edit["active"]

	# trigger
	Edit["trigger"] = Data.getStr("trigger", "", len_max=128).lower().split(" ")[0]
	if Edit["trigger"]:
		# try to get command with this trigger
		check_double_trigger:list = await getDiscordServerCommands(PhaazeDiscord, guild_id=Edit["guild_id"], trigger=Edit["trigger"])
		if check_double_trigger:
			CommandToCheck:DiscordCommand = check_double_trigger.pop(0)
			# tried to set a trigger twice
			if str(CommandToCheck.command_id) != str(CurrentEditCommand.command_id):
				return await cls.Tree.Api.Discord.Commands.errors.apiDiscordCommandExists(cls, WebRequest, trigger=Edit["trigger"])

		update["trigger"] = Edit["trigger"]

	# cooldown
	Edit["cooldown"] = Data.getInt("cooldown", UNDEFINED, min_x=0)
	if Edit["cooldown"] != UNDEFINED:
		# wants a invalid cooldown
		if not (cls.BASE.Limit.discord_commands_cooldown_min <= Edit["cooldown"] <= cls.BASE.Limit.discord_commands_cooldown_max):
			return await cls.Tree.Api.errors.apiWrongData(cls, WebRequest, msg="'cooldown' is wrong")

		update["cooldown"] = Edit["cooldown"]

	# currency
	Edit["required_currency"] = Data.getInt("required_currency", UNDEFINED, min_x=0)
	if Edit["required_currency"] != UNDEFINED:
		update["required_currency"] = Edit["required_currency"]

	# require
	Edit["require"] = Data.getInt("require", UNDEFINED, min_x=0)
	if Edit["require"] != UNDEFINED:
		update["require"] = Edit["require"]

	# content
	Edit["content"] = Data.getStr("content", UNDEFINED, len_max=1750)
	if Edit["content"] != UNDEFINED:
		update["content"] = Edit["content"]

	# hidden
	Edit["hidden"] = Data.getBool("hidden", UNDEFINED)
	if Edit["hidden"] != UNDEFINED:
		update["hidden"] = Edit["hidden"]

	# function (and type)
	Edit["complex"] = Data.getBool("complex", False)
	Edit["function"] = Data.getStr("function", UNDEFINED, len_max=256)
	if not Edit["complex"] and Edit["function"] != UNDEFINED:
		# it its not complex, we need a function
		if Edit["function"] not in [cmd["function"].__name__ for cmd in command_register]:
			return await cls.Tree.Api.errors.apiWrongData(cls, WebRequest, msg=f"'{Edit['function']}' is not a valid value for field 'function'")

		update["complex"] = Edit["complex"]
		update["function"] = Edit["function"]

	if Edit["complex"] and not Edit["function"]:
		# it is complex, we need a content
		if not update.get("content", ""):
			return await cls.Tree.Api.errors.apiWrongData(cls, WebRequest, msg=f"'complex' is true, but missing 'content'")

		update["complex"] = Edit["complex"]
		update["function"] = ""

	# get user info
	AuthDiscord:AuthDiscordWebUser = await authDiscordWebUser(cls, WebRequest)
	if not AuthDiscord.found:
		return await cls.Tree.Api.errors.apiMissingAuthorisation(cls, WebRequest)

	# get member
	CheckMember:discord.Member = Guild.get_member(int(AuthDiscord.User.user_id))
	if not CheckMember:
		return await cls.Tree.Api.Discord.errors.apiDiscordMemberNotFound(cls, WebRequest, guild_id=Edit["guild_id"], user_id=AuthDiscord.User.user_id)

	# check permissions
	if not (CheckMember.guild_permissions.administrator or CheckMember.guild_permissions.manage_guild):
		return await cls.Tree.Api.Discord.errors.apiDiscordMissingPermission(cls, WebRequest, guild_id=Edit["guild_id"], user_id=AuthDiscord.User.user_id)

	if not update:
		return await cls.Tree.Api.errors.apiWrongData(cls, WebRequest, msg=f"No changes, please add at least one")

	cls.BASE.PhaazeDB.updateQuery(
		table="discord_command",
		content=update,
		where="`discord_command`.`guild_id` = %s AND `discord_command`.`id` = %s",
		where_values=(CurrentEditCommand.server_id, CurrentEditCommand.command_id)
	)

	# logging
	GuildSettings:DiscordServerSettings = await getDiscordSeverSettings(PhaazeDiscord, Edit["guild_id"], prevent_new=True)
	log_coro:Coroutine = loggingOnCommandEdit(PhaazeDiscord, GuildSettings, Editor=CheckMember, command_trigger=CurrentEditCommand.trigger, command_info=update)
	asyncio.ensure_future(log_coro, loop=cls.BASE.DiscordLoop)

	cls.BASE.Logger.debug(f"(API/Discord) Commands: {Edit['guild_id']=} edited {Edit['command_id']=}", require="discord:commands")
	return cls.response(
		text=json.dumps(dict(msg="Commands: Edited entry", changes=update, status=200)),
		content_type="application/json",
		status=200
	)
Example #3
0
async def apiDiscordLevelsEdit(cls: "PhaazebotWeb",
                               WebRequest: ExtendedRequest) -> Response:
    """
	Default url: /api/discord/levels/edit
	"""
    Data: WebRequestContent = WebRequestContent(WebRequest)
    await Data.load()

    # get required stuff
    Edit: StorageTransformer = StorageTransformer()
    Edit["guild_id"] = Data.getStr("guild_id", "", must_be_digit=True)
    Edit["member_id"] = Data.getStr("member_id", "", must_be_digit=True)

    # checks
    if not Edit["guild_id"]:
        return await cls.Tree.Api.errors.apiMissingData(
            cls, WebRequest, msg="missing or invalid 'guild_id'")

    if not Edit["member_id"]:
        return await cls.Tree.Api.errors.apiMissingData(
            cls, WebRequest, msg="missing or invalid 'member_id'")

    PhaazeDiscord: "PhaazebotDiscord" = cls.BASE.Discord
    Guild: discord.Guild = discord.utils.get(PhaazeDiscord.guilds,
                                             id=int(Edit["guild_id"]))
    if not Guild:
        return await cls.Tree.Api.Discord.errors.apiDiscordGuildUnknown(
            cls, WebRequest)

    # get user info
    AuthDiscord: AuthDiscordWebUser = await authDiscordWebUser(cls, WebRequest)
    if not AuthDiscord.found:
        return await cls.Tree.Api.Discord.errors.apiMissingAuthorisation(
            cls, WebRequest)

    # get member
    CheckMember: discord.Member = Guild.get_member(
        int(AuthDiscord.User.user_id))
    if not CheckMember:
        return await cls.Tree.Api.Discord.errors.apiDiscordMemberNotFound(
            cls,
            WebRequest,
            guild_id=Edit["guild_id"],
            user_id=AuthDiscord.User.user_id)

    # check permissions
    # to edit configs, at least moderator rights are needed, (there can be options that require server only duh)
    if not (CheckMember.guild_permissions.administrator
            or CheckMember.guild_permissions.manage_guild):
        return await cls.Tree.Api.Discord.errors.apiDiscordMissingPermission(
            cls,
            WebRequest,
            guild_id=Edit["guild_id"],
            user_id=AuthDiscord.User.user_id)

    # get level user
    res_level: list = await getDiscordServerUsers(PhaazeDiscord,
                                                  guild_id=Edit["guild_id"],
                                                  member_id=Edit["member_id"])
    if not res_level:
        return await cls.Tree.Api.Discord.errors.apiDiscordGuildUnknown(
            cls, WebRequest, msg="Could not find a level for this user")
    CurrentLevelUser: DiscordUserStats = res_level.pop(0)

    # check all update values
    update: dict = dict()

    # currency
    Edit["currency"] = Data.getInt("currency", UNDEFINED, min_x=0)
    if Edit["currency"] != UNDEFINED:
        update["currency"] = Edit["currency"]

    # exp
    Edit["exp"] = Data.getInt("exp", UNDEFINED, min_x=0)
    if Edit["exp"] != UNDEFINED:
        update["exp"] = Edit["exp"]

        if Edit["exp"] != 0:
            update["edited"] = 1
        else:
            update["edited"] = 0

    # on_server
    Edit["on_server"] = Data.getBool("on_server", UNDEFINED)
    if Edit["on_server"] != UNDEFINED:
        if not Guild.owner == CheckMember:
            return await cls.Tree.Api.Discord.errors.apiDiscordMissingPermission(
                cls,
                WebRequest,
                guild_id=Edit["guild_id"],
                user_id=AuthDiscord.User.user_id,
                msg="changing 'on_server' require server owner")

        update["on_server"] = Edit["on_server"]

    if not update:
        return await cls.Tree.Api.errors.apiMissingData(
            cls, WebRequest, msg="No changes, please add at least one")

    cls.BASE.PhaazeDB.updateQuery(
        table="discord_user",
        content=update,
        where=
        "`discord_user`.`guild_id` = %s AND `discord_user`.`member_id` = %s",
        where_values=(CurrentLevelUser.guild_id, CurrentLevelUser.member_id))

    # logging
    GuildSettings: DiscordServerSettings = await getDiscordSeverSettings(
        PhaazeDiscord, Edit["guild_id"], prevent_new=True)
    log_coro: Coroutine = loggingOnLevelEdit(
        PhaazeDiscord,
        GuildSettings,
        Editor=CheckMember,
        changed_member_id=CurrentLevelUser.member_id,
        changes=update)
    asyncio.ensure_future(log_coro, loop=cls.BASE.DiscordLoop)

    cls.BASE.Logger.debug(
        f"(API/Discord) Level: {Edit['guild_id']=} {Edit['member_id']=} updated",
        require="discord:levels")
    return cls.response(text=json.dumps(
        dict(msg="Level: Updated", changes=update, status=200)),
                        content_type="application/json",
                        status=200)