quick.db leveling system - database.updateValue is not a function - javascript

I'm trying to make a leveling system with a discord bot using quick.db. I've been working with this for a while and couldn't figure it out so I figured I'd go here. My current code is:
(app.js)
//Message Leveling
database.updateValue(message.author.id + message.guild.id, 1).then(i => {
let messages;
if (i.value = 25) messages = 25; // Level 1
else if (i.value == 50) messages = 50; // Level 2
else if (i.value == 75) messages = 75; //Level 3
if (!isNaN(messages)) { // If messages iss STILL empty, run this
database.updateValue(`userLevel_${message.author.id + message.guild.id}`, 1).then(o => {
message.channel.send(`You sent ${messages} messages, LEVEL UP HOME BOY! level ${o.value}`)
})
}
})
(messages.js)
const db = require('quick.db');
var database = require('quick.db')
exports.run = (bot, message, args, func) => {
database.fetchObject(message.author.id + message.guild.id).then(i => {
database.fetchObject(`userLevel_${message.author.id + message.guild.id}`).then(o => {
message.channel.send('Messages sent: `' + (i.value + 1) + '`\nLevel: `' + o.value +'`');
})
})
}
Now, the error I get happens in app.js but I figured the code from messages.js might be helpful. The error is:
[help] database.updateValue(message.author.id + message.guild.id, 1).then(i => {
[help] ^
[help]
[help] TypeError: database.updateValue is not a function
Being new to this I still don't quite understand what a TypeError is or how to fix it, despite looking it up on google (I know, I'm a real researcher). So I was hoping someone could give me a hand. I also couldn't find an example of this error, so I'm pretty lost.
As always, thanks for taking the time to read my question, if I got any terminology wrong feel free to ask me about what I mean, or you can just call me stupid.
<3

database.updateValue isn't a function, instead you would want to use database.addlike:
database.add(`level_${message.guild.id}_${message.author.id}`, 1).then(i => {
let messages;
if (i.value = 25) messages = 25; // Level 1
else if (i.value == 50) messages = 50; // Level 2
else if (i.value == 75) messages = 75; //Level 3
if (!isNaN(messages)) { // If messages iss STILL empty, run this
database.add(`level_${message.guild.id}_${message.author.id}`, 1).then(o => {
message.channel.send(`You sent ${messages} messages, LEVEL UP HOME BOY! level ${o.value}`)
})
}
})
For fetching databases, fetchObject isn't a function, use fetch or get
const db = require('quick.db');
var database = require('quick.db')
exports.run = (bot, message, args, func) => {
db.fetch(level_${message.guild.id}_${message.author.id}).then(i => {
db.fetch(level_${message.guild.id}_${message.author.id}).then(o => {
message.channel.send('Messages sent: `' + (i.value + 1) + '`\nLevel: `' + o.value +'`');
})
})
}
If you've defined quick.db as db then instead of calling database, call db otherwise it just wouldn't work.
Thanks,
Chills

Related

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.

How to create an item XP based leveling system for Discord game bot

I am making a Discord bot that is essentially a remake of RuneScape (learning JavaScript right now and RS is my favorite game :P). You type in command /mine and it mines some ores for you. I have it to where there is a /help command and /mine command. I'm looking to add an item XP based leveling system. Each "ore" (item you're mining) has a set XP value so for example, Dirt = 5XP and Stone = 10XP so each time I mine a piece of stone I get 10XP and then it adds up until I reach the level mark. Level 1-10 is 1000XP and it exponentially gets larger on a scale of 1.5 I'm just not sure how to implement this into the program.
I'm only been using JavaScript and Discord.js for a few months, so please don't roast my code too hard haha.
Here is the program:
const Discord = require('discord.js');
const client = new Discord.Client();
const dirt = (Math.floor(Math.random() * 12) + " <:dirt:679475976274575391> Dirt");
const stone = (Math.floor(Math.random() * 8) + " <:stone:679476006758907904> Stone");
const ores = [dirt, stone];
client.on('ready', () => {
console.log("Connected as " + client.user.tag)
})
client.on('message', (receivedMessage) => {
if (receivedMessage.author == client.user) {
return
}
if (receivedMessage.content.startsWith("/")) {
processCommand(receivedMessage)
}
})
function processCommand(receivedMessage) {
let fullCommand = receivedMessage.content.substr(1)
let splitCommand = fullCommand.split(" ")
let primaryCommand = splitCommand[0]
let arguments = splitCommand.slice(1)
console.log("Command received: " + primaryCommand)
console.log("Arguments: " + arguments)
if (primaryCommand == "help") {
helpCommand(receivedMessage)
} else if (primaryCommand == "mine") {
mineCommand(receivedMessage)
} else if (primaryCommand == "level") {
levelCommand(receivedMessage)
}
}
function randomOre() {
return ores[Math.floor(Math.random() * ores.length)];
}
function ore() {
if (randomOre() == dirt) {
return (dirt)
} else if (randomOre() == stone) {
return (stone)
} else if (randomOre() == dirt && stone) {
return (dirt + "\n" + stone)
}
}
function helpCommand(receivedMessage) {
const helpEmbed = new Discord.RichEmbed()
.setColor('#0099ff')
.setTitle('Help')
.setAuthor('Virtual Miner')
.setDescription('List of all available commands here.')
.addField(name='**/mine**', value='Allows users to mine for ores and collect them.')
.addField('/shop - Gives users access to the shop where you can buy better pickaxes, upgrades, boosts, and minecarts.')
.setFooter('Virtual Miner | Made by Aqyl#0093')
receivedMessage.channel.send(helpEmbed)
}
function mineCommand(receivedMessage) {
user = receivedMessage.member.user.tag;
const mineEmbed = new Discord.RichEmbed()
.setColor('#0099ff')
.setAuthor(user)
.addField(name="**You found: **", value=ore())
.setFooter('Virtual Miner | Made by Aqyl#0093')
receivedMessage.channel.send(mineEmbed)
}
bot_secret_token = "*********************************************"
client.login(bot_secret_token)
Either documentation, videos, or just a plain old explanation would be really helpful. Thanks!
I recommend this tutorial from discordjs guide as it teaches you how to build a currency system. You can adapt the code and change it into a message based experience system with a little work.
it uses a Sequelize V5 database to store the data for it

Why doesn't kicking people work using discord.js

const Discord = require("discord.js"),
bot = new Discord.Client();
let pre = "?"
bot.on("message", async msg => {
var msgArray = msg.content.split(" ");
var args = msgArray.slice(1);
var prisonerRole = msg.guild.roles.find("name", "Prisoner");
let command = msgArray[0];
if (command == `${pre}roll`) {
if (!msg.member.roles.has(prisonerRole.id)) {
roll = Math.floor(Math.random()*6)+1;
msg.reply(`You rolled a ${roll}`)
} else {
msg.reply(`HaHa NOOB, you're in prison you don't get priveleges!`)
}
}
if (command == `${pre}kick`) {
var leaderRole = msg.guild.roles.find("name", "LEADER");
var co_leaderRole = msg.guild.roles.find("name", "CO-LEADER");
if (msg.member.roles.has(leaderRole.id) ||
msg.member.roles.has(co_leaderRole.id)) {
var kickUser = msg.guild.member(msg.mentions.users.first());
var kickReason = args.join(" ").slice(22);
msg.guild.member(kickUser).kick();
msg.channel.send(`${msg.author} has kicked ${kickUser}\nReason: ${kickReason}`);
} else {
return msg.reply("Ya pleb, you can't kick people!");
}
}
})
bot.login("token").then(function() {
console.log('Good!')
}, function(err) {
console.log('Still good, as long as the process now exits.')
bot.destroy()
})
Everything works except actually kicking the person. The message sends nut it doesn't kick people. For example, when I type in ?kick #BobNuggets#4576 inactive, it says
#rishabhase has kicked #BobNuggets
Reason: inactive
But it doesn't actually kick the user, which is weird, can you help me?
Change
msg.guild.member(kickUser).kick();
to
kickUser.kick();
also, make sure the bot is elevated in hierarchy
Use kickUser.kick();
I recommend using a command handler to neaten up your code. You don't want all your commands in one .js file.
Try something like this for the Ban command itself. I use this for my Bot:
client.on("message", (message) => {
if (message.content.startsWith("!ban")) {
if(!message.member.roles.find("name", "Role that can use this bot"))
return;
// Easy way to get member object though mentions.
var member= message.mentions.members.first();
// ban
member.ban().then((member) => {
// Successmessage
message.channel.send(":wave: " + member.displayName + " has been successfully banned :point_right: ");
}).catch(() => {
// Failmessage
message.channel.send("Access Denied");
});
}
});
That should work, set the role you want to use it (cAsE sEnSiTiVe) and change !ban to whatever you feel like using. If you change all "ban"s in this to kick, it will have the same effect. If this helped you, mark this as the answer so others can find it, if not, keep looking :)

Firebase function execution and subscription to list that is being updated by a firebase function

I think a firebase function updating a list that I have in the firebase database is being captured by a subscription that is subscribed to that list. From what the list output looks like on my phone (in the app)...and from what my console output looks like (the way it repeats) it seems like it is capturing the whole list and displaying it each time one is added. So (I looked this up)...I believe this equation represents what is happening:
(N(N + 1))/2
It is how you get the sum of all of the numbers from 1 to N. Doing the math in my case (N = 30 or so), I get around 465 entries...so you can see it is loading a ton, when I only want it to load the first 10.
To show what is happening with the output here is a pastebin https://pastebin.com/B7yitqvD.
In the output pay attention to the array that is above/before length - 1 load. You can see that it is rapidly returning an array with one more entry every time and adding it to the list. I did an extremely rough count of how many items are in my list too, and I got 440...so that roughly matches the 465 number.
The chain of events starts in a page that isn't the page with the list with this function - which initiates the sorting on the firebase functions side:
let a = this.http.get('https://us-central1-mane-4152c.cloudfunctions.net/sortDistance?text='+resp.coords.latitude+':'+resp.coords.longitude+':'+this.username);
this.subscription6 = a.subscribe(res => {
console.log(res + "response from firesbase functions");
loading.dismiss();
}, err => {
console.log(JSON.stringify(err))
loading.dismiss();
})
Here is the function on the page with the list that I think is capturing the entire sort for some reason. The subscription is being repeated as the firebase function sorts, I believe.
loadDistances() {
//return new Promise((resolve, reject) => {
let cacheKey = "distances"
let arr = [];
let mapped;
console.log("IN LOADDISTANCES #$$$$$$$$$$$$$$$$$$$$$");
console.log("IN geo get position #$$$$$$$5354554354$$$$$$$");
this.distancelist = this.af.list('distances/' + this.username, { query: {
orderByChild: 'distance',
limitToFirst: 10
}});
this.subscription6 = this.distancelist.subscribe(items => {
let x = 0;
console.log(JSON.stringify(items) + " length - 1 load");
items.forEach(item => {
let storageRef = firebase.storage().ref().child('/settings/' + item.username + '/profilepicture.png');
storageRef.getDownloadURL().then(url => {
console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!");
item.picURL = url;
}).catch((e) => {
console.log("in caught url !!!!!!!$$$$$$$!!");
item.picURL = 'assets/blankprof.png';
});
this.distances.push(item);
if(x == items.length - 1) {
this.startAtKey4 = items[x].distance;
}
x++;
})
//this.subscription6.unsubscribe();
})
}
The subscription in loadDistances function works fine as long as I don't update the list from the other page - another indicator that it might be capturing the whole sort and listing it repeatedly as it sorts.
I have tried as as I could think of to unsubscribe from the list after I update...so then I could just load the list of 10 the next time the page with the list enters, instead of right after the update (over and over again). I know that firebase functions is in beta. Could this be a bug on their side? Here is my firebase functions code:
exports.sortDistance = functions.https.onRequest((req, res) => {
// Grab the text parameter.
var array = req.query.text.split(':');
// Push the new message into the Realtime Database using the Firebase Admin SDK.
// Get a database reference to our posts
var db = admin.database();
var ref = db.ref("profiles/stylists");
var promises = [];
// Attach an asynchronous callback to read the data at our posts reference
ref.on("value", function(snapshot) {
//console.log(snapshot.val());
var snap = snapshot.val();
for(const user in snap) {
promises.push(new Promise(function(resolve, reject) {
var snapadd = snap[user].address;
console.log(snapadd + " snap user address (((((((())))))))");
if(snapadd != null || typeof snapadd != undefined) {
googleMapsClient.geocode({
address: snapadd
}).asPromise()
.then(response => {
console.log(response.json.results[0].geometry.location.lat);
console.log(" +++ " + response.json.results[0].geometry.location.lat + ' ' + response.json.results[0].geometry.location.lng + ' ' + array[0] + ' ' + array[1]);
var distanceBetween = distance(response.json.results[0].geometry.location.lat, response.json.results[0].geometry.location.lng, array[0], array[1]);
console.log(distanceBetween + " distance between spots");
var refList = db.ref("distances/"+array[2]);
console.log(snap[user].username + " snap username");
refList.push({
username: snap[user].username,
distance: Math.round(distanceBetween * 100) / 100
})
resolve();
})
.catch(err => { console.log(err); resolve();})
}
else {
resolve();
}
}).catch(err => console.log('error from catch ' + err)));
//console.log(typeof user + 'type of');
}
var p = Promise.all(promises);
console.log(JSON.stringify(p) + " promises logged");
res.status(200).end();
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
});
What is weird is, when I check the firebase functions logs, all of this appears to only run once...but I still think the subscription could be capturing the whole sorting process in some weird way while rapidly returning it. To be as clear as possible with what I think is going on - I think each stage of the sort is being captured in an (N(N + 1))/2...starting at 1 and going to roughly 30...and the sum of the sorting ends up being the length of my list (with 1-10 items repeated over and over again).
I updated to angularfire2 5.0 and angular 5.0...which took a little while, but ended up solving the problem:
this.distanceList = this.af.list('/distances/' + this.username,
ref => ref.orderByChild("distance").limitToFirst(50)).valueChanges();
In my HTML I used an async pipe, which solved the sorting problem:
...
<ion-item *ngFor="let z of (distanceList|async)" no-padding>
...

Discord bot with js - for loop not working correctly

I am making a discord bot using js, but for some reason I can not get the bot to print an array properly via a for loop. The code is below
const Discord = require('discord.js');
const getJSON = require('get-json')
const BotToken = "tokenid";
const TwitchClientID = "?client_id=clientid";
const TwitchAPI = "https://api.twitch.tv/kraken/streams/";
const bot = new Discord.Client();
var channelNames = ["channel1", "channel2", "channel3"];
bot.login('botloginid');
// !Live command that itterates through channelNames array and prints whether stream is currently live or not
bot.on("message", function(message) {
if (message.content === "!Live") {
for (var i = 0; i < channelNames.length; i++) {
getJSON(TwitchAPI+channelNames[i]+TwitchClientID, function(error, response) {
if (response.stream == null) {
message.reply(channelNames[i] + ' is currently not live');
} else {
message.reply(channelNames[i] + ' is currently live');
}
});
}
}
});
If I put in a message.reply('i is ' + i); before getJSON, it prints out 0 1 2, which is correct, but if I put message.reply('i is ' + i); after getJSON, it prints out, i is 3, i is 3, i is 3. And because the array indexes are 0,1,2 , returning 3 makes the bot display 'undefined is live/not live' rather than the name of the channel. I've never had a problem with loops in the past and I cant understand why it would be changing to 3 under getJSON and not even iterating as the loop is definitely working.

Categories