Skip to content

Commit 47cbbdb

Browse files
author
sherlocksometimes
committed
feat: add config to set scanner cooldown by role
example config setup (scanZone accepts the same format) "scanNext": { ... // Discord roles that will be allowed to use scanNext "discordRoles": ["Supporter","Scanner", "Mod"], // Default cooldown if no role specific cooldown "userCooldownSeconds": 60, //List of role/cooldown pair shortest matching time will apply "discordRoleCooldown":[["Supporter",30],["Mod",0]], ... } Expected outcome: Supporter: 30 second cooldown Mod: 0 second cooldown Scanner: will get the default 60 second cooldown. Supporter & Mod role: the lower 0 second cooldown would apply.
1 parent 8be85b6 commit 47cbbdb

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

packages/types/lib/server.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export interface Permissions {
174174
scanner: string[]
175175
areaRestrictions: string[]
176176
webhooks: string[]
177+
scannerCooldowns: string[]
177178
}
178179

179180
export interface Waypoint {

server/src/configs/default.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@
615615
"pokemon": false,
616616
"gmf": true,
617617
"scanNextInstance": "scanNext",
618+
"rules": [],
618619
"scanNextDevice": "Device01",
619620
"scanNextSleeptime": 5,
620621
"userCooldownSeconds": 0,
@@ -634,6 +635,7 @@
634635
"gmf": false,
635636
"scanZoneMaxSize": 10,
636637
"userCooldownSeconds": 0,
638+
"rules": [],
637639
"advancedScanZoneOptions": false,
638640
"scanZoneRadius": {
639641
"pokemon": 70,
@@ -1025,4 +1027,4 @@
10251027
"tracesSampleRate": 0.1
10261028
}
10271029
}
1028-
}
1030+
}

server/src/graphql/resolvers.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,19 @@ const resolvers = {
384384
gymRadius: scanner.scanZone.scanZoneRadius.gym,
385385
spacing: scanner.scanZone.scanZoneSpacing,
386386
maxSize: scanner.scanZone.scanZoneMaxSize,
387-
cooldown: scanner.scanZone.userCooldownSeconds,
387+
cooldown:
388+
perms?.scannerCooldowns?.[mode] ||
389+
scanner.scanZone.userCooldownSeconds,
388390
refreshQueue: scanner.backendConfig.queueRefreshInterval,
389391
enabled: scanner[mode].enabled,
390392
}
391393
: {
392394
scannerType: scanner.backendConfig.platform,
393395
showScanCount: scanner.scanNext.showScanCount,
394396
showScanQueue: scanner.scanNext.showScanQueue,
395-
cooldown: scanner.scanNext.userCooldownSeconds,
397+
cooldown:
398+
perms?.scannerCooldowns?.[mode] ||
399+
scanner.scanZone.userCooldownSeconds,
396400
refreshQueue: scanner.backendConfig.queueRefreshInterval,
397401
enabled: scanner[mode].enabled,
398402
}
@@ -597,11 +601,11 @@ const resolvers = {
597601
(!req.session.cooldown || req.session.cooldown < Date.now())
598602
) {
599603
const validCoords = getValidCoords(category, data?.scanCoords, perms)
600-
604+
const cooldownSeconds =
605+
perms?.scannerCooldowns?.[category] ||
606+
config.getSafe(`scanner.${category}.userCooldownSeconds`)
601607
const cooldown =
602-
config.getSafe(`scanner.${category}.userCooldownSeconds`) *
603-
validCoords.filter(Boolean).length *
604-
1000 +
608+
cooldownSeconds * validCoords.filter(Boolean).length * 1000 +
605609
Date.now()
606610
req.session.cooldown = cooldown
607611
return scannerApi(

server/src/services/DiscordClient.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,23 @@ class DiscordClient {
129129
webhooks: new Set(),
130130
scanner: new Set(),
131131
blockedGuildNames: new Set(),
132+
rules: new Map(),
132133
}
133134
const scanner = config.getSafe('scanner')
135+
perms.scannerCooldowns = {}
136+
134137
try {
135138
const guilds = user.guilds.map((guild) => guild.id)
136139
if (this.strategy.allowedUsers.includes(user.id)) {
137140
Object.keys(this.perms).forEach((key) => (perms[key] = true))
138141
perms.admin = true
139142
config.getSafe('webhooks').forEach((x) => permSets.webhooks.add(x.name))
140-
Object.keys(scanner).forEach(
141-
(x) => scanner[x]?.enabled && permSets.scanner.add(x),
142-
)
143+
Object.keys(scanner).forEach((x) => {
144+
if (scanner[x]?.enabled) {
145+
permSets.scanner.add(x)
146+
scanner[x].userCooldownSeconds = 0
147+
}
148+
})
143149
log.info(
144150
HELPERS.custom(this.rmStrategy, '#7289da'),
145151
`User ${user.username} (${user.id}) in allowed users list, skipping guild and role check.`,
@@ -188,7 +194,21 @@ class DiscordClient {
188194
(x) => permSets.webhooks.add(x),
189195
)
190196
scannerPerms(userRoles, 'discordRoles', trialActive).forEach(
191-
(x) => permSets.scanner.add(x),
197+
(x) => {
198+
permSets.scanner.add(x)
199+
perms.scannerCooldowns[x] = scanner[x].rules.reduce(
200+
(acc, rule) => {
201+
if (
202+
userRoles.includes(rule?.role) &&
203+
rule.cooldown < acc
204+
) {
205+
return rule.cooldown
206+
}
207+
return acc
208+
},
209+
scanner[x].userCooldownSeconds,
210+
)
211+
},
192212
)
193213
}
194214
}),

server/src/services/config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ if (Array.isArray(config.webhooks)) {
340340
}
341341
Object.keys(config.scanner || {}).forEach((key) => {
342342
config.scanner[key] = replaceBothAliases(config.scanner[key] || {})
343+
config.scanner[key]?.rules?.forEach((rule) => {
344+
rule.role = replaceAliases(rule.role)
345+
})
346+
if (config.scanner[key]?.rules) {
347+
config.scanner[key].rulesObj = Object.fromEntries(
348+
config.scanner[key]?.rules?.map((rule) => [rule.role, rule.cooldown]),
349+
)
350+
}
343351
})
344352

345353
if (

0 commit comments

Comments
 (0)