-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: role/channel statistics mappings, linting
- Loading branch information
Tobiah
committed
Jan 20, 2020
1 parent
0dcc038
commit 5c79a0e
Showing
10 changed files
with
188 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const Command = require('../../models/Command.js'); | ||
const { captures: { channel: cc, role: rc } } = require('../../CommonFunctions'); | ||
|
||
class TrackRole extends Command { | ||
/** | ||
* Constructs a callable command | ||
* @param {Genesis} bot The bot object | ||
*/ | ||
constructor(bot) { | ||
super(bot, 'settings.roles.track', 'r track', 'Track a role in a server', 'UTIL'); | ||
this.requiresAuth = true; | ||
this.allowDM = false; | ||
this.regex = new RegExp(`^${this.call} ${cc} ${rc}`, 'i'); | ||
this.usages = [ | ||
{ | ||
description: 'Add a new channel <-> role stats binding', | ||
parameters: ['channel', 'role'], | ||
}, | ||
]; | ||
} | ||
|
||
/** | ||
* Run the command | ||
* @param {Message} message Message with a command to handle, reply to, | ||
* or perform an action based on parameters. | ||
* @returns {string} success status | ||
*/ | ||
async run(message) { | ||
const { guild } = message; | ||
const channelId = (message.strippedContent.match(cc) || [])[0] | ||
.replace('<#', '') | ||
.replace('>', ''); | ||
|
||
if (!channelId) return this.messageManager.statuses.FAILURE; | ||
const channel = guild.channels.get(channelId); | ||
if (!channel) return this.messageManager.statuses.FAILURE; | ||
|
||
const roleId = (message.strippedContent.replace(channelId, '') | ||
.match(rc) || [])[0] | ||
.replace('<@&', '') | ||
.replace('>', ''); | ||
|
||
if (!roleId) return this.messageManager.statuses.FAILURE; | ||
const role = guild.roles.get(roleId); | ||
if (!role) return this.messageManager.statuses.FAILURE; | ||
|
||
await this.settings.trackRole(guild, channel, role); | ||
this.messageManager.notifySettingsChange(message, true, true); | ||
return this.messageManager.statuses.SUCCESS; | ||
} | ||
} | ||
|
||
module.exports = TrackRole; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
|
||
const Command = require('../../models/Command.js'); | ||
const { captures: { role: rc } } = require('../../CommonFunctions'); | ||
|
||
class UntrackRole extends Command { | ||
/** | ||
* Constructs a callable command | ||
* @param {Genesis} bot The bot object | ||
*/ | ||
constructor(bot) { | ||
super(bot, 'settings.roles.untrack', 'r untrack', 'Untrack a role in a server', 'UTIL'); | ||
this.requiresAuth = true; | ||
this.allowDM = false; | ||
this.regex = new RegExp(`^${this.call} ${rc}`, 'i'); | ||
this.usages = [ | ||
{ | ||
description: 'Remove an existing role channel binding', | ||
parameters: ['channel', 'role'], | ||
}, | ||
]; | ||
} | ||
|
||
/** | ||
* Run the command | ||
* @param {Message} message Message with a command to handle, reply to, | ||
* or perform an action based on parameters. | ||
* @returns {string} success status | ||
*/ | ||
async run(message) { | ||
const { guild } = message; | ||
|
||
const roleId = (message.strippedContent.match(rc) || [])[0] | ||
.replace('<@&', '') | ||
.replace('>', ''); | ||
|
||
if (!roleId) return this.messageManager.statuses.FAILURE; | ||
const role = guild.roles.get(roleId); | ||
if (!role) return this.messageManager.statuses.FAILURE; | ||
|
||
await this.settings.untrackRole(guild, role); | ||
this.messageManager.notifySettingsChange(message, true, true); | ||
return this.messageManager.statuses.SUCCESS; | ||
} | ||
} | ||
|
||
module.exports = UntrackRole; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const { games } = require('../CommonFunctions'); | ||
|
||
class UpdateGuildStatistics extends require('../models/BaseEventHandler') { | ||
constructor(bot) { | ||
super(bot, 'handlers.statsupdate', 'guildMemberUpdate'); | ||
} | ||
|
||
async execute(...[, newMember]) { | ||
if (!games.includes('UTIL')) return; | ||
this.logger.debug(`Running ${this.id} for ${this.event}. Params: ${newMember.guild}`); | ||
|
||
const { guild } = newMember; | ||
const mappedRoles = await this.settings.getTrackedRoles(guild); | ||
|
||
guild.roles | ||
.filter(r => Object.keys(mappedRoles).includes(r.id)) | ||
.each((role) => { | ||
const channel = guild.channels.get(mappedRoles[role.id]); | ||
channel.setName(`${role.name} :: ${role.members.size}`); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = UpdateGuildStatistics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const SQL = require('sql-template-strings'); | ||
|
||
class StatisticsQueries { | ||
constructor(db) { | ||
this.db = db; | ||
} | ||
|
||
async trackRole(guild, channel, role) { | ||
return this.db.query(SQL` | ||
INSERT IGNORE INTO role_stats | ||
(guild_id, channel_id, role_id) | ||
VALUES (${guild.id}, ${channel.id}, ${role.id}) | ||
`); | ||
} | ||
|
||
async untrackRole(guild, role) { | ||
return this.db.query(SQL` | ||
DELETE FROM role_stats | ||
WHERE guild_id = ${guild.id} | ||
AND role_id = ${role.id} | ||
`); | ||
} | ||
|
||
async getTrackedRoles(guild) { | ||
const q = SQL`SELECT role_id, channel_id FROM role_stats WHERE guild_id = ${guild.id}`; | ||
const map = {}; | ||
const res = (await this.db.query(q))[0]; | ||
res.forEach(({ role_id: roleId, channel_id: channelId }) => { | ||
map[roleId] = channelId; | ||
}); | ||
return map; | ||
} | ||
} | ||
|
||
module.exports = StatisticsQueries; |