Skip to content

Commit

Permalink
newusers: Implement slash commands.
Browse files Browse the repository at this point in the history
When the original report is gone, and we want to re-mark a user as spam
or not spam, we now have /report and /unreport commands for that.
  • Loading branch information
KockaAdmiralac committed Aug 15, 2023
1 parent 77b79a5 commit e92b5cb
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions modules/newusers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ const {
InteractionResponseType,
MessageComponentTypes
} = require('discord-interactions');
const {WebhookClient} = require('discord.js');
const {WebhookClient, MessageFlags} = require('discord.js');

const UPDATE_PROFILE_QUERY = 'UPDATE `profiles` SET ' +
const UPDATE_PROFILE_QUERY_BASE = 'UPDATE `profiles` SET ' +
'`is_spam` = ?, ' +
'`classifying_user` = ?, ' +
'`classification_date` = ? ' +
'WHERE `id` = ?';
'`classification_date` = ?';
const UPDATE_PROFILE_QUERY_ID = `${UPDATE_PROFILE_QUERY_BASE}
WHERE \`id\` = ?`;
const UPDATE_PROFILE_QUERY_NAME = `${UPDATE_PROFILE_QUERY_BASE}
WHERE \`name\` = ?`;
const GET_USERNAME_QUERY = 'SELECT `name` FROM `profiles` WHERE `id` = ?';

/**
Expand Down Expand Up @@ -78,12 +81,15 @@ class NewUsersServer {
const {type, data, message, member} = req.body;
const {
custom_id: customId,
component_type: componentType
component_type: componentType,
name,
options
} = data;
const [status, userId] = (customId || '').split('-');
const isSpam = status === 'spam';
const reporterId = member.user.id;
const reporter = member.user.username;
const [userOpt] = options || [];
switch (type) {
case InteractionType.PING:
res.json({
Expand Down Expand Up @@ -125,6 +131,29 @@ class NewUsersServer {
});
break;
case InteractionType.APPLICATION_COMMAND:
switch (name) {
case 'report':
await this.#staging.addUser(userOpt.value, reporter);
await this.#classify(true, reporterId, userOpt.value);
break;
case 'unreport':
await this.#staging.removeUser(userOpt.value);
await this.#classify(false, reporterId, userOpt.value);
break;
default:
res.status(400).json({
message: 'Unexpected slash command.'
});
return;
}
res.json({
data: {
content: 'Done!',
flags: MessageFlags.Ephemeral
},
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE
});
break;
case InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE:
case InteractionType.MODAL_SUBMIT:
default:
Expand All @@ -138,15 +167,19 @@ class NewUsersServer {
* Classifies a profile as spam or not spam.
* @param {boolean} isSpam Whether the profile is spam
* @param {string} discordUserId ID of the classifying user
* @param {number} fandomUserId ID of the Fandom user being classified
* @param {number|string} fandomUser ID/username of the Fandom user being
* classified
* @returns {Promise} Result of the insert operation
*/
#classify(isSpam, discordUserId, fandomUserId) {
return this.#db.execute(UPDATE_PROFILE_QUERY, [
#classify(isSpam, discordUserId, fandomUser) {
const query = typeof fandomUser === 'string' ?
UPDATE_PROFILE_QUERY_NAME :
UPDATE_PROFILE_QUERY_ID;
return this.#db.execute(query, [
isSpam,
discordUserId,
new Date(),
fandomUserId
fandomUser
]);
}
/**
Expand Down

0 comments on commit e92b5cb

Please sign in to comment.