Here is the challenge bot slash command:
const {
SlashCommandBuilder
} = require('@discordjs/builders');
const {
MessageEmbed,
MessageAttachment,
Role
} = require('discord.js');
const {
$where
} = require('../../schemas/balance');
const Challenge = require('../../schemas/challenge');
const challenges = require('./challenges.json');
module.exports = {
data: new SlashCommandBuilder()
.setName('challenge')
.setDescription('Get your DAILY Fortune! Challenge and progress through the server!'),
async execute(interaction, message) {
const item = challenges[Math.floor(Math.random() * challenges.length)];
const filter = response => {
return response.author.id === interaction.user.id;
};
interaction.reply({
content: `${item.question}`,
ephemeral: true
})
.then(() => {
interaction.channel.awaitMessages({
filter,
max: 1,
time: 30000,
errors: ['time']
})
.then(collected => {
const response = collected.first().content;
collected.first().delete();
if (item.answers.includes(response.toLowerCase())) {
interaction.followUp({
content: `${collected.first().author} got the correct answer!`,
ephemeral: true
});
console.log("Challenge Answered Correct");
var guild = message.guilds.cache.get('948892863926771722');
var role = guild.roles.cache.find(role => role.name === 'Fortune Hunters');
var member = guild.members.cache.get(collected.first().author.id);
member.roles.add(role);
} else {
collected.first().delete();
interaction.followUp({
content: `Looks like you missed the answer this time, come back tomorrow for another chance to find your Fortune! with our daily challenges!`,
ephemeral: true
});
console.log("Challenge Answered Incorrectly");
}
})
.catch(collected => {
interaction.followUp({
content: 'You ran out of time!',
ephemeral: true
});
console.log("Timed Out");
});
});
},
};
And then I have the database setup but I’m not sure how to link it up how I did for the balance command. I think I set it up right, I made clones of the balance stuff and renamed it challenge which brought me up to the implementation into the actual command.
SCHEMA:
const mongoose = require('mongoose');
const challengeSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
guildId: String,
memberId: String,
amount: {type: Number, default: 0 },
correctAnswers: {type: Number, default: 0 },
wrongAnswers: {type: Number, default: 0 },
dateLastAnswered: { type: Date, default: Date.now },
});
module.exports = mongoose.model('Challenge', challengeSchema, 'challenges');
And then there’s the createChallenge function:
const Balance = require('../schemas/challenge');
const mongoose = require("mongoose");
module.exports = (client) => {
client.createChallenge = async (member) => {
let challengeProfile = await Challenge.findOne({ memberId: member.id, guildId: member.guild.id });
if (challengeProfile) {
return challengeProfile;
} else {
challengeProfile = await new Challenge({
_id: mongoose.Types.ObjectId(),
guildId: member.guild.id,
memberId: member.id,
});
await challengeProfile.save().catch(err => console.log(err));
return challengeProfile;
console.log('The Challenge Database is live!');
}
};
};
I know the database is setup, because for the /balance command in mongo I can see the balances being updated with the user IDs and all that information. Here is what I have for the balance slash command:
const { SlashCommandBuilder } = require('@discordjs/builders');
const Balance = require('../../schemas/balance');
module.exports = {
data: new SlashCommandBuilder()
.setName('balance')
.setDescription('Returns info based on a user's balance.')
.addSubcommand(subcommand =>
subcommand
.setName("user")
.setDescription("Gets information of a user mentioned")
.addUserOption(option => option.setName("target").setDescription("The user mentioned"))),
async execute(interaction, client) {
let user = (interaction.options.getUser("target") ? interaction.options.getUser("target") : interaction.user);
const balanceProfile = await client.createBalance(interaction.member);
await interaction.reply({ content: `${interaction.user.tag} has ${balanceProfile.amount}$FP.`});
},
};
The balance schema:
const mongoose = require('mongoose');
const balanceSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
guildId: String,
memberId: String,
amount: {type: Number, default: 0 }
});
module.exports = mongoose.model('Balance', balanceSchema, 'balances');
Create balance function:
const Balance = require('../schemas/balance');
const mongoose = require("mongoose");
module.exports = (client) => {
client.createBalance = async (member) => {
let balanceProfile = await Balance.findOne({ memberId: member.id, guildId: member.guild.id });
if (balanceProfile) {
return balanceProfile;
} else {
balanceProfile = await new Balance({
_id: mongoose.Types.ObjectId(),
guildId: member.guild.id,
memberId: member.id,
});
await balanceProfile.save().catch(err => console.log(err));
return balanceProfile;
}
};
};
I hope all this information is helpful enough for someone to help… I’ve been struggling with this for about 9 hours now and it’s killing me. I can’t figure it out and we need to have this bot live this weekend. Any assistance you can give, I would greatly greatly appreciate! Like I mentioned what I’m trying to do is use the mongoDB database to store when someone does the /challenge command so that I can limit the command to being once per day, and assign an $FP balance reward with the reward role being given after 3 correct answers instead of just the first one.
2
Answers
I have since been able to solve this issue. Here is the finished code, now I just have to create the event handler that will assign a new addition to the balance when someone gets a correct answer:
Hey If your looking to schedule tasks please try this npm package
https://www.npmjs.com/package/cron
It will help.
There is an example how I used it to mark issues in my app as overdue at the middle of the night.