Command for index.js - javascript

How would i create a help command that displays all the commands when people do !help as i have no idea how to do a help command in the index.js file or would it be easir to put the help would be mutch apreated. its using the dicord econmy code but it dosnt have a help command to display all the commands
const eco = require("discord-economy");
//Create the bot client
const client = new Discord.Client();
//Set the prefix and token of the bot.
const settings = {
prefix: '!',
token: 'token'
}
//Whenever someone types a message this gets activated.
//(If you use 'await' in your functions make sure you put async here)
client.on('message', async message => {
//This reads the first part of your message behind your prefix to see which command you want to use.
var command = message.content.toLowerCase().slice(settings.prefix.length).split(' ')[0];
//These are the arguments behind the commands.
var args = message.content.split(' ').slice(1);
//If the message does not start with your prefix return.
//If the user that types a message is a bot account return.
if (!message.content.startsWith(settings.prefix) || message.author.bot) return;
if (command === 'balance') {
var output = await eco.FetchBalance(message.author.id)
message.channel.send(`Hey ${message.author.tag}! You own ${output.balance} coins.`);
}
if (command === 'daily') {
var output = await eco.Daily(message.author.id)
//output.updated will tell you if the user already claimed his/her daily yes or no.
if (output.updated) {
var profile = await eco.AddToBalance(message.author.id, 100)
message.reply(`You claimed your daily coins successfully! You now own ${profile.newbalance} coins.`);
} else {
message.channel.send(`Sorry, you already claimed your daily coins!\nBut no worries, over ${output.timetowait} you can daily again!`)
}
}
if (command === 'resetdaily') {
var output = await eco.ResetDaily(message.author.id)
message.reply(output) //It will send 'Daily Reset.'
}
if (command === 'leaderboard') {
//If you use discord-economy guild based you can use the filter() function to only allow the database within your guild
//(message.author.id + message.guild.id) can be your way to store guild based id's
//filter: x => x.userid.endsWith(message.guild.id)
//If you put a mention behind the command it searches for the mentioned user in database and tells the position.
if (message.mentions.users.first()) {
var output = await eco.Leaderboard({
filter: x => x.balance > 50,
search: message.mentions.users.first().id
})
message.channel.send(`The user ${message.mentions.users.first().tag} is number ${output} on my leaderboard!`);
} else {
eco.Leaderboard({
limit: 3, //Only takes top 3 ( Totally Optional )
filter: x => x.balance > 50 //Only allows people with more than 100 balance ( Totally Optional )
}).then(async users => { //make sure it is async
if (users[0]) var firstplace = await client.fetchUser(users[0].userid) //Searches for the user object in discord for first place
if (users[1]) var secondplace = await client.fetchUser(users[1].userid) //Searches for the user object in discord for second place
if (users[2]) var thirdplace = await client.fetchUser(users[2].userid) //Searches for the user object in discord for third place
message.channel.send(`My leaderboard:
1 - ${firstplace && firstplace.tag || 'Nobody Yet'} : ${users[0] && users[0].balance || 'None'}
2 - ${secondplace && secondplace.tag || 'Nobody Yet'} : ${users[1] && users[1].balance || 'None'}
3 - ${thirdplace && thirdplace.tag || 'Nobody Yet'} : ${users[2] && users[2].balance || 'None'}`)
})
}
}
if (command === 'transfer') {
var user = message.mentions.users.first()
var amount = args[1]
if (!user) return message.reply('Reply the user you want to send money to!')
if (!amount) return message.reply('Specify the amount you want to pay!')
var output = await eco.FetchBalance(message.author.id)
if (output.balance < amount) return message.reply('You have fewer coins than the amount you want to transfer!')
var transfer = await eco.Transfer(message.author.id, user.id, amount)
message.reply(`Transfering coins successfully done!\nBalance from ${message.author.tag}: ${transfer.FromUser}\nBalance from ${user.tag}: ${transfer.ToUser}`);
}
if (command === 'coinflip') {
var flip = args[0] //Heads or Tails
var amount = args[1] //Coins to gamble
if (!flip || !['heads', 'tails'].includes(flip)) return message.reply('Please specify the flip, either heads or tails!')
if (!amount) return message.reply('Specify the amount you want to gamble!')
var output = await eco.FetchBalance(message.author.id)
if (output.balance < amount) return message.reply('You have fewer coins than the amount you want to gamble!')
var gamble = await eco.Coinflip(message.author.id, flip, amount).catch(console.error)
message.reply(`You ${gamble.output}! New balance: ${gamble.newbalance}`)
}
if (command === 'dice') {
var roll = args[0] //Should be a number between 1 and 6
var amount = args[1] //Coins to gamble
if (!roll || ![1, 2, 3, 4, 5, 6].includes(parseInt(roll))) return message.reply('Specify the roll, it should be a number between 1-6')
if (!amount) return message.reply('Specify the amount you want to gamble!')
var output = eco.FetchBalance(message.author.id)
if (output.balance < amount) return message.reply('You have fewer coins than the amount you want to gamble!')
var gamble = await eco.Dice(message.author.id, roll, amount).catch(console.error)
message.reply(`The dice rolled ${gamble.dice}. So you ${gamble.output}! New balance: ${gamble.newbalance}`)
}
if (command == 'delete') { //You want to make this command admin only!
var user = message.mentions.users.first()
if (!user) return message.reply('Please specify a user I have to delete in my database!')
if (!message.guild.me.hasPermission(`ADMINISTRATION`)) return message.reply('You need to be admin to execute this command!')
var output = await eco.Delete(user.id)
if (output.deleted == true) return message.reply('Successfully deleted the user out of the database!')
message.reply('Error: Could not find the user in database.')
}
if (command === 'work') { //I made 2 examples for this command! Both versions will work!
var output = await eco.Work(message.author.id)
//50% chance to fail and earn nothing. You earn between 1-100 coins. And you get one out of 20 random jobs.
if (output.earned == 0) return message.reply('Awh, you did not do your job well so you earned nothing!')
message.channel.send(`${message.author.username}
You worked as a \` ${output.job} \` and earned :money_with_wings: ${output.earned}
You now own :money_with_wings: ${output.balance}`)
var output = await eco.Work(message.author.id, {
failurerate: 10,
money: Math.floor(Math.random() * 500),
jobs: ['cashier', 'shopkeeper']
})
//10% chance to fail and earn nothing. You earn between 1-500 coins. And you get one of those 3 random jobs.
if (output.earned == 0) return message.reply('Awh, you did not do your job well so you earned nothing!')
message.channel.send(`${message.author.username}
You worked as a \` ${output.job} \` and earned :money_with_wings: ${output.earned}
You now own :money_with_wings: ${output.balance}`)
}
if (command === 'slots') {
var amount = args[0] //Coins to gamble
if (!amount) return message.reply('Specify the amount you want to gamble!')
var output = await eco.FetchBalance(message.author.id)
if (output.balance < amount) return message.reply('You have fewer coins than the amount you want to gamble!')
var gamble = await eco.Slots(message.author.id, amount, {
width: 3,
height: 1
}).catch(console.error)
message.channel.send(gamble.grid)//Grid checks for a 100% match vertical or horizontal.
message.reply(`You ${gamble.output}! New balance: ${gamble.newbalance}`)
}
});
//Your secret token to log the bot in. (never show this to anyone!)
client.login(settings.token) ```

The easiest way to do it would be to add something like this
if (command === 'help') {
message.reply('here are my commands: commamd1 commamd2')
}
however, you could use an embed to make it look nicer, see here fore some info about them

Related

How Would I Make an External Database File? - Discord.JS

I am trying to build a discord bot, that would do leveling and warns and such, but i have no idea how I would link a file to the main javascript file so i can have all the database code be "external". I want to use SQLite for my database.
This is currently what i have
client.on("ready", () => {
// Check if the table "points" exists.
const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
if (!table['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have two prepared statements to get and set the score data.
client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (#id, #user, #guild, #points, #level);");
});
client.on("messageCreate", message => {
if (message.author.bot) return;
let score;
if (message.guild) {
score = client.getScore.get(message.author.id, message.guild.id);
if (!score) {
score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1 }
}
score.points++;
const curLevel = Math.floor(0.1 * Math.sqrt(score.points));
if (score.level < curLevel) {
score.level++;
message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
}
client.setScore.run(score);
}
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "give") {
// Limited to guild owner - adjust to your own preference!
if (!message.author.id === message.guild.ownerId) return message.reply("You're not the boss of me, you can't do that!");
const user = message.mentions.users.first() || client.users.cache.get(args[0]);
if (!user) return message.reply("You must mention someone or give their ID!");
const pointsToAdd = parseInt(args[1], 10);
if (!pointsToAdd) return message.reply("You didn't tell me how many points to give...");
// Get their current points.
let userScore = client.getScore.get(user.id, message.guild.id);
// It's possible to give points to a user we haven't seen, so we need to initiate defaults here too!
if (!userScore) {
userScore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, points: 0, level: 1 }
}
userScore.points += pointsToAdd;
// We also want to update their level (but we won't notify them if it changes)
let userLevel = Math.floor(0.1 * Math.sqrt(score.points));
userScore.level = userLevel;
// And we save it!
client.setScore.run(userScore);
return message.channel.send(`${user.tag} has received ${pointsToAdd} points and now stands at ${userScore.points} points.`);
}
if (command === "leaderboard") {
/*const top10 = sql.prepare("SELECT * FROM scores WHERE guild = ? ORDER BY points DESC LIMIT 10;").all(message.guild.id);*/
// Now shake it and show it! (as a nice embed, too!)
const embed = new EmbedBuilder()
.setTitle("Leader board")
.setDescription("Our top 10 points leaders!")
.setColor("#ff0000")
.addFields({ name: '\u200b', value: '\u200b' });
/*for (const data of top10) {
embed.addFields({ name: client.users.cache.get(data.user).tag, value: `${data.points} points (level ${data.level})` });
}*/
return message.channel.send({ embed: embed });
}
// Command-specific code here!
});
The Idea is that when someone messages in chat, the bot will see that message and randomize the amount of XP given to a member. I dont have a full idea on how this can be done externally, if it can.
If i understand this correctly, you want another JS file to be run inside of your current file. Node JS makes this pretty easy!
require('./filename.js')
(also, you might want to use external files to store your commands too!)

Rate limit on guild.roles.create?

My project involves checking a json with course data and creating roles for each course. This worked fine the first few times but after making a few errors, deleting and recreating, the roles aren't being created.
Update roles command:
Essentially the roles get fetched once so multiple fetch requests aren't needed.
const guild = interaction.guild;
await guild.roles.fetch();
const rolesAdded = Array.from(guild.roles.cache.values()).map((r) => { return r.name; });
for(const d of getDisciplines()){
const roles = getDiscplineRoles(d);
for(const r of roles){
if(rolesAdded.includes(r[0])) continue;
let colour;
if(r[1] == "M"){
colour = "Purple";
} else if (r[1] == "S"){
colour = "Blue";
}
addRole(guild, r[0], colour);
rolesAdded.push(r[0]);
}
let courses = getCourses(d);
courses = courses.map((c) => {
return c[0];
});
for(const course of courses){
if(rolesAdded.includes(course)) continue;
addRole(interaction.guild, course, "LightGrey");
rolesAdded.push(course);
}
}
await interaction.reply("Roles created");
addRole function:
The console.log runs fine so i know this works
addRole: async function(guild, roleName, colour) {
// Get role and create if it doesn't exist
const role = await guild.roles.cache.find((r) => r.name === roleName);
if (role !== undefined){
return;
}
console.log(`${roleName} does not exists`);
await guild.roles.create({
name: roleName,
color: colour,
});
}
I have tried looking up something about a limit but have only found 2 forum posts mentioning 2 numbers, 250 and 1000.
Note: No error is returned at all, the program just seems to be waiting for the request to be filled
Links:
https://github.com/discord/discord-api-docs/issues/1480
https://support.discord.com/hc/en-us/community/posts/360050533812-Extreme-rate-limits-on-the-role-create-endpoint

Receiving massive latency on a recursive function(s) for awaiting input

I have the following code
const Discord = require("discord.js"),
{ MessageEmbed } = Discord,
ms = require("ms"),
module.exports.run = async (client, message) => {
async function awaitMessages(func) {
let output;
const m = await message.channel.awaitMessages({ filter: m => m.author.id === message.author.id, time: 60000 });
if (m.content !== "cancel") output = await func(m);
else return await message.channel.send("cancelled") && null;
if (typeof output === "function") return await output() && awaitMessages(func);
else return output;
}
function makeWaitArg(argName, description) {
const embeds = [new MessageEmbed()
.setTitle(`Giveaway ${argName}`)
.setDescription(description + " within the next 60 seconds.")
.setColor("#2F3136")
.setTimestamp()
.setFooter("Create A Giveaway! | Type cancel to cancel.", message.author.displayAvatarURL())];
return () => message.channel.send({ embeds: embeds })
};
function makeError(title, desc) {
const embeds = [new MessageEmbed().setFooter("type cancel to cancel")];
return () => message.channel.send({ embed: embeds })
}
const channel = await awaitMessages(makeWaitArg("Channel", "Please type the channel you want the giveaway to be in."), ({content}) => {
if (message.guild.channels.cache.has(content) || message.guild.channel.cache.get(content)) return content;
else return makeError("Channel", "That channel doesn't exist. Try Again.");
}),
prize = await awaitMessages(makeWaitArg("Prize.", "Please send what you would like the giveaway prize to be"), ({ content }) => {
if (content.length > 256) return makeError("Giveaway Prize Too Long.", "Please ensure the giveaway prize is less than 256 characters. Try again.");
else return content;
}),
// ill do those just be quick with logic
winnerCount = await awaitMessages(makeWaitArg("Winner Count.","Please send how many winners you would like the giveaway to have"), ({ content }) => {
if (isNaN(content)) return makeError("Invalid Winner Count.", "Please provide valid numbers as the winner count. Try again.");
else if (parseInt(content) > 15) return makeError("You can not have more than 15 winners per giveaway!")
else return content;
}),
time = await awaitMessages(makeWaitArg("Time.", "Please send how long you would like the giveaway to last"), ({ content }) => {
if (!ms(content)) return makeError("Invalid Time.", "Please provide a valid time. Try again.");
else return ms(content);
})
if (!channel || !prize || !winnerCount || !time) return;
// Start the giveaway
};
Recursion is being used to appropriately ask for multiple inputs upon failure to provide a correct input, according to my logic everything should work fine and from what I observe is that I face a massive latency between two of the steps which might be due to unnecessary load on the system while performing recursion, I need to know where that is originating and would like to rectify it.

Discord.js bot was working, added one extra command and everything broke. Can anyone help me figure out why?

Tried to venture in to the realm of making discord bots. Followed along with a fairly simple tutorial, tweaking it along the way to fit what I was trying to make. The bot originally worked, but I went back in to add the "Mistake" command, and suddenly it's not working. I added in console.log pretty much everywhere, trying to figure out how far everything was getting.
When I start the bot, it will spit out the "Bot Online" log. When I input a command, it will spit out the "Commands" log, but it won't register the command at all. I've tried looking for any minor typos, missing brackets, etc... but I just can't seem to figure out what's gone wrong. I'm hoping that someone here can help! Thank you!
const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();
const SQLite = require('better-sqlite3');
const sql = new SQLite('./scores.sqlite');
client.on('ready', () => {
console.log('Bot Online');
const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'goals';").get();
if (!table['count(*)']) {
sql.prepare('CREATE TABLE goals (id TEXT PRIMARY KEY, user TEXT, guild TEXT, goals INTEGER);').run();
sql.prepare('CREATE UNIQUE INDEX idx_goals_id ON goals (id);').run();
sql.pragma('synchronous = 1');
sql.pragma('journal_mode = wal');
}
//Statements to get and set the goal data
client.getGoals = sql.prepare('SELECT * FROM goals WHERE user = ? AND guild = ?');
client.setGoals = sql.prepare('INSERT OR REPLACE INTO goals (id, user, guild, goals) VALUES (#id, #user, #guild, #goals);');
});
client.on('message', (message) => {
if (message.author.bot) return;
let goalTotal;
if (message.guild) {
goalTotal = client.getGoals.get(message.author.id, message.guild.id);
if (!goalTotal) {
goalTotal = {
id: `${message.guild.id}-${message.author.id}`,
user: message.author.id,
guild: message.guild.id,
goals: 0,
};
}
}
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
console.log('Commands');
if (command === 'Goals') {
console.log('Goals');
return message.reply(`You Currently Have ${goalTotal.goals} Own Goals.`);
}
if (command === 'OwnGoal') {
console.log('Own Goal');
const user = message.mentions.users.first() || client.users.cache.get(args[0]);
if (!user) return message.reply('You must mention someone.');
let userscore = client.getGoals.get(user.id, message.guild.id);
if (!userscore) {
userscore = {
id: `${message.guild.id}-${user.id}`,
user: user.id,
guild: message.guild.id,
goals: 0,
};
}
userscore.goals++;
console.log({ userscore });
client.setGoals.run(userscore);
return message.channel.send(`${user.tag} has betrayed his team and now has a total of ${userscore.goals} own goals.`);
}
if (command === 'Mistake') {
console.log('Mistake');
const user = message.mentions.users.first() || client.users.cache.get(args[0]);
if (!user) return message.reply('You must mention someone.');
let userscore = client.getGoals.get(user.id, message.guild.id);
if (!userscore) {
return message.reply('This person has no Rocket Bot activity.');
}
if (userscore === 0) {
return message.reply('This player currently has no goals.');
}
if (userscore > 0) {
userscore.goals--;
}
console.log({ userscore });
client.setGoals.run(userscore);
return message.channel.send(`${user.tag} was falsely accused and now has a total of ${userscore.goals} own goals.`);
}
if (command === 'Leaderboard') {
console.log('Leaderboard');
const leaderboard = sql.prepare('SELECT * FROM goals WHERE guild = ? ORDER BY goals DESC;').all(message.guild.id);
const embed = new Discord.MessageEmbed()
.setTitle('Rocket Bot Leaderboard')
.setAuthor(client.user.username, client.user.avatarURL())
.setDescription('Total Goals Scored Against Own Team:')
.setFooter('Rocket Bot')
.setThumbnail('https://imgur.com/a/S9HN4bT')
.setColor('0099ff');
for (const data of leaderboard) {
embed.addFields({
name: client.users.cache.get(data.user).tag,
value: `${data.goals} goals`,
inline: true,
});
}
return message.channel.send({ embed });
}
if (command === 'RocketHelp') {
console.log('Help');
return message.reply(
'Rocket Bot Commands:' +
'\n' +
'!Goals - See How Many Goals You Have Scored Against Your Own Team' +
'\n' +
'!OwnGoal - Tag Another Player With # To Add One To Their Total' +
'\n' +
'!Mistake - Tag Another Player With # To Subtract One From Their Total' +
'\n' +
'!Leaderboard - Show The Current Leaderboard'
);
}
});
client.login(config.token);
You are improperly splitting the message content. You added g to the regex by accident.
Correct line:
const args = message.content.slice(config.prefix.length).trim().split(/ +/);
Because of improper args split, it could not find any command at all, hence no console log was invoked after Commands.

Why is message.client.users.cache.random() only getting people that have messaged?

The command I set up just grabs a random user to spice up the message a bit. I had to fix something last night and when I restarted the bot, it is now only getting anyone that has messaged. Before, it was getting any random person in the server like it's supposed to. I have also tried to use
message.guild.members.cache.random();
with the same results. Here is the rest of the code from that command
const Discord = require('discord.js');
const fs = require('fs');
const colors = require('../../colors.json');
const candyAmount = require('../../candyheld.json');
const client = new Discord.Client();
module.exports = {
name: 'trickortreat',
description: 'Special Halloween command',
execute(message, args) {
if (!candyAmount[message.author.id]) {
candyAmount[message.author.id] = {
candyStored: 5
}
fs.writeFile('./candyheld.json', JSON.stringify(candyAmount), err => {
if (err) console.error(err);
});
message.delete();
return message.channel.send('For starting the event, you have been given 5 pieces of candy!');
}
// Stores a random number of candy from 1-3
let candyGiven = Math.floor(Math.random() * 3 + 1);
let jackpot = Math.floor(Math.random() * 10 + 20 );
let highJackpot = Math.floor(Math.random() * 100 + 200);
let randomMember = message.client.users.cache.random();
// Sets the possible messages to be received
let trickortreatmessage = [
'The scarecrow standing behind you jumps out at you and makes you drop all your candy!',
`${randomMember} was nice enough to give you Candy! ${message.author} got ${candyGiven} pieces of candy!`,
`Oh no! ${message.author} asked ${randomMember} for Candy and they decided to egg you instead!`,
`The Headless Horseman rides and slashes his way to every bin of candy all just to give you everything he got!\n\n${message.author} got ${jackpot} Candy!`,
`The wolves howl in sync as Candy rains from the sky! The adrenaline makes you move at lightning speed to grab every piece of Candy!\n\n ${message.author} got ${highJackpot} Candy!!!`
]
// Store one of the random messages
let trickortreat;
let odds = Math.floor(Math.random() * 5000);
if (odds <= 25) trickortreat = trickortreatmessage[4]; // .5% chance
else if (odds <= 49) trickortreat = trickortreatmessage[0]; // .5% chance
else if (odds <= 499) trickortreat = trickortreatmessage[3]; // 9% chance
else if (odds <= 1999) trickortreat = trickortreatmessage[2]; // 30% chance
else trickortreat = trickortreatmessage[1]; // 60% chance
if (trickortreat == trickortreatmessage[0]) {
candyAmount[message.author.id].candyStored = 0;
} else if (trickortreat == trickortreatmessage[1]) {
candyAmount[message.author.id].candyStored += candyGiven;
} else if (trickortreat == trickortreatmessage[3]) {
candyAmount[message.author.id].candyStored += jackpot;
} else if (trickortreat == trickortreatmessage[4]) {
candyAmount[message.author.id].candyStored += highJackpot;
}
fs.writeFile('./candyheld.json', JSON.stringify(candyAmount), err => {
if (err) console.error(err);
});
const embed = new Discord.MessageEmbed()
.setColor(colors.serverRed)
.addField('\u200B', `**${trickortreat}**`)
message.delete();
message.channel.send(embed);
},
};
I had the same problem that "message.guild.members.cache" starts empty even though I hadn't done any code changes related to it. It was due to changes made by Discord.
It is easy to fix though. You need to log in to the Discord developer site > Applications, choose your application/bot, got to the "Bot" tab, scroll down to "Privileged Gateway Intents" and tick off the "PRESENCE INTENT" and "SERVER MEMBERS INTENT
":
Note that if your bot is in 100 or more servers you need to whitelist it.
After that I restarted the bot and now it works fine again.

Categories