-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from OqixDevs/add-reviews
- Loading branch information
Showing
13 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ButtonBuilder, ButtonInteraction, ButtonStyle } from 'discord.js'; | ||
import { prisma } from '../model'; | ||
|
||
export const data = new ButtonBuilder() | ||
.setCustomId('Dislike') | ||
.setLabel('👎') | ||
.setStyle(ButtonStyle.Primary); | ||
|
||
export async function execute(interaction: ButtonInteraction) { | ||
const prevEmbed = interaction.message.embeds[0]; | ||
const reviewId = prevEmbed.footer?.text.split(' ').pop(); | ||
await prisma.reviews.update({ | ||
where: { | ||
id: reviewId, | ||
}, | ||
data: { | ||
negativeRating: { | ||
increment: 1, | ||
}, | ||
}, | ||
}); | ||
return interaction.update({ | ||
content: 'Thank you for your vote', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export * as addChannel from './addChannelButton'; | ||
export * as openCreateModal from './openCreateModalButton'; | ||
export * as Like from './likeButton'; | ||
export * as Dislike from './dislikeButton'; | ||
export * as PreviousReview from './previousEmbedButton'; | ||
export * as NextReview from './nextEmbedButton'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ButtonBuilder, ButtonInteraction, ButtonStyle } from 'discord.js'; | ||
import { prisma } from '../model'; | ||
|
||
export const data = new ButtonBuilder() | ||
.setCustomId('Like') | ||
.setLabel('👍') | ||
.setStyle(ButtonStyle.Primary); | ||
|
||
export async function execute(interaction: ButtonInteraction) { | ||
const prevEmbed = interaction.message.embeds[0]; | ||
const reviewId = prevEmbed.footer?.text.split(' ').pop(); | ||
await prisma.reviews.update({ | ||
where: { | ||
id: reviewId, | ||
}, | ||
data: { | ||
positiveRating: { | ||
increment: 1, | ||
}, | ||
}, | ||
}); | ||
return interaction.update({ | ||
content: 'Thank you for your vote', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ButtonBuilder, ButtonInteraction, ButtonStyle } from 'discord.js'; | ||
import { embedContentBuilder } from '../utils/embedContentBuilder'; | ||
import { reviewEmbedConstructor } from '../embeds'; | ||
|
||
export const data = new ButtonBuilder() | ||
.setCustomId('NextReview') | ||
.setLabel('Next') | ||
.setStyle(ButtonStyle.Primary); | ||
|
||
export async function execute(interaction: ButtonInteraction) { | ||
const move = 1; | ||
const content = await embedContentBuilder(interaction, move); | ||
if (content == undefined) { | ||
return interaction.update({ | ||
content: 'Error while processing reviews!', | ||
}); | ||
} | ||
const embed = reviewEmbedConstructor(content); | ||
return interaction.update({ | ||
content: '', | ||
embeds: [embed], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ButtonBuilder, ButtonInteraction, ButtonStyle } from 'discord.js'; | ||
import { embedContentBuilder } from '../utils/embedContentBuilder'; | ||
import { reviewEmbedConstructor } from '../embeds'; | ||
|
||
export const data = new ButtonBuilder() | ||
.setCustomId('PreviousReview') | ||
.setLabel('Previous') | ||
.setStyle(ButtonStyle.Primary); | ||
|
||
export async function execute(interaction: ButtonInteraction) { | ||
const move = -1; | ||
const content = await embedContentBuilder(interaction, move); | ||
if (content == undefined) { | ||
return interaction.update({ | ||
content: 'Error while processing reviews!', | ||
}); | ||
} | ||
const embed = reviewEmbedConstructor(content); | ||
return interaction.update({ | ||
content: '', | ||
embeds: [embed], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { | ||
ActionRowBuilder, | ||
ButtonBuilder, | ||
//ButtonInteraction, | ||
ChatInputCommandInteraction, | ||
SlashCommandBuilder, | ||
} from 'discord.js'; | ||
import { prisma } from '../model'; | ||
import { reviewEmbedConstructor } from '../embeds'; | ||
import { Dislike, Like, NextReview, PreviousReview } from '../buttons'; | ||
import { Review } from '../utils'; | ||
|
||
/** | ||
* Edits existing bot message in a channel. | ||
*/ | ||
export const data = new SlashCommandBuilder() | ||
.setName('review') | ||
.setDescription('Interacts with review feature of the bot') | ||
.addSubcommand((subcommand) => | ||
subcommand | ||
.setName('get') | ||
.setDescription('Outputs table with reviews for the subject') | ||
.addStringOption((option) => | ||
option | ||
.setName('subject') | ||
.setDescription('Code of the subject (e.g. PV276)') | ||
.setRequired(true) | ||
) | ||
) | ||
.addSubcommand((subcommand) => | ||
subcommand | ||
.setName('add') | ||
.setDescription('Adds new review for the subjet') | ||
.addStringOption((option) => | ||
option | ||
.setName('subject') | ||
.setDescription('Code of the subject (e.g. PV276)') | ||
.setRequired(true) | ||
) | ||
.addStringOption((option) => | ||
option | ||
.setName('text') | ||
.setDescription('Text of the review') | ||
.setRequired(true) | ||
) | ||
); | ||
|
||
export async function execute(interaction: ChatInputCommandInteraction) { | ||
await interaction.reply({ | ||
content: 'Processing review request...', | ||
ephemeral: true, | ||
}); | ||
const subjectCode = interaction.options.getString('subject'); | ||
if (interaction.options.getSubcommand() === 'add') { | ||
const userId = interaction.user.id; | ||
const textReview = interaction.options.getString('text'); | ||
if (textReview == undefined || subjectCode == undefined) { | ||
console.log('Error with arguments for create review command.'); | ||
return interaction.editReply({ | ||
content: `Error while creating a review for ${subjectCode}.`, | ||
}); | ||
} | ||
const existingReview = await prisma.reviews.findMany({ | ||
where: { | ||
discordUserId: { | ||
equals: userId, | ||
}, | ||
subjectCode: { | ||
equals: subjectCode, | ||
}, | ||
}, | ||
}); | ||
console.log(existingReview); | ||
if (existingReview.length != 0) { | ||
return interaction.editReply({ | ||
content: `You have already submitted review from subject ${subjectCode}.`, | ||
}); | ||
} | ||
try { | ||
await prisma.reviews.create({ | ||
data: { | ||
discordUserId: userId, | ||
subjectCode: subjectCode.toUpperCase(), | ||
reviewText: textReview, | ||
}, | ||
}); | ||
} catch (err) { | ||
console.log(`Database error: ${err}`); | ||
return interaction.editReply({ | ||
content: `Error while creating a review for ${subjectCode}.`, | ||
}); | ||
} | ||
return interaction.editReply({ | ||
content: `Review for subject ${subjectCode} created.`, | ||
}); | ||
} else if (interaction.options.getSubcommand() === 'get') { | ||
const submittedReviews = await prisma.reviews.findMany({ | ||
orderBy: { | ||
dateReview: 'asc', | ||
}, | ||
where: { | ||
subjectCode: subjectCode?.toUpperCase(), | ||
}, | ||
}); | ||
if (submittedReviews.length == 0) { | ||
return interaction.editReply({ | ||
content: `No reviews for ${subjectCode?.toUpperCase()} :(`, | ||
}); | ||
} | ||
const title = `Reviews for course ${subjectCode}`; | ||
const reviewer = await interaction.client.users.fetch( | ||
submittedReviews[0].discordUserId | ||
); | ||
const review: Review = { | ||
id: submittedReviews[0].id, | ||
invokedUsername: interaction.user.username, | ||
invokedAvatarUrl: interaction.user.displayAvatarURL(), | ||
title: title, | ||
description: `\`\`\`${submittedReviews[0].reviewText}\`\`\``, | ||
positiveRating: submittedReviews[0].positiveRating, | ||
negativeRating: submittedReviews[0].negativeRating, | ||
reviewerUsername: reviewer.username, | ||
reviwerAvatarUrl: reviewer.displayAvatarURL(), | ||
reviewNumber: 1, | ||
numberOfReviews: submittedReviews.length, | ||
}; | ||
const reviewEmbed = reviewEmbedConstructor(review); | ||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents( | ||
PreviousReview.data, | ||
NextReview.data, | ||
Like.data, | ||
Dislike.data | ||
); | ||
return interaction.editReply({ | ||
content: '', | ||
embeds: [reviewEmbed], | ||
components: [row], | ||
}); | ||
} else { | ||
return interaction.editReply({ | ||
content: `Invalid command.`, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './reviewsEmbed'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { EmbedBuilder } from 'discord.js'; | ||
import { Review } from '../utils'; | ||
|
||
export function reviewEmbedConstructor(reviewInfo: Review) { | ||
const reviewEmbed = new EmbedBuilder() | ||
.setColor('#2f00ff') | ||
.setTitle(reviewInfo.title) | ||
.setAuthor({ | ||
name: reviewInfo.invokedUsername, | ||
iconURL: reviewInfo.invokedAvatarUrl, | ||
}) | ||
.setDescription(reviewInfo.description) | ||
.addFields({ | ||
name: 'Rating of the review', | ||
value: `${reviewInfo.positiveRating}👍|${reviewInfo.negativeRating}👎`, | ||
}) | ||
.setTimestamp() | ||
.setFooter({ | ||
text: `Reviwer: ${reviewInfo.reviewerUsername} · Review number: ${reviewInfo.reviewNumber}/${reviewInfo.numberOfReviews} · ID: ${reviewInfo.id}`, | ||
iconURL: reviewInfo.reviwerAvatarUrl, | ||
}); | ||
|
||
return reviewEmbed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface Review { | ||
id: string; | ||
invokedUsername: string; | ||
invokedAvatarUrl: string; | ||
title: string; | ||
description: string; | ||
positiveRating: number; | ||
negativeRating: number; | ||
reviewerUsername: string; | ||
reviwerAvatarUrl: string; | ||
reviewNumber: number; | ||
numberOfReviews: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ButtonInteraction } from 'discord.js'; | ||
import { Review } from './Review'; | ||
import { prisma } from '../model'; | ||
|
||
export async function embedContentBuilder( | ||
interaction: ButtonInteraction, | ||
move: number | ||
) { | ||
const prevEmbed = interaction.message.embeds[0]; | ||
const reviewId = prevEmbed.footer?.text.split(' ').pop(); | ||
const subjectCode = prevEmbed.title?.replace('Reviews for course ', ''); | ||
const submittedReviews = await prisma.reviews.findMany({ | ||
orderBy: { | ||
dateReview: 'asc', | ||
}, | ||
where: { | ||
subjectCode: subjectCode?.toUpperCase(), | ||
}, | ||
}); | ||
if (prevEmbed.title == null) { | ||
return; | ||
} | ||
for (let index = 0; index < submittedReviews.length; index++) { | ||
if (submittedReviews[index].id === reviewId) { | ||
if (index + move >= submittedReviews.length) { | ||
index = -1; | ||
} | ||
if (index + move < 0) { | ||
index = submittedReviews.length; | ||
} | ||
const reviewer = await interaction.client.users.fetch( | ||
submittedReviews[index + move].discordUserId | ||
); | ||
const review: Review = { | ||
id: submittedReviews[index + move].id, | ||
invokedUsername: interaction.user.username, | ||
invokedAvatarUrl: interaction.user.displayAvatarURL(), | ||
title: prevEmbed.title, | ||
description: `\`\`\`${ | ||
submittedReviews[index + move].reviewText | ||
}\`\`\``, | ||
positiveRating: submittedReviews[index + move].positiveRating, | ||
negativeRating: submittedReviews[index + move].negativeRating, | ||
reviewerUsername: reviewer.username, | ||
reviwerAvatarUrl: reviewer.displayAvatarURL(), | ||
reviewNumber: index + move + 1, | ||
numberOfReviews: submittedReviews.length, | ||
}; | ||
return review; | ||
} | ||
} | ||
console.log('Error occured while processing reviews.'); | ||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters