Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Jobs/Tasks + Moderation Overhaul #44

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
ab44ac2
refactor: Jobs system prep. for rename to tasks + improvements.
sustained Dec 31, 2019
23720ed
chore: Rename jobs to tasks.
sustained Dec 31, 2019
f94abdc
fix: Default NODE_ENV globally to development if it isn't set.
sustained Dec 31, 2019
570d917
fix: Remove unused import.
sustained Dec 31, 2019
59b7af6
fix: Default to empty message.
sustained Dec 31, 2019
872db03
fix: Missing constant.
sustained Dec 31, 2019
eb1e781
fix: Missing constant.
sustained Dec 31, 2019
67feaab
fix: Constant duplication.
sustained Dec 31, 2019
f9c7742
feat: Add beta task.
sustained Dec 31, 2019
7501461
feat: Moderation system improvements.
sustained Dec 31, 2019
517378c
feat: Persistent storage of task configuration.
sustained Dec 31, 2019
ec207aa
fix: Display N/A when there isn't a guild.
sustained Dec 31, 2019
dd2d277
other: Simplify task list output.
sustained Dec 31, 2019
2ff1f92
other: Ignore DB file.
sustained Dec 31, 2019
99513b3
fix: Simplify output (redux).
sustained Dec 31, 2019
6bb1f64
fix: Remove logging.
sustained Jan 2, 2020
83f7d0b
fix: Rename commands.
sustained Jan 2, 2020
04f5487
fix: Ignore and remove moderation DB.
sustained Jan 2, 2020
6928376
fix: Make log message clearer.
sustained Jan 2, 2020
db9afa3
fix: Ensure msg.member is set.
sustained Jan 2, 2020
d5e832b
other: Remove warn/ban tasks.
sustained Jan 2, 2020
5cbc742
feat: Merge moderation-related tasks.
sustained Jan 2, 2020
355cafc
other: Update actions.
sustained Jan 2, 2020
be0072e
refactor: Extract actions.
sustained Jan 2, 2020
19edabe
Merge remote-tracking branch 'upstream/master'
sustained Jan 10, 2020
01a333d
Merge branch 'master' into refactor-jobs-tasks
sustained Jan 10, 2020
e36471d
other: Remove beta task.
sustained Jan 10, 2020
73047b7
enhance: Add per-event config + checks.
sustained Jan 10, 2020
995cc54
style: Use inline code.
sustained Jan 10, 2020
45991ac
other: Add current guild.
sustained Jan 10, 2020
430c46c
style: Explain code.
sustained Jan 10, 2020
b1be1ed
fix: NODE_ENV
sustained Jan 10, 2020
0e6df97
other: Refactor task service slightly.
sustained Jan 10, 2020
0fd749b
feat: Add reset command.
sustained Jan 10, 2020
69756ac
refactor: Log task.
sustained Jan 10, 2020
e8f2343
feat: Add error command.
sustained Jan 10, 2020
f8035ad
fix: Lower-case triggers.
sustained Jan 10, 2020
62f4ef8
feat: Guild-specific tasks.
sustained Jan 10, 2020
d543150
other: Reply instead.
sustained Jan 10, 2020
35724bb
other: Check for guild-specific tasks.
sustained Jan 10, 2020
82fc2c3
enhance: Debug mode also affects ignored roles.
sustained Jan 10, 2020
937cde5
fix: Logic error.
sustained Jan 10, 2020
7e2bbe7
style: Clean up comments.
sustained Jan 10, 2020
d06aa36
fix: Call super method.
sustained Jan 10, 2020
b085689
fix: Channel could be on wrong guild.
sustained Jan 10, 2020
b86105b
refactor: Slight refactor.
sustained Jan 10, 2020
2eae7b2
other: Make guild-specific.
sustained Jan 10, 2020
3969a96
fix: Missing return.
sustained Jan 10, 2020
6ddcc62
deps: Update version.
sustained Jan 10, 2020
7e894e6
style: Grammar.
sustained Jan 12, 2020
523afed
fix: Switch to a case-insensitive match for roles and channels.
sustained Jan 12, 2020
2fde584
refactor: Refactor bans since (new) kick logic is very similar.
sustained Jan 12, 2020
8c86083
fix: We should still continue even if there is no log channel.
sustained Jan 12, 2020
32ceca5
other: Make it clear how important this is.
sustained Jan 12, 2020
e4b13f7
fix: Update description.
sustained Jan 12, 2020
95ae97e
style: The protected roles includes moderators, so...
sustained Jan 12, 2020
7e3aced
style: Fix typo and add tag to align with other logs.
sustained Jan 12, 2020
b2cc959
style: Simplify options.
sustained Jan 12, 2020
d18faaf
feat: Add new kick action + reorder arguments (more-significant-first).
sustained Jan 12, 2020
2a3667b
style: JSDoc all the things + method reordering.
sustained Jan 12, 2020
1950e45
style: Fix a comment.
sustained Jan 12, 2020
4faccf2
style: Fix class name.
sustained Jan 12, 2020
02cca57
fix: Development-only command registration in production.
sustained Jan 12, 2020
a8a2c4d
feat: If a new task is created, its config needs writing to the DB.
sustained Jan 12, 2020
c351e36
fix: Address logic issues + clean up shouldEventFire.
sustained Jan 12, 2020
3c0d3cb
style: Unused import.
sustained Jan 12, 2020
9af9e4e
docs: Better task documentation.
sustained Jan 12, 2020
8f6597d
docs: Mention the necessity of calling `super.shouldExecute()`.
sustained Jan 12, 2020
0054c78
style: Update examples, aliases, descriptions and prompts.
sustained Jan 12, 2020
1b525a0
fix: Re-order checks.
sustained Jan 12, 2020
acbc515
other: Update default actions to notify.
sustained Jan 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions data/ban-words.txt

This file was deleted.

1 change: 1 addition & 0 deletions data/moderation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.json
1 change: 1 addition & 0 deletions data/tasks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.json
32 changes: 29 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"github-api": "^3.3.0",
"fuse.js": "^3.4.6",
"hjson": "^3.1.2",
"lowdb": "^1.0.0",
"prettier": "^1.18.2"
},
"devDependencies": {
Expand Down
76 changes: 54 additions & 22 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ import { readdirSync } from 'fs'
import { join } from 'path'
import { Collection } from 'discord.js'
import { CommandoClient } from 'discord.js-commando'
import { setDefaults } from './services/tasks'

/*
Ensure that NODE_ENV is set to development if it is unset.
*/
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development'
}

const { NODE_ENV, COMMAND_PREFIX = '!' } = process.env

/*
We allow a comma-separated list of owner IDs, so check for
that and apply it back onto process.env if found.
*/
let OWNER_IDS = process.env.OWNER_IDS || '269617876036616193' // Default to @evan#9589
if (OWNER_IDS.includes(',')) {
OWNER_IDS = OWNER_IDS.split(',')
Expand All @@ -13,7 +25,7 @@ if (OWNER_IDS.includes(',')) {
}
process.env.OWNER_IDS = OWNER_IDS

const PATH_JOBS = join(__dirname, 'jobs')
const PATH_TASKS = join(__dirname, 'tasks')
const PATH_TYPES = join(__dirname, 'types')
const PATH_COMMANDS = join(__dirname, 'commands')

Expand All @@ -23,26 +35,31 @@ const client = new CommandoClient({
})

/*
Initialise jobs.
Initialise tasks.
*/

client.jobs = new Collection()
client.tasks = new Collection()

const jobFiles = readdirSync(PATH_JOBS).filter(file => file.endsWith('.js'))
const taskFiles = readdirSync(PATH_TASKS).filter(file => file.endsWith('.js'))

for (const file of jobFiles) {
for (const file of taskFiles) {
try {
const { default: jobDefinition } = require(`./jobs/${file}`)
const { default: taskDefinition } = require(`./tasks/${file}`)

const jobInstance = new jobDefinition(client)
const taskInstance = new taskDefinition(client)

client.jobs.set(jobInstance.name, jobInstance)
client.tasks.set(taskInstance.name, taskInstance)
} catch (e) {
console.warn('Could not load job file: ' + file)
console.warn('Could not load task file: ' + file)
console.error(e)
}
}

/*
Write configuration file if applicable (DB doesn't yet exist).
*/
setDefaults(client.tasks)

/*
Register command groups.

Expand All @@ -62,19 +79,19 @@ client.registry.registerGroups([
name: 'Moderation',
},
{
id: 'jobs',
name: 'Jobs',
id: 'tasks',
name: 'Tasks',
},
{
id: 'rfcs',
name: 'RFCs',
},
{
id: 'development',
name: 'development',
},
])

if (NODE_ENV === 'development') {
client.registry.registerGroup('development', 'development')
}

/*
Register default command groups, commands and argument types.

Expand All @@ -86,7 +103,11 @@ client.registry.registerGroups([
*/
client.registry.registerDefaults()
client.registry.registerTypesIn(PATH_TYPES)
client.registry.registerCommandsIn(PATH_COMMANDS)
client.registry.registerCommandsIn({
dirname: PATH_COMMANDS,
// NOTE: Exclude any commands in the development group, when in production.
excludeDirs: NODE_ENV === 'production' ? '^\\..*|development$' : undefined,
})

if (NODE_ENV === 'production') {
const evalCommand = client.registry.findCommands('eval')
Expand All @@ -95,10 +116,10 @@ if (NODE_ENV === 'production') {
client.registry.unregisterCommand(evalCommand[0])
}
}

/*
Set up some global error handling and some purely informational event handlers.
*/

client.on('warn', console.warn)
client.on('error', console.error)

Expand All @@ -118,11 +139,22 @@ client.on('message', msg => {
return
}

client.jobs
.filter(job => job.enabled)
.forEach(job => {
if (job.shouldExecute(msg)) {
job.run(msg)
client.tasks
.filter(task => {
if (!task.enabled) {
return false
}

// Check for guild-specific tasks.
if (task.guild && msg.guild && task.guild !== msg.guild.id) {
return false
}

return true
})
.forEach(task => {
if (task.shouldExecute(msg)) {
task.run(msg)
}
})
})
Expand Down
26 changes: 26 additions & 0 deletions src/commands/development/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Command } from 'discord.js-commando'

/*
This development-only command just throws an error.
*/
module.exports = class DevelopmentErrorCommand extends Command {
constructor(client) {
super(client, {
guarded: true,
name: 'error',
examples: ['!error'],
group: 'development',
guildOnly: false,
memberName: 'error',
description: 'Create a command error.',
})
}

hasPermission() {
return true
}

async run() {
throw new Error('Well, you asked for it.')
}
}
1 change: 0 additions & 1 deletion src/commands/development/paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const items = [
module.exports = class DevelopmentPaginateCommand extends Command {
constructor(client) {
super(client, {
enabled: process.env.NODE_ENV === 'development',
guarded: true,
name: 'paginate',
args: [
Expand Down
48 changes: 0 additions & 48 deletions src/commands/jobs/disable.js

This file was deleted.

48 changes: 0 additions & 48 deletions src/commands/jobs/enable.js

This file was deleted.

Loading