Skip to content

Commit 0878152

Browse files
authored
Merge pull request #21 from GitCodeCareer/utils-and-tests
Utils and tests
2 parents 0d14137 + 33189ad commit 0878152

14 files changed

+3948
-60
lines changed

babel.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
},
10+
],
11+
],
12+
};

commands/announce.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
const config = require("../config.json");
2-
31
exports.run = (client, message, args) => {
42

5-
if (message.member.roles.find(val => val.id === config.adminRoleId)) {
3+
if (Auth.isAdmin(message.member)) {
64
if(!args || args.length < 1) return message.reply("Must provide the text for the announcement after the command.");
75

86
const announcement = args.join(' ');

commands/helpme.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const Discord = require("discord.js");
22

3-
const config = require("../config.json");
4-
53
exports.run = (client, message, args) => {
64

75
if(!args || args.length < 1) return message.reply(`, you must type your question after the command. You may also use "!helpme PRIVATE" if you would like your question to remain confidential.`);
@@ -18,7 +16,7 @@ exports.run = (client, message, args) => {
1816

1917
message.author.send({embed: member_embed})
2018

21-
const assignee = message.guild.roles.get(config.adminRoleId).members.random(1)
19+
const assignee = message.guild.roles.get(Config.getRole(role_type="admin")).members.random(1)
2220

2321
const staff_embed = new Discord.RichEmbed()
2422
.setTitle("CodeCareer Assistance")
@@ -27,7 +25,7 @@ exports.run = (client, message, args) => {
2725
.addField("Assignee", `${assignee.toString()}`, true)
2826
.addField("Date of Request", message.createdAt, true);
2927

30-
const channel = message.guild.channels.get(config.helpLogChannelId)
28+
const channel = message.guild.channels.get(Config.getChannel("tickets"))
3129
channel.send({embed: staff_embed})
3230

3331
message.delete()

commands/poll.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
const Discord = require("discord.js");
2-
3-
const config = require("../config.json");
4-
51
exports.run = (client, message, args) => {
62

7-
if (message.member.roles.find(val => val.id === config.adminRoleId)) {
3+
if (Auth.isAdmin(message.member)) {
84
if(!args || args.length < 1) return message.reply("Must provide the text of the poll question after the command.");
95

106
const question = args.join(' ')

config.example.json

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
2-
"clientID": "",
3-
"clientSecret": "",
4-
"token": "",
5-
"cmdPrefix": "!",
6-
"welcomeMessageChannel": "💬-all-chat",
7-
"helpLogChannelId": "",
8-
"adminRoleId": "",
2+
"botToken": "",
3+
"commandPrefix": "!",
4+
"channels": {
5+
"joins": "",
6+
"tickets": ""
7+
},
8+
"roles": {
9+
"admin": "",
10+
"mod": "",
11+
"staff": []
12+
},
913
"database": {
1014
"host": "localhost",
1115
"dbname": "",

events/guildMemberAdd.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const config = require("../config.json");
2-
31
exports.run = (client, member) => {
42

53
// Send the message to a designated channel on a server:
6-
const channel = member.guild.channels.find(ch => ch.name === config.welcomeMessageChannel);
4+
const channel = member.guild.channels.get(Config.getChannel("joins"));
75

86
// Do nothing if the channel wasn't found on this server
97
if (!channel) return;

events/message.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const config = require("../config.json");
2-
31
exports.run = (client, msg) => {
42

53
if (msg.author.bot) return;
6-
if(msg.content.indexOf(config.cmdPrefix) !== 0) return;
4+
if(msg.content.indexOf(Config.getCommandPrefix()) !== 0) return;
75

8-
const args = msg.content.slice(config.cmdPrefix.length).trim().split(/ +/g);
6+
const args = msg.content.slice(Config.getCommandPrefix().length).trim().split(/ +/g);
97
const command = args.shift().toLowerCase().replace('/', '');
108

119
try {

index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
global.Config = require('./utils/config').config;
2+
global.Auth = require('./utils/auth').auth;
3+
14
const Discord = require("discord.js");
25
const client = new Discord.Client();
36

4-
const config = require("./config.json");
5-
67
client.on('ready', () => {
78
console.log(`Logged in as ${client.user.tag}!`);
89
});
@@ -18,4 +19,4 @@ fs.readdir('./events/', (err, files) => {
1819
});
1920
});
2021

21-
client.login(config.token);
22+
client.login(Config.getBotToken());

jest.config.js

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// Respect "browser" field in package.json when resolving modules
12+
// browser: false,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "/private/var/folders/p6/jphk387d57n6x6n2q65b483c0000gp/T/jest_dy",
16+
17+
// Automatically clear mock calls and instances between every test
18+
clearMocks: true,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: null,
25+
26+
// The directory where Jest should output its coverage files
27+
// coverageDirectory: null,
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "/node_modules/"
32+
// ],
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: null,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: null,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files usin a array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: null,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: null,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// An array of directory names to be searched recursively up from the requiring module's location
64+
// moduleDirectories: [
65+
// "node_modules"
66+
// ],
67+
68+
// An array of file extensions your modules use
69+
// moduleFileExtensions: [
70+
// "js",
71+
// "json",
72+
// "jsx",
73+
// "ts",
74+
// "tsx",
75+
// "node"
76+
// ],
77+
78+
// A map from regular expressions to module names that allow to stub out resources with a single module
79+
// moduleNameMapper: {},
80+
81+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
82+
// modulePathIgnorePatterns: [],
83+
84+
// Activates notifications for test results
85+
// notify: false,
86+
87+
// An enum that specifies notification mode. Requires { notify: true }
88+
// notifyMode: "failure-change",
89+
90+
// A preset that is used as a base for Jest's configuration
91+
// preset: null,
92+
93+
// Run tests from one or more projects
94+
// projects: null,
95+
96+
// Use this configuration option to add custom reporters to Jest
97+
// reporters: undefined,
98+
99+
// Automatically reset mock state between every test
100+
// resetMocks: false,
101+
102+
// Reset the module registry before running each individual test
103+
// resetModules: false,
104+
105+
// A path to a custom resolver
106+
// resolver: null,
107+
108+
// Automatically restore mock state between every test
109+
// restoreMocks: false,
110+
111+
// The root directory that Jest should scan for tests and modules within
112+
// rootDir: null,
113+
114+
// A list of paths to directories that Jest should use to search for files in
115+
// roots: [
116+
// "<rootDir>"
117+
// ],
118+
119+
// Allows you to use a custom runner instead of Jest's default test runner
120+
// runner: "jest-runner",
121+
122+
// The paths to modules that run some code to configure or set up the testing environment before each test
123+
// setupFiles: [],
124+
125+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
126+
// setupFilesAfterEnv: [],
127+
128+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
129+
// snapshotSerializers: [],
130+
131+
// The test environment that will be used for testing
132+
testEnvironment: "node",
133+
134+
// Options that will be passed to the testEnvironment
135+
// testEnvironmentOptions: {},
136+
137+
// Adds a location field to test results
138+
// testLocationInResults: false,
139+
140+
// The glob patterns Jest uses to detect test files
141+
// testMatch: [
142+
// "**/__tests__/**/*.[jt]s?(x)",
143+
// "**/?(*.)+(spec|test).[tj]s?(x)"
144+
// ],
145+
146+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
147+
// testPathIgnorePatterns: [
148+
// "/node_modules/"
149+
// ],
150+
151+
// The regexp pattern or array of patterns that Jest uses to detect test files
152+
// testRegex: [],
153+
154+
// This option allows the use of a custom results processor
155+
// testResultsProcessor: null,
156+
157+
// This option allows use of a custom test runner
158+
// testRunner: "jasmine2",
159+
160+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
161+
// testURL: "http://localhost",
162+
163+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
164+
// timers: "real",
165+
166+
// A map from regular expressions to paths to transformers
167+
// transform: null,
168+
169+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
170+
// transformIgnorePatterns: [
171+
// "/node_modules/"
172+
// ],
173+
174+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
175+
// unmockedModulePathPatterns: undefined,
176+
177+
// Indicates whether each individual test should be reported during the run
178+
// verbose: null,
179+
180+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
181+
// watchPathIgnorePatterns: [],
182+
183+
// Whether to use watchman for file crawling
184+
// watchman: true,
185+
};

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
"author": "CodeCareer.org",
88
"license": "MIT",
99
"private": true,
10+
"scripts": {
11+
"test": "jest"
12+
},
1013
"dependencies": {
1114
"discord.js": "^11.4.2",
1215
"request": "^2.88.0",
1316
"request-promise-native": "^1.0.7"
17+
},
18+
"devDependencies": {
19+
"@babel/core": "^7.3.3",
20+
"@babel/preset-env": "^7.3.1",
21+
"babel-jest": "^24.1.0",
22+
"jest": "^24.1.0"
1423
}
1524
}

tests/config.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Config = require('../utils/config').config;
2+
3+
test('bot token returns string value', () => {
4+
expect(Config.getBotToken()).toMatch(/[\w\d.]+/)
5+
})
6+
7+
test('command prefix returns string value of length 1', () => {
8+
expect(Config.getCommandPrefix()).toHaveLength(1)
9+
})

utils/auth.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Auth {
2+
3+
constructor() {
4+
5+
}
6+
7+
isAdmin(member) {
8+
return member.roles.find(val => val.id === Config.getRole("admin"))
9+
}
10+
11+
isMod(member) {
12+
return member.roles.find(val => val.id === Config.getRole("mod"))
13+
}
14+
15+
isRole(role_type) {
16+
return member.roles.find(val => val.id === Config.getRole(role_type))
17+
}
18+
}
19+
20+
module.exports = {
21+
auth: new Auth
22+
}

0 commit comments

Comments
 (0)