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: Add is admin utility #567

Merged
merged 9 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
51 changes: 31 additions & 20 deletions botCommands/points.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const config = require('../config');
const { registerBotCommand } = require('../botEngine');
const club40Gifs = require('./club_40_gifs.json');

const { isAdmin } = require('../utils/is-admin');

axios.default.defaults.headers.common.Authorization = `Token ${config.pointsbot.token}`;

function gifPicker(gifContainer, clubChannel) {
Expand All @@ -11,30 +13,35 @@ function gifPicker(gifContainer, clubChannel) {
clubChannel.send(`Gif by ${gifContainer[choice].author}`);
}

function getUserIdsFromMessage(client, author, guild, text, regex, authorMember, channel) {
function getUserIdsFromMessage(
client,
author,
guild,
text,
regex,
authorMember,
channel,
) {
const matches = [];
const processedIDs = [];
let match = regex.exec(text);

while (match !== null) {
const userID = match[1].replace('!', '');
if (match[2] === '?++') {
let isAdmin = false;
authorMember.roles.cache.forEach((value) => {
if (config.roles.adminRolesName.includes(value.name)) {
isAdmin = true;
}
});

if (isAdmin) {
if (isAdmin(authorMember)) {
matches.push([userID, 2]);
} else {
channel.send('Only maintainers or core members can give double points!');
channel.send(
'Only maintainers or core members can give double points!',
);
}
match = regex.exec(text);
} else {
if (processedIDs.includes(userID)) {
channel.send('Only maintainers or core members can give double points!');
channel.send(
'Only maintainers or core members can give double points!',
);
} else {
processedIDs.push(userID);
matches.push([userID, 1]);
Expand All @@ -47,7 +54,8 @@ function getUserIdsFromMessage(client, author, guild, text, regex, authorMember,

const deductPoints = {
regex: /(?<!\S)<@!?(\d+)>\s?(--)(?!\S)/,
cb: () => 'http://media.riffsy.com/images/636a97aa416ad674eb2b72d4a6e9ad6c/tenor.gif',
cb: () =>
'http://media.riffsy.com/images/636a97aa416ad674eb2b72d4a6e9ad6c/tenor.gif',
};

registerBotCommand(deductPoints.regex, deductPoints.cb);
Expand Down Expand Up @@ -163,26 +171,29 @@ const awardPoints = {
if (user) {
const recipientMember = await guild.members.fetch(user);
if (
recipientMember
&& !recipientMember.roles.cache.find((r) => r.name === 'club-40')
&& pointsUser.points > 39
recipientMember &&
!recipientMember.roles.cache.find((r) => r.name === 'club-40') &&
pointsUser.points > 39
) {
const pointsRole = guild.roles.cache.find(
(r) => r.name === 'club-40',
);
recipientMember.roles.add(pointsRole);
const clubChannel = client.channels.cache.get(
'707225752608964628',
);
const clubChannel =
client.channels.cache.get('707225752608964628');

if (clubChannel) {
const welcomeMessage = (previousPoints < 40) ? `HEYYY EVERYONE SAY HI TO ${user} the newest member of CLUB 40. Please check the pins at the top right!` : `WELCOME BACK TO CLUB 40 ${user}!! Please review the pins at the top right!`;
const welcomeMessage =
previousPoints < 40
? `HEYYY EVERYONE SAY HI TO ${user} the newest member of CLUB 40. Please check the pins at the top right!`
: `WELCOME BACK TO CLUB 40 ${user}!! Please review the pins at the top right!`;
clubChannel.send(welcomeMessage);
gifPicker(club40Gifs, clubChannel);
}
}
channel.send(
`${exclamation(pointsUser.points, isGoodQuestion)} ${user} now has ${pointsUser.points
`${exclamation(pointsUser.points, isGoodQuestion)} ${user} now has ${
pointsUser.points
} ${plural(pointsUser.points)}`,
);
}
Expand Down
116 changes: 36 additions & 80 deletions botCommands/points.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const axios = require('axios');
const {
Guild, Channel, Client, User, Member,
} = require('discord.js');
const { Guild, Channel, Client, User, Member } = require('discord.js');
const { Collection } = require('@discordjs/collection');
const commands = require('./points');

axios.post = jest.fn();
Expand Down Expand Up @@ -31,7 +30,8 @@ jest.mock('discord.js', () => ({
},
users: {
cache: {
get: (userId) => users.filter((filteredUser) => `<@${userId}>` === filteredUser.id)[0],
get: (userId) =>
users.filter((filteredUser) => `<@${userId}>` === filteredUser.id)[0],
},
},
user,
Expand Down Expand Up @@ -305,13 +305,7 @@ describe('callback', () => {
const mentionedUser3 = User([], 4, 2);
const mentionedUser4 = User([], 5, 0);
const client = Client(
[
author,
mentionedUser1,
mentionedUser2,
mentionedUser3,
mentionedUser4,
],
[author, mentionedUser1, mentionedUser2, mentionedUser3, mentionedUser4],
channel,
);

Expand Down Expand Up @@ -367,31 +361,21 @@ describe('callback', () => {
describe('where one user is mentioned more than once', () => {
it('returns correct output for only 1 user mentioned twice', async () => {
const mentionedUser1 = User([], 2, 5);
const client = Client(
[
author,
mentionedUser1,
],
channel,
);
const client = Client([author, mentionedUser1], channel);
const data = {
author,
content: `${mentionedUser1.id} ++ ${mentionedUser1.id} ++`,
channel,
client,
guild: Guild([
author,
mentionedUser1,
]),
guild: Guild([author, mentionedUser1]),
};

axios.post
.mockResolvedValueOnce({
data: {
...mentionedUser1,
points: (mentionedUser1.points += 1),
},
});
axios.post.mockResolvedValueOnce({
data: {
...mentionedUser1,
points: (mentionedUser1.points += 1),
},
});

await commands.awardPoints.cb(data);

Expand All @@ -402,32 +386,22 @@ describe('callback', () => {

it('returns correct output for only 1 user mentioned more than 5 times', async () => {
const mentionedUser1 = User([], 2, 5);
const client = Client(
[
author,
mentionedUser1,
],
channel,
);
const client = Client([author, mentionedUser1], channel);

const data = {
author,
content: `${mentionedUser1.id} ++ ${mentionedUser1.id} ++ ${mentionedUser1.id} ++ ${mentionedUser1.id} ++ ${mentionedUser1.id} ++ ${mentionedUser1.id} ++`,
channel,
client,
guild: Guild([
author,
mentionedUser1,
]),
guild: Guild([author, mentionedUser1]),
};

axios.post
.mockResolvedValueOnce({
data: {
...mentionedUser1,
points: (mentionedUser1.points += 1),
},
});
axios.post.mockResolvedValueOnce({
data: {
...mentionedUser1,
points: (mentionedUser1.points += 1),
},
});

await commands.awardPoints.cb(data);

Expand All @@ -443,23 +417,14 @@ describe('callback', () => {
it('returns correct output for 1 user mentioned more than once with another user', async () => {
const mentionedUser1 = User([], 2, 21);
const mentionedUser2 = User([], 3, 23);
const client = Client(
[
author,
mentionedUser1,
],
channel,
);
const client = Client([author, mentionedUser1], channel);

const data = {
author,
content: `${mentionedUser1.id} ++ ${mentionedUser1.id} ++ ${mentionedUser2.id} ++`,
channel,
client,
guild: Guild([
author,
mentionedUser1,
]),
guild: Guild([author, mentionedUser1]),
};

axios.post
Expand Down Expand Up @@ -629,9 +594,7 @@ describe('callback', () => {

await commands.awardPoints.cb(botSpamChannelData);
expect(botSpamChannelData.channel.send).toHaveBeenCalled();
expect(
botSpamChannelData.channel.send.mock.calls[0][0],
).toMatchSnapshot();
expect(botSpamChannelData.channel.send.mock.calls[0][0]).toMatchSnapshot();

await commands.awardPoints.cb(bannedChannelData);
expect(bannedChannelData.channel.send).toHaveBeenCalled();
Expand All @@ -645,7 +608,8 @@ describe('?++ callback', () => {

it('returns correct output for a user who does not have an admin role', async () => {
const mentionedUser = User([], 2, 20);
const memberMap = new Map();
// change to use Collection
Asartea marked this conversation as resolved.
Show resolved Hide resolved
const memberMap = new Collection();
memberMap.set('role-1', { name: '@everyone' });
const member = Member(memberMap);
// users must be passed in as an array
Expand Down Expand Up @@ -673,7 +637,7 @@ describe('?++ callback', () => {

it('returns correct output for a single user w/o club-40', async () => {
const mentionedUser = User([], 2, 20);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
// users must be passed in as an array
Expand Down Expand Up @@ -701,7 +665,7 @@ describe('?++ callback', () => {

it('returns correct output for a single user entering club-40', async () => {
const mentionedUser = User([], 2, 39);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const client = Client([author, mentionedUser], channel);
Expand Down Expand Up @@ -731,7 +695,7 @@ describe('?++ callback', () => {

it('returns correct output for a single user re-entering club-40', async () => {
const mentionedUser = User([], 2, 40);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const client = Client([author, mentionedUser], channel);
Expand Down Expand Up @@ -764,17 +728,11 @@ describe('?++ callback', () => {
const mentionedUser2 = User([], 3, 21);
const mentionedUser3 = User([], 4, 2);
const mentionedUser4 = User([], 5, 0);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const client = Client(
[
author,
mentionedUser1,
mentionedUser2,
mentionedUser3,
mentionedUser4,
],
[author, mentionedUser1, mentionedUser2, mentionedUser3, mentionedUser4],
channel,
);

Expand Down Expand Up @@ -833,7 +791,7 @@ describe('?++ callback', () => {
const mentionedUser3 = User([], 4, 1);
const mentionedUser4 = User([], 5, 0);
const mentionedUser5 = User([], 6, 21);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const client = Client(
Expand Down Expand Up @@ -908,7 +866,7 @@ describe('?++ callback', () => {

it('returns correct output for a user mentioning themselves', async () => {
const client = Client([author], channel);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const data = {
Expand All @@ -935,7 +893,7 @@ describe('?++ callback', () => {

it('returns correct output for a user mentioning Odin Bot', async () => {
const odinBot = User([], 0, 0);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const client = Client([author, odinBot], channel, odinBot);
Expand All @@ -962,7 +920,7 @@ describe('?++ callback', () => {
{ virtual: true },
);
const mentionedUser = User([], 2, 20);
const memberMap = new Map();
const memberMap = new Collection();
memberMap.set('role-1', { name: 'core' });
const member = Member(memberMap);
const botSpamChannel = Channel('513125912070455296');
Expand All @@ -989,9 +947,7 @@ describe('?++ callback', () => {

await commands.awardPoints.cb(botSpamChannelData);
expect(botSpamChannelData.channel.send).toHaveBeenCalled();
expect(
botSpamChannelData.channel.send.mock.calls[0][0],
).toMatchSnapshot();
expect(botSpamChannelData.channel.send.mock.calls[0][0]).toMatchSnapshot();

await commands.awardPoints.cb(bannedChannelData);
expect(bannedChannelData.channel.send).toHaveBeenCalled();
Expand Down
12 changes: 6 additions & 6 deletions events/message-create.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { Events } = require('discord.js');
const GettingHiredMessageService = require('../services/getting-hired-message.service');
const config = require('../config');
const { isAdmin } = require('../utils/is-admin');

const botCommands = [];

let authorBuffer = [];

let currentIntroductionsMessage = null;
const introductionsWelcomeMessage = 'Welcome to The Odin Project! Take a moment to survey all of the channels on the sidebar, especially the <#823266307293839401> channel for answers to commonly asked questions. We\'re excited for you to join us on your programming journey. Happy learning!';
const introductionsWelcomeMessage =
"Welcome to The Odin Project! Take a moment to survey all of the channels on the sidebar, especially the <#823266307293839401> channel for answers to commonly asked questions. We're excited for you to join us on your programming journey. Happy learning!";

function createAuthorEntry(message) {
const entry = {
Expand Down Expand Up @@ -46,9 +48,7 @@ module.exports = {
*/
let isAdminMessage = false;
try {
isAdminMessage = message.member.roles.cache.some(
(r) => config.roles.adminRolesName.includes(r.name),
);
isAdminMessage = isAdmin(message.member);
} catch (e) {
// The only 'con' is a command or message gets ignored.
}
Asartea marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -121,8 +121,8 @@ module.exports = {
// introductions
if (!isAdminMessage) {
if (
currentIntroductionsMessage
&& currentIntroductionsMessage.content === introductionsWelcomeMessage
currentIntroductionsMessage &&
currentIntroductionsMessage.content === introductionsWelcomeMessage
) {
currentIntroductionsMessage.delete();
}
Expand Down
Loading