From 9f9b9c68d5f2ab28a7aba229e2885be50785a279 Mon Sep 17 00:00:00 2001 From: versx Date: Sat, 13 Mar 2021 16:05:19 -0800 Subject: [PATCH] Remove cityRoles and just use geofence names as assignable roles if enabled --- src/config.example.json | 2 -- src/data/map.js | 16 ---------------- src/routes/api.js | 26 +++++--------------------- src/routes/ui.js | 2 +- src/services/discord.js | 10 +++++----- 5 files changed, 11 insertions(+), 45 deletions(-) diff --git a/src/config.example.json b/src/config.example.json index 2928547..c86bf46 100644 --- a/src/config.example.json +++ b/src/config.example.json @@ -30,7 +30,6 @@ "roles": [ "000000000000000000" ], - "cityRoles": [], "geofences": [] }, { @@ -39,7 +38,6 @@ "roles": [ "000000000000000000" ], - "cityRoles": [], "geofences": [] } ], diff --git a/src/data/map.js b/src/data/map.js index 3fc0a23..35ed043 100644 --- a/src/data/map.js +++ b/src/data/map.js @@ -32,21 +32,6 @@ const buildCityList = (guilds) => { return cities; }; -const buildCityRoleList = (guilds) => { - const roles = []; - const configGuilds = config.discord.guilds; - for (let i = 0; i < configGuilds.length; i++) { - const configGuild = configGuilds[i]; - if (guilds.includes(configGuild.id)) { - (configGuild.cityRoles || []).forEach(cityRole => roles.push({ - 'name': cityRole, - 'guild': configGuild.id, - })); - } - } - return roles; -}; - const getLureTypes = () => { return [ { name: 'Normal' }, @@ -59,6 +44,5 @@ const getLureTypes = () => { module.exports = { getPokemonNameIdsList, buildCityList, - buildCityRoleList, getLureTypes, }; \ No newline at end of file diff --git a/src/routes/api.js b/src/routes/api.js index b89c3fc..fd870da 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -987,12 +987,12 @@ router.post('/lures/delete_all', async (req, res) => { router.post('/role/add', async (req, res) => { const { guild_id, roles } = req.body; const user_id = req.session.user_id; - const cityRoles = getRoles(guild_id, roles); + const areas = getAreas(guild_id, roles); let error = false; - for (const cityRole of cityRoles) { - const result = await DiscordClient.addRole(guild_id, user_id, cityRole); + for (const area of areas) { + const result = await DiscordClient.addRole(guild_id, user_id, area); if (!result) { - console.error('Failed to assign city role', cityRole, 'to guild', guild_id, 'user', user_id); + console.error('Failed to assign city role', area, 'to guild', guild_id, 'user', user_id); error = true; } } @@ -1024,7 +1024,7 @@ router.post('/roles/remove_all', async (req, res) => { showError(res, 'roles-remove-all', `Failed to find guild ${guild_id} to remove all city roles for user ${userId}`); return; } - const roles = guild.cityRoles; + const roles = guild.geofences; const result = await DiscordClient.removeAllRoles(guild_id, userId, roles); if (!result) { // Failed to remove all city roles @@ -1113,22 +1113,6 @@ const ellipsis = (str) => { return value === str ? value : value + '...'; }; -const getRoles = (guildId, cityName) => { - let areas; - if (cityName === 'all' || cityName.includes('all')) { - config.discord.guilds.map(x => { - if (x.id === guildId) { - areas = x.cityRoles; - } - }); - } else if (!Array.isArray(cityName)) { - areas = [cityName]; - } else { - areas = cityName; - } - return areas || []; -}; - const showError = (res, page, message) => { console.error(message); const errorData = { ...defaultData }; diff --git a/src/routes/ui.js b/src/routes/ui.js index 1796efa..fd07964 100644 --- a/src/routes/ui.js +++ b/src/routes/ui.js @@ -395,7 +395,7 @@ if (config.enableGeofenceRoles) { router.get('/role/add', (req, res) => { const data = { ...defaultData }; data.servers = validateRoles(req, res); - data.roles = map.buildCityRoleList(req.session.guilds); + data.roles = map.buildCityList(req.session.guilds); res.render('role-add', data); }); diff --git a/src/services/discord.js b/src/services/discord.js index dab2cf3..1824c77 100644 --- a/src/services/discord.js +++ b/src/services/discord.js @@ -122,7 +122,7 @@ class DiscordClient { } } - static async removeAllRoles(guildId, userId, cityRoles) { + static async removeAllRoles(guildId, userId, areas) { const guild = await this.getGuild(guildId); if (!guild) { console.error('Failed to find guild by id', guildId); @@ -134,14 +134,14 @@ class DiscordClient { return false; } try { - for (const cityRole of cityRoles) { - const role = this.getRoleByName(guild, cityRole); + for (const area of areas) { + const role = this.getRoleByName(guild, area); if (!role) { - console.error('Failed to find role by name', cityRole); + console.error('Failed to find role by name', area); continue; } await member.roles.remove(role); - console.info('Removed city role', cityRole, 'from user', member.user.username); + console.info('Removed city role', area, 'from user', member.user.username); } return true; } catch (err) {