Skip to content

Commit

Permalink
Merge pull request #28 from versx/geofences-only-roles
Browse files Browse the repository at this point in the history
Remove cityRoles & use geofence names as assignable roles
  • Loading branch information
versx authored Mar 14, 2021
2 parents 4550707 + 9f9b9c6 commit f22ff78
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 45 deletions.
2 changes: 0 additions & 2 deletions src/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"roles": [
"000000000000000000"
],
"cityRoles": [],
"geofences": []
},
{
Expand All @@ -39,7 +38,6 @@
"roles": [
"000000000000000000"
],
"cityRoles": [],
"geofences": []
}
],
Expand Down
16 changes: 0 additions & 16 deletions src/data/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -59,6 +44,5 @@ const getLureTypes = () => {
module.exports = {
getPokemonNameIdsList,
buildCityList,
buildCityRoleList,
getLureTypes,
};
26 changes: 5 additions & 21 deletions src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion src/routes/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
10 changes: 5 additions & 5 deletions src/services/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit f22ff78

Please sign in to comment.