Skip to content

Instantly share code, notes, and snippets.

@bclinkinbeard
Last active September 26, 2020 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bclinkinbeard/f6980051aaf1de42b2779cd7798dc8b9 to your computer and use it in GitHub Desktop.
Save bclinkinbeard/f6980051aaf1de42b2779cd7798dc8b9 to your computer and use it in GitHub Desktop.
const muteStorage = new Keyv(MONGODB_URI, { namespace: `${env}mute` })
const unmute = async () => {
client.guilds.cache.forEach((g) => {
const muted = g.members.cache.filter((m) => {
const roleNames = Array.from(
m.roles.cache.mapValues((r) => r.name).values(),
)
return roleNames.includes(ROLES.MUTED)
})
if (muted.size) {
const guildRoles = g.roles.cache
muted.forEach(async (m) => {
const muteRecord = await muteStorage.get(m.id)
if (!muteRecord) {
m.roles.remove(findRoleByName(guildRoles, ROLES.MUTED))
}
})
}
console.log(`${g.name}: ${muted.size} muted`)
})
}
client.once('ready', async () => {
setInterval(unmute, 1000 * 10)
})
if (command === COMMANDS.UNMUTE) {
await muteStorage.delete(mutee.id)
mutee.roles.remove(mutedRole)
} else {
const mention = `<@!${mutee.id}>`
const msg = request.substr(mention.length).trim()
duration = msg.substr(0, msg.indexOf(' '))
if (!duration.length) {
return message.channel.send('You must provide a duration and reason')
}
const unit = duration.substr(-1)
const num = parseInt(duration.substr(0, duration.indexOf(unit)))
if (!['m', 'h', 'd'].includes(unit)) {
return message.channel.send('Duration must end in "m", "h", or "d"')
}
if (isNaN(num)) {
return message.channel.send('Could not parse number from duration')
}
reason = msg.substr(duration.length + 1)
if (!reason.length) {
return message.channel.send('You must provide a reason')
}
let ms
switch (unit) {
case 'm':
ms = 1e3 * 60
break
case 'h':
ms = 1e3 * 60 * 60
break
case 'd':
ms = 1e3 * 60 * 60 * 24
break
}
await muteStorage.set(mutee.id, reason, ms * num)
mutee.roles.add(mutedRole)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment