Skip to content

Instantly share code, notes, and snippets.

@iamnotacoder-djs
Created February 9, 2022 19:27
Show Gist options
  • Save iamnotacoder-djs/76103a0048ae236abbdc8f24419d757f to your computer and use it in GitHub Desktop.
Save iamnotacoder-djs/76103a0048ae236abbdc8f24419d757f to your computer and use it in GitHub Desktop.
Discord.js@13.6.0 Quiz Mini-game
const { Client, Intents, MessageEmbed, MessageActionRow, MessageButton } = require('discord.js'),
client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES ] });
client.db = require("quick.db");
var config = {
token: "your bot token",
prefix: "/",
adminID: "admin id",
embed_color: "#ffffff"
};
var quiz = require("./quiz.json");
client.login(config.token);
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
if ([null, undefined].includes(client.db.get(`quiz`))) client.db.set(`quiz`, {});
if ([null, undefined].includes(client.db.get(`quiz.spawns`))) client.db.set(`quiz.spawns`, {});
});
client.on('messageCreate', async (message) => {
if (!message.content.startsWith(config.prefix)) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift();
if (command == "play") {
message.delete();
const m = await message.channel.send(getMMenuPage());
client.db.set(`quiz.spawns.m${m.id}`, message.author.id);
}
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isButton()) {
if (client.db.get(`quiz.spawns.m${interaction.message.id}`) != interaction.user.id) return interaction.reply(getMessagePermissionError(client.db.get(`quiz.spawns.m${interaction.message.id}`)));
const q = quiz;
if (interaction.customId == "mmenu_play") {
interaction.message.edit(getQuizRoom(q, 0, interaction.user.id));
defer(interaction);
}
if (interaction.customId.includes("game_answer_")) {
const answerId = interaction.customId.replace(`game_answer_`, ``).trim().split(`_`)[0];
const roomId = interaction.customId.replace(`game_answer_`, ``).trim().split(`_`)[2];
if (q.rooms[roomId].buttons[answerId].next_id != -1) {
interaction.message.edit(getQuizRoom(q, q.rooms[roomId].buttons[answerId].next_id, interaction.user.id));
} else if (q.rooms[roomId].buttons[answerId].ending_id != -1) {
interaction.message.edit(getQuizEnding(q, q.rooms[roomId].buttons[answerId].ending_id, interaction.user.id));
} else {
interaction.message.edit(getQuizRoom(q, 0, interaction.user.id));
}
defer(interaction);
}
}
});
function getMMenuPage() {
const q = quiz;
return {
embeds: [
new MessageEmbed()
.setTitle(q.title)
.setDescription(q.description)
.addField(`Rooms`, `${q.rooms.length}`, true)
.addField(`Endings`, `${q.endings.length}`, true)
.addField(`Author`, `[${q.author}](https://discordapp.com/users/${q.author_id}/)`)
.addField(`Created at`, `<t:${q.created_at}:R>`, true)
],
components: [
new MessageActionRow()
.addComponents(
new MessageButton()
.setLabel(`Play`)
.setStyle("SUCCESS")
.setCustomId(`mmenu_play`)
)
]
};
}
function getMessagePermissionError(ownerId) {
return {
embeds: [
new MessageEmbed()
.setTitle(`Permission Error`)
.setDescription(`This game was started by another user: <@${ownerId}>\nIf you want to play by yourself, use the command **/play**`)
],
ephemeral: true
};
}
function getQuizRoom(q, page = 0, userId) {
const room = q.rooms[page];
let components = [];
for (let i = 0; i < room.buttons.length; i++) {
if (components.length < i + 1) components.push(new MessageActionRow());
components[i].addComponents(
new MessageButton()
.setLabel(`${room.buttons[i].text}`)
.setStyle("PRIMARY")
.setCustomId(`game_answer_${i}_${q.id}_${page}`)
)
}
let embed = new MessageEmbed()
.setTitle(q.title)
.setDescription(`Playing: <@${userId}>\n\n\`\`\`${room.text}\`\`\``);
if (room.image != undefined) {
embed.setImage(room.image);
}
return {
embeds: [ embed ],
components: components
};
}
function getQuizEnding(q, page = 0, userId) {
const ending = q.endings[page];
return {
embeds: [
new MessageEmbed()
.setTitle(`[The Final] ${q.title}`)
.setDescription(`Playing: <@${userId}>\n\n\`\`\`${ending.text}\`\`\``)
],
components: []
};
}
async function defer(interaction) {
await interaction.deferReply();
interaction.deleteReply();
// or
// interaction.deferUpdate();
}
{
"id": 0,
"title": "Quiz game example",
"description": "Yo, this is quiz game example. Actually this is description.",
"author": "idaspin",
"created_at": "1643963036",
"author_id": "431904802918891531",
"endings": [
{
"id": 0,
"text": "This is quiz game ending."
}
],
"rooms": [
{
"id": 0,
"text": "Lets look at idaspin's projects.",
"buttons": [
{
"text": "ApexMap - Interactive Map for Apex Legends",
"next_id": 1,
"ending_id": -1
},
{
"text": "RoomCreator Discord Bot",
"next_id": 2,
"ending_id": -1
}
]
},
{
"id": 1,
"text": "ApexMap - Interactive Map for Apex Legends",
"image": "https://apexmap.online/tools/img/apexmaponline.png",
"buttons": [
{
"text": "Finish this quest",
"next_id": -1,
"ending_id": 0
},
{
"text": "Go Back",
"next_id": 0,
"ending_id": -1
}
]
},
{
"id": 2,
"text": "RoomCreator Discord Bot",
"image": "http://rcbot.idaspin.ru/images/logo.png",
"buttons": [
{
"text": "Finish this quest",
"next_id": -1,
"ending_id": 0
},
{
"text": "Go Back",
"next_id": 0,
"ending_id": -1
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment