async def searchRole(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, Data: WebRequestContent) -> Response: search_term: str = Data.getStr("term", "") guild_id: str = Data.getStr("guild_id", "", must_be_digit=True) if not guild_id: return await apiMissingData(cls, WebRequest, msg="invalid or missing 'guild_id'") Guild: discord.Guild = discord.utils.get(cls.BASE.Discord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) Role: discord.Role = getDiscordRoleFromString(cls.BASE.Discord, Guild, search_term, contains=True) if not Role: return await apiDiscordRoleNotFound(cls, WebRequest) data: dict = { "name": str(Role.name), "id": str(Role.id), "color": Role.color.value, "managed": Role.managed, "position": Role.position } return cls.response(text=json.dumps(dict(result=data, status=200)), content_type="application/json", status=200)
async def searchMember(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, Data: WebRequestContent) -> Response: search_term: str = Data.getStr("term", "") guild_id: str = Data.getStr("guild_id", "", must_be_digit=True) if not guild_id: return await apiMissingData(cls, WebRequest, msg="invalid or missing 'guild_id'") Guild: discord.Guild = discord.utils.get(cls.BASE.Discord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) Member: discord.Member = getDiscordMemberFromString( cls.BASE.Discord, Guild, search_term) if not Member: return await apiDiscordMemberNotFound(cls, WebRequest) data: dict = { "name": str(Member.name), "nick": Member.nick, "id": str(Member.id), "discriminator": Member.discriminator, "avatar": Member.avatar } return cls.response(text=json.dumps(dict(result=data, status=200)), content_type="application/json", status=200)
async def singleActionEnableNSFWChannel(cls:"WebIndex", WebRequest:Request, action:str, Data:WebRequestContent, Configs:DiscordServerSettings, CurrentGuild:discord.Guild) -> Response: """ Default url: /api/discord/configs/edit?enabled_nsfwchan_action=something """ guild_id:str = Data.getStr("guild_id", "") action = action.lower() channel_id:str = Data.getStr("enabled_nsfwchan_id", "", must_be_digit=True) if not guild_id: # should never happen return await apiMissingData(cls, WebRequest, msg="missing field 'guild_id'") if not channel_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid field 'enabled_nsfwchan_id'") ActionChannel:discord.TextChannel = CurrentGuild.get_channel(int(channel_id)) if not ActionChannel and action == "add": return await apiDiscordChannelNotFound(cls, WebRequest, guild_name=CurrentGuild.name, guild_id=CurrentGuild.id, channel_id=channel_id) if action == "add": if str(ActionChannel.id) in Configs.enabled_nsfwchannels: return await apiWrongData(cls, WebRequest, msg=f"'{ActionChannel.name}' is already added") cls.Web.BASE.PhaazeDB.insertQuery( table = "discord_enabled_nsfwchannel", content = { "guild_id": guild_id, "channel_id": channel_id, } ) cls.Web.BASE.Logger.debug(f"(API/Discord) Enabled nsfw channel list Update: S:{guild_id} - add: {channel_id}", require="discord:configs") return cls.response( text=json.dumps( dict(msg="enabled nsfw channel list successfull updated", add=channel_id, status=200) ), content_type="application/json", status=200 ) elif action == "remove": if channel_id not in Configs.enabled_nsfwchannels: return await apiWrongData(cls, WebRequest, msg=f"can't remove '{channel_id}', is currently not added") cls.Web.BASE.PhaazeDB.deleteQuery(""" DELETE FROM `discord_enabled_nsfwchannel` WHERE `guild_id` = %s AND `channel_id` = %s""", (guild_id, channel_id) ) cls.Web.BASE.Logger.debug(f"(API/Discord) Enabled nsfw channel list Update: S:{guild_id} - rem: {channel_id}", require="discord:configs") return cls.response( text=json.dumps( dict(msg="enabled nsfw channel list successfull updated", remove=channel_id, status=200) ), content_type="application/json", status=200 ) else: return await apiWrongData(cls, WebRequest)
async def singleActionExceptionRole(cls:"WebIndex", WebRequest:Request, action:str, Data:WebRequestContent, Configs:DiscordServerSettings, CurrentGuild:discord.Guild) -> Response: """ Default url: /api/discord/configs/edit?exceptionrole_action=something """ guild_id:str = Data.getStr("guild_id", "") action = action.lower() role_id:str = Data.getStr("exceptionrole_id", "", must_be_digit=True) if not guild_id: # should never happen return await apiMissingData(cls, WebRequest, msg="missing field 'guild_id'") if not role_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid field 'exceptionrole_id'") ActionRole:discord.Role = CurrentGuild.get_role(int(role_id)) if not ActionRole and action == "add": return await apiDiscordRoleNotFound(cls, WebRequest, guild_id=CurrentGuild.id, guild_name=CurrentGuild.name, role_id=role_id) if action == "add": if str(ActionRole.id) in Configs.blacklist_whitelistroles: return await apiWrongData(cls, WebRequest, msg=f"'{ActionRole.name}' is already added") cls.Web.BASE.PhaazeDB.insertQuery( table = "discord_blacklist_whitelistrole", content = { "guild_id": guild_id, "role_id": role_id } ) cls.Web.BASE.Logger.debug(f"(API/Discord) Exception role list Update: S:{guild_id} - add: {role_id}", require="discord:configs") return cls.response( text=json.dumps( dict(msg="exception role list successfull updated", add=role_id, status=200) ), content_type="application/json", status=200 ) elif action == "remove": if role_id not in Configs.blacklist_whitelistroles: return await apiWrongData(cls, WebRequest, msg=f"can't remove '{role_id}', is currently not added") cls.Web.BASE.PhaazeDB.deleteQuery(""" DELETE FROM `discord_blacklist_whitelistrole` WHERE `guild_id` = %s AND `role_id` = %s""", (guild_id, role_id) ) cls.Web.BASE.Logger.debug(f"(API/Discord) Exception role list Update: S:{guild_id} - rem: {role_id}", require="discord:configs") return cls.response( text=json.dumps( dict(msg="exception role list successfull updated", remove=role_id, status=200) ), content_type="application/json", status=200 ) else: return await apiWrongData(cls, WebRequest)
async def singleActionLinkWhitelist(cls:"WebIndex", WebRequest:Request, action:str, Data:WebRequestContent, Configs:DiscordServerSettings) -> Response: """ Default url: /api/discord/configs/edit?linkwhitelist_action=something """ guild_id:str = Data.getStr("guild_id", "") action = action.lower() action_link:str = Data.getStr("linkwhitelist_link", "").replace(";;;", "") # ;;; is the sql sepperator if not guild_id: # should never happen return await apiMissingData(cls, WebRequest, msg="missing field 'guild_id'") if not action_link: return await apiMissingData(cls, WebRequest, msg="missing field 'linkwhitelist_link'") if action == "add": cls.Web.BASE.PhaazeDB.insertQuery( table = "discord_blacklist_whitelistlink", content = { "link": action_link, "guild_id": guild_id } ) cls.Web.BASE.Logger.debug(f"(API/Discord) Link Whitelist Update: S:{guild_id} - add: {action_link}", require="discord:configs") return cls.response( text=json.dumps( dict(msg="link whitelist successfull updated", add=action_link, status=200) ), content_type="application/json", status=200 ) elif action == "remove": if action_link not in Configs.blacklist_whitelistlinks: return await apiWrongData(cls, WebRequest, msg=f"can't remove '{action_link}', it's currently not in the link whitelist") cls.Web.BASE.PhaazeDB.deleteQuery(""" DELETE FROM `discord_blacklist_whitelistlink` WHERE `guild_id` = %s AND `link` = %s""", (guild_id, action_link) ) cls.Web.BASE.Logger.debug(f"(API/Discord) Link Whitelist Update: S:{guild_id} - rem: {action_link}", require="discord:configs") return cls.response( text=json.dumps( dict(msg="link whitelist successfull updated", remove=action_link, status=200) ), content_type="application/json", status=200 ) else: return await apiWrongData(cls, WebRequest)
async def searchChannel(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, Data: WebRequestContent) -> Response: search_term: str = Data.getStr("term", "") guild_id: str = Data.getStr("guild_id", "", must_be_digit=True) if not guild_id: return await apiMissingData(cls, WebRequest, msg="invalid or missing 'guild_id'") Guild: discord.Guild = discord.utils.get(cls.BASE.Discord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) Channel: Union[discord.TextChannel, discord.VoiceChannel, discord.CategoryChannel] = getDiscordChannelFromString( cls.BASE.Discord, Guild, search_term, contains=True) if not Channel: return await apiDiscordChannelNotFound(cls, WebRequest) data: dict = { "name": str(Channel.name), "id": str(Channel.id), "position": Channel.position, } if type(Channel) is discord.TextChannel: data["channel_type"] = "text" elif type(Channel) is discord.VoiceChannel: data["channel_type"] = "voice" elif type(Channel) is discord.CategoryChannel: data["channel_type"] = "category" else: data["channel_type"] = "unknown" return cls.response(text=json.dumps(dict(result=data, status=200)), content_type="application/json", status=200)
async def searchGuild(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, Data: WebRequestContent) -> Response: search_term: str = Data.getStr("term", "") Guild: discord.Guild = getDiscordGuildFromString(cls.BASE.Discord, search_term, contains=True) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) data: dict = { "name": str(Guild.name), "id": str(Guild.id), "owner_id": str(Guild.owner_id), "icon": Guild.icon, "banner": Guild.banner } return cls.response(text=json.dumps(dict(result=data, status=200)), content_type="application/json", status=200)
async def searchChannel(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, Data: WebRequestContent) -> Response: search_term: str = Data.getStr("term", "", len_max=128) res: List[TwitchUser] = await getTwitchUsers(cls.BASE, item=search_term, item_type="login") if not res: return await apiNotFound(cls, WebRequest) else: SearchUser: TwitchUser = res.pop(0) data: dict = { "id": str(SearchUser.user_id), "display_name": str(SearchUser.display_name), "login": str(SearchUser.login), } return cls.response(text=json.dumps(dict(result=data, status=200)), content_type="application/json", status=200)
async def singleActionUserRole(cls: "WebIndex", WebRequest: Request, action: str, Data: WebRequestContent) -> Response: """ Default url: /api/admin/users/edit?userrole_action=something """ user_id: str = Data.getStr("user_id", "") action = action.lower() userrole_role: str = Data.getStr("userrole_role", "") if not user_id: # should never happen return await apiMissingData(cls, WebRequest, msg="missing field 'user_id'") if not userrole_role: return await apiMissingData( cls, WebRequest, msg="missing or invalid field 'userrole_role'") # if it's not a number, try to get id from name, else... just select it again... REEEE res: list = cls.Web.BASE.PhaazeDB.selectQuery( """ SELECT * FROM `role` WHERE LOWER(`role`.`name`) = LOWER(%s) OR `role`.`id` = %s""", (userrole_role, userrole_role)) if not res: return await apiWrongData( cls, WebRequest, msg=f"role '{userrole_role}' could not be resolved as a role") # here we hope there is only one result, in theory there can be more, BUT since this is a admin endpoint, # i wont will do much error handling Role: WebRole = WebRole(res.pop(0)) # prevent role missabuse if Role.name.lower() in ["superadmin", "admin"]: if not (await cls.getWebUserInfo(WebRequest)).checkRoles( ["superadmin"]): return await apiNotAllowed( cls, WebRequest, msg=f"Only Superadmin's can assign/remove '{Role.name}' to user" ) if action == "add": try: cls.Web.BASE.PhaazeDB.insertQuery(table="user_has_role", content=dict( user_id=user_id, role_id=Role.role_id)) return cls.response(text=json.dumps( dict(msg="user roles successfull updated", add=Role.name, status=200)), content_type="application/json", status=200) except: return await apiWrongData( cls, WebRequest, msg=f"user already has role: '{Role.name}'") elif action == "remove": try: cls.Web.BASE.PhaazeDB.deleteQuery( """ DELETE FROM `user_has_role` WHERE `role_id` = %s AND `user_id` = %s""", (Role.role_id, user_id)) return cls.response(text=json.dumps( dict(msg="user roles successfull updated", rem=Role.name, status=200)), content_type="application/json", status=200) except: return await apiWrongData( cls, WebRequest, msg=f"user don't has role: '{Role.name}'") else: return await apiWrongData(cls, WebRequest)
async def singleActionMedal(cls: "WebIndex", WebRequest: Request, action: str, Data: WebRequestContent, CurrentLevelUser: DiscordUserStats) -> Response: """ Default url: /api/discord/levels/edit?medal_action=something """ guild_id: str = Data.getStr("guild_id", "") member_id: str = Data.getStr("member_id", "") action = action.lower() medal_name: str = Data.getStr("medal_name", "").strip(" ").strip("\n") if not guild_id: # should never happen return await apiMissingData(cls, WebRequest, msg="missing or invalid 'guild_id'") if not member_id: # should never happen return await apiMissingData(cls, WebRequest, msg="missing or invalid 'member_id'") if not medal_name: return await apiMissingData(cls, WebRequest, msg="missing 'medal_name'") if action == "add": if medal_name in CurrentLevelUser.medals: return await apiWrongData(cls, WebRequest, msg=f"'{medal_name}' is already added") if len(CurrentLevelUser.medals ) >= cls.Web.BASE.Limit.DISCORD_LEVEL_MEDAL_AMOUNT: return await apiDiscordLevelMedalLimit(cls, WebRequest) cls.Web.BASE.PhaazeDB.insertQuery(table="discord_user_medal", content={ "guild_id": guild_id, "member_id": member_id, "name": medal_name }) cls.Web.BASE.Logger.debug( f"(API/Discord) Level Medal Update: S:{guild_id} M:{member_id} - add: {medal_name}", require="discord:level") return cls.response(text=json.dumps( dict(msg="level medals update successfull updated", add=medal_name, status=200)), content_type="application/json", status=200) elif action == "remove": if medal_name not in CurrentLevelUser.medals: return await apiWrongData( cls, WebRequest, msg=f"can't remove '{medal_name}', is currently not added") cls.Web.BASE.PhaazeDB.deleteQuery( """ DELETE FROM `discord_user_medal` WHERE `guild_id` = %s AND `member_id` = %s AND `name` = %s""", (guild_id, member_id, medal_name)) cls.Web.BASE.Logger.debug( f"(API/Discord) Level Medal Update: S:{guild_id} M:{member_id} - rem: {medal_name}", require="discord:level") return cls.response(text=json.dumps( dict(msg="level medals update successfull updated", rem=medal_name, status=200)), content_type="application/json", status=200) else: return await apiWrongData(cls, WebRequest)