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

Add support for automatically reacting to approved submissions #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
token: ''
mute_role: ''
prefix: '!'
database:
user: 'postgres'
host: 'localhost'
database: 'postgres'
password: 'postgres'
port: 5432
2 changes: 2 additions & 0 deletions pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ create unique index if not exists submissions_review_message_id_uindex_2
create unique index if not exists submissions_submission_id_uindex
on anon_muting.submissions (submission_id);

alter table anon_muting.events
add column if not exists publish_reactions text[] not null default '{}';
55 changes: 55 additions & 0 deletions src/commands/events/addreaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { CommandoClient, Command, CommandoMessage } from 'discord.js-commando';
import { GuildEmoji } from 'discord.js';
import { pool } from '../../db/db';

export = class AddReaction extends Command {
constructor(bot: CommandoClient) {
super(bot, {
name: 'add_reaction',
aliases: ['areact', 'add_react', 'addreaction', 'addreact'],
group: 'events',
memberName: 'add reaction',
userPermissions: ['MANAGE_CHANNELS'],
description: 'Add a reaction to be automatically applied to approved submissions',
args: [
{
key: 'name',
prompt: 'The name of the event',
type: 'string',
},
{
key: 'emoji',
prompt: 'The emote to apply to the approved submission',
type: 'custom-emoji|default-emoji',
},
],
argsType: 'multiple',
guildOnly: true,
});
}

async run(msg: CommandoMessage, { name, emoji }: { name: string, emoji: string | GuildEmoji}) {
let action: string;
if (typeof emoji === 'string') {
action = emoji;
} else if (emoji instanceof GuildEmoji) {
if (!emoji.available) {
return msg.reply('This emoji is not available');
}
action = emoji.id;
} else {
return msg.reply('Emote was not in a recognised form');
}
const result = await pool.query('UPDATE anon_muting.events\
SET publish_reactions = array_append( publish_reactions, $1 )\
WHERE\
name = $2\
AND NOT $1 = ANY ( publish_reactions ) RETURNING event_id',
[action, name]);
if (result.rowCount) {
return msg.reply('Added this emote to the reaction list');
} else {
return msg.reply('Either the selected event does not exist, or this emote is already a part of it');
}
}
}
55 changes: 55 additions & 0 deletions src/commands/events/delreaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { CommandoClient, Command, CommandoMessage } from 'discord.js-commando';
import { GuildEmoji } from 'discord.js';
import { pool } from '../../db/db';

export = class DelReaction extends Command {
constructor(bot: CommandoClient) {
super(bot, {
name: 'del_reaction',
aliases: ['dreact', 'del_react', 'delreaction', 'delreact'],
group: 'events',
memberName: 'delete reaction',
userPermissions: ['MANAGE_CHANNELS'],
description: 'Remove an emote from being automatically added as a reaction to approved submissions',
args: [
{
key: 'name',
prompt: 'The name of the event',
type: 'string',
},
{
key: 'emoji',
prompt: 'The emote to apply to no longer apply to the approved submission',
type: 'custom-emoji|default-emoji',
},
],
argsType: 'multiple',
guildOnly: true,
});
}

async run(msg: CommandoMessage, { name, emoji }: { name: string, emoji: string | GuildEmoji}) {
let action: string;
if (typeof emoji === 'string') {
action = emoji;
} else if (emoji instanceof GuildEmoji) {
if (!emoji.available) {
return msg.reply('This emoji is not available');
}
action = emoji.id;
} else {
return msg.reply('Emote was not in a recognised form');
}
const result = await pool.query('UPDATE anon_muting.events\
SET publish_reactions = array_remove( publish_reactions, $1 )\
WHERE\
name = $2\
AND $1 = ANY ( publish_reactions ) RETURNING event_id',
[action, name]);
if (result.rowCount) {
return msg.reply('Removed this emote from the reaction list');
} else {
return msg.reply('Either the selected event does not exist, or this emote wasn\'t on the reaction list in the first place');
}
}
}
75 changes: 75 additions & 0 deletions src/commands/events/listevents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { CommandoClient, Command, CommandoMessage } from 'discord.js-commando';
import { MessageEmbed } from 'discord.js';
import { pool } from '../../db/db';

export = class ListEvents extends Command {
constructor(bot: CommandoClient) {
super(bot, {
name: 'list_events',
aliases: ['lsevent', 'lsevents', 'listevents', 'listevent', 'list_event'],
group: 'events',
memberName: 'list events',
userPermissions: ['MANAGE_CHANNELS'],
description: 'List events for suggestions',
guildOnly: true,
});
}

async run(msg: CommandoMessage) {
const results = await pool.query<{
created_by: string;
submissions_channel_id: string;
review_channel_id: string;
name: string;
restriction: number;
publish_reactions: string[];
}>('SELECT created_by, submissions_channel_id, review_channel_id, name, restriction, publish_reactions FROM anon_muting.events WHERE active = true');
const rows = results.rows;
let lastReply: CommandoMessage | undefined;
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const embed = new MessageEmbed();
embed.setTitle(row.name);
embed.addField("Submissions", `<#${row.submissions_channel_id}>`, true);
embed.addField("Review", `<#${row.review_channel_id}>`, true);
embed.addField("Created by", `<@${row.created_by}>`);
switch (row.restriction) {
default:
embed.addField("Restrictions", `Unknown (${row.restriction})`);
break;
case 0:
break;
case 1:
embed.addField("Restrictions", "Image only");
break;
case 2:
embed.addField("Restrictions", "GIF/MP4 only");
break;
case 3:
embed.addField("Restrictions", "Require attachment");
break;
case 4:
embed.addField("Restrictions", "Text only");
break;
}
const reactionList = row.publish_reactions;
if (reactionList.length !== 0) {
const formattedList = [];
for (let i = 0; i < reactionList.length; i++) {
const emote = reactionList[i];
if (emote.length > 4) {
formattedList.push(`<:reaction:${emote}>`);
} else {
formattedList.push(emote);
}
}
embed.addField("Reactions", formattedList.join(' '));
}
lastReply = await msg.replyEmbed(embed);
}
if (lastReply === void 0) {
return msg.reply("There are no active events");
}
return lastReply;
}
}
10 changes: 8 additions & 2 deletions src/handler/reactionhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class ReactionHandler {

private async handleSubmissionReview() {
const submission = await pool.query(
'SELECT e.submissions_channel_id, submission_id, user_id FROM anon_muting.submissions \
'SELECT e.submissions_channel_id, submission_id, user_id, publish_reactions FROM anon_muting.submissions \
INNER JOIN anon_muting.events e on e.event_id = submissions.event_id \
WHERE submissions.review_message_id = $1 AND e.review_channel_id = $2', [this.reaction.message.id, this.reaction.message.channel.id],
);
Expand Down Expand Up @@ -75,9 +75,15 @@ export default class ReactionHandler {
submissionEmbed.author = {
name: 'Submission',
};
await submissionsChannel.send(submissionEmbed);
const createdMessage = await submissionsChannel.send(submissionEmbed);

approved = true;

const neededReactions = submission.rows[0].publish_reactions;
for (let i = 0; i < neededReactions.length; i++) {
const reaction = neededReactions[i];
await createdMessage.react(reaction);
}
break;

case '👎':
Expand Down