I've been programming a discord bot, i have figured out how to export the numbers and usernames to a JSON file for each user, containing an object with the numbers i want, and their usernames, the files are saved as the users ID.json. problem is i need to find which user has the highest number from the "uses" part of the object, but i cannot figure out how i can make a script to search each files object, each file only having one object, For the file with the highest number, then give me the username/ID of the user. each files structure is ||{"uses":(number of uses),"username":"(their username)"}|| sorry if this shouldn't go in this subreddit, but i was my first place to turn after a while of searching for help. thank youEDIT:Im not the best with javascript, just a warningEDIT2: The codeconst Discord = require('discord.js') const client = new Discord.Client() fs = require("fs") //Command Time Sync var date = new Date() var logtime = (String("Month: " + (date.getMonth() + 1) + " Day: " + date.getDate() + " Time:" + date.getHours() + "-" + date.getMinutes() + "-" + date.getSeconds())) client.on('ready', () => { //client.user.setActivity("Hugs in Total: " + hugs) // List servers the bot is connected to console.log("Servers:") client.guilds.forEach((guild) => { console.log(" - " + guild.name) }) }) client.on('error', console.error); console.log("Error: \n" + console.error) client.on('error', error => { console.log(error); }); client.on('message', (receivedMessage) => { //client.user.setActivity("With Foxes") //Info For Playing Data if (receivedMessage.author == client.user) { // Prevent bot from responding to its own messages return } if (receivedMessage.content.startsWith("=w=")) { return }else if (receivedMessage.content.startsWith("f!feed")) { count(receivedMessage) } }) function count(receivedMessage){ try{ var userfile = ("./users/" + receivedMessage.author.id + ".json") if (fs.existsSync(userfile)) { var jsondata = (fs.readFileSync(userfile, 'utf8')) var parseddata = JSON.parse(jsondata) var usecount = parseddata.uses }else{ var usecount = 0 } var newuserdata = { "uses":usecount + 1, "username":(receivedMessage.author.username) } var newusersting = JSON.stringify(newuserdata) fs.writeFileSync(userfile,newusersting) console.log( receivedMessage.author.username + " now has " + newuserdata.uses + " feeds") } catch(err){ console.log(err) } } // Get your bot's secret token from: // https://discordapp.com/developers/applications/ // Click on your application -> Bot -> Token -> "Click to Reveal Token" bot_secret_token = "HIDDEN" client.login(bot_secret_token)
Submitted October 20, 2019 at 01:57AM by Noah_The_MLG
No comments:
Post a Comment