Discord.js database using repl.it - javascript

I made a database script in repl.it and it's working but I keep getting null and then users database here is my code:
client.on("message", async (message) => {
if (message.content.toLowerCase().startsWith("#مخالفات")) {
var pfpMember = message.mentions.members.first() || message.member;
const violation = await db.get(`wallet_${message.mentions.members.first()}`);
if (violation === null) violation = "العضو ليس لديه اي مخالفات";
const pembed = new Discord.MessageEmbed()
.setTitle(`شرطة لوس سانتوس`)
.setDescription(`المخالفات المرورية :` + violation)
.setColor("RANDOM")
.setFooter("شرطة لوس سانتوس")
.setThumbnail(pfpMember.user.displayAvatarURL());
message.channel.send(pembed);
}
});
And here is a screenshot:

The issue is that you cannot reassign violation as it is a constant, not a variable.
Using the following should solve the issue:
client.on("message", async (message) => {
if (message.content.toLowerCase().startsWith("#مخالفات")) {
var pfpMember = message.mentions.members.first() || message.member;
let violation = await db.get(`wallet_${message.mentions.members.first()}`);
if (violation === null) violation = "العضو ليس لديه اي مخالفات";
const pembed = new Discord.MessageEmbed()
.setTitle(`شرطة لوس سانتوس`)
.setDescription(`المخالفات المرورية :` + violation)
.setColor("RANDOM")
.setFooter("شرطة لوس سانتوس")
.setThumbnail(pfpMember.user.displayAvatarURL());
message.channel.send(pembed);
}
});

Related

The Discord.js command I wrote does not working

I have an error in these codes.
client.on("message", async message => {
if(message.author.bot || message.channel.type === 'dm') return;
let prefix = botsettings.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = message.content.substring(message.content.indexOf(' ')+1);
if(!message.content.startsWith(prefix)) return;
let commandfile = bot.commands.get(cmd.slice(prefix.lenght)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.lenght)))
if(commandfile) commandfile.run(bot,message,args)
if(cmd === `${prefix} reactions`){
let embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('Kişiliğinize göre bir rol seçiniz.')
.setColor('GREEN')
let msgEmbed = await message.channel.send(embed).
msgEmbed.react(':closed_book:')
}
})
I am encountering this error. I couldn't find the botsettings part.
UnhandledPromiseRejectionWarning: ReferenceError: botsettings is not defined
This means that you didn't define the "botsettings" file anywhere.
Try adding
const botsettings = require ("<path to the file>")
to the top of your code.
If you're trying to make commands, try using this. I use it a lot in my bot (currently v11, too much of a hassle to update to v12. But the same code should work anyway.):
client.on("message", async (message) => {
if(message.author.bot) return;
const args = message.content.slice(botsettings.prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (message.content.indexOf(botsettings.prefix) !== 0) return;
if(command === "reactions"){
let embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('Kişiliğinize göre bir rol seçiniz.')
.setColor('GREEN')
let msgEmbed = await message.channel.send(embed).
msgEmbed.react(':closed_book:')
}
});
Before doing this, make sure you have r const botsettings = ("./path/to/your/botsettings.json"); at the top of your entire code.

Issue with Java Discord.js Modmail System

I have been trying to make a discord ModMail system on my bot and there is an error that I can't understand my code is below:
client.on('message', message => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/+ /)
if (!message.channel.name.includes('modmail')) {
return;
} else {
if (isNaN(args[0])) {
return message.channel.send('Sorry but that is not a valid user')
}
let storage = message.guild.members.cache;
let memberId = storage.find(member => member.id.toLowerCase() === args[0]);
if (memberId) {
const msg = message.content.slice(args[0].length + prefix.length).split(" ").join(" ");
let embed = new Discord.MessageEmbed()
.setThumnail(message.author.displayAvatarURL())
.setDescription(`<#${message.author.id}>\n------------\n${msg}`)
.setColor('#599cff')
.setFooter('ModMail System')
.setTimestamp();
client.users.fetch(args[0]).then(user => user.send(embed).catch(err => console.log(err)));
message.channel.send('Your message was sent to the staff team! Please be patient for your reply.')
} else {
return message.channel.send('Could not find the user sorry.');
}
}
});
client.on('message', message => {
var msg = message.content;
var n = message.author.username;
if (message.channel.type === 'dm') {
if (message.author.bot) return;
let blacklisted = ['word1', 'word2']
let foundInText = false;
for (var i in blacklisted) {
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
if (foundInText) {
return message.channel.send('Please do not use foul language in the message. (if you must please cover it with symbols *not letters*)');
}
message.channel.send('Your message was sent to the staff team! Please be patient for your reply.');
const mailMessage = `${message.author.id} ${msg}`
client.channels.fetch('795031406442315816').then(user => user.send(mailMessage).catch(err => console.log(err)));
}
});
The error that is shown is:
SyntaxError: Invalid regular expression: /+ /: Nothing to repeat
This has been confusing me for a while (I am new to code) please if you know how to fix this it would help a lot thank you.
I think the error is because there is a problem here:
const args = message.content.slice(prefix.length).split(/+ /)
If you change .split(/+ /) to .split(' ') or .split(/ /) it would work.

why aren't my discord bot commands working

im new to code and i'm just trying to make a discord bot but i made commands and i made it possible for my bot's status to say something but none of it works. it goes online and it doesn't tell me anything's wrong with my code but none of it works.
code:
const Discord = require("discord.js");
const bot = new Discord.Client();
let prefix=".";
bot.on("ready", () => {
bot.user.setStatus("idle")
console.log("lets gooo")
});
const update = () => {
bot.user.setActivity(".help | residing on " + bot.guilds.size + " Servers", { type: 'WATCHING' });
};
bot.on('ready', update);
bot.on('guildCreate', update);
bot.on('guildRemove', update);
bot.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(!command.startsWith(prefix)) return;
});
if(command === `${prefix}userinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#5ED315")
.setThumbnail( `${message.author.avatarURL}`)
.addField("Name", `${message.author.username}#${message.author.discriminator}`)
.addField("ID", message.author.id)
message.reply("check dms");
message.channel.send({embed});
};
if("command" === `${prefix}help`) {
let embed = new Discord.RichEmbed()
.addField(".help", "gives you this current information")
.setTitle("help")
.setColor("#5ED315")
.addField(".user", "gives you info about a user(currently being worked on)")
.addField(".server","gives you info about a server(currently working on it)")
.addField("link to support server","https://discord.gg/cRJk74kDvj")
.addField("invite link for bot","https://discord.com/api/oauth2/authorize?client_id=771489748651868173&permissions=8&scope=bot")
};
message.reply("here's a list of commands that i'm able to do")
message.channel.send({embed});
messageArray = message.content.split("");
let command = messageArray[0];
if(command === `${prefix}serverinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#5ED315")
.addField("Name", `${message.guild.name}`)
.addField("Owner", `${message.guild.owner.user}`)
.addField("Server ID" , message.guild.id)
.addField("User Count", `${message.guild.members.filter(m => m.presence.status !== 'offline').size} / ${message.guild.memberCount}`)
.addField("Roles", `${message.guild.roles.size}`);
message.channel.send({embed});
};
bot.login("bot token");
why isn't anything working?? i really need help
26:
27: if(!message.startsWith(prefix)) return;
28:
29: });
^^^
There's your problem. You're closing the message handler too early, so it doesn't bother to touch the command code. (Well, it does, but not the way you want it to.)
You should move that closing }); to after your command definitons. (before the bot.login)

How to allow the bot to create a file / append a file on my dropbox

Since my discord group often have quite a bit of dispute , I made a bot to record chat messages on demand to keep evidence of what people have said. However, since the bot is running on heroku, i could not let the bot dump the log into the github thing. I would need it to append to the file in the dropbox. And if a file didnt exsist, create a file for me.
I am not familliar with the dropbox api and thus wanting you guyz to help me complete the code.
p.s. if there is any problem/ bad parts of the code, feel free to help me improve it , its my first time creating a bot.
Thanks in advance
code :
https://pastebin.com/asftq3zJ
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require("fs");
//Options
const prefix = "!";
const documentation = "https://docs.google.com/spreadsheets/d/1dlUBQur9d8ANC-JJH6HA4gn4zxbFyz2xwQnTiPYZQi4/edit?usp=sharing"
//On Ready
client.on("ready",() => {
console.log("JojoTheOtokonko is ready")
client.user.setActivity("AV",{type: "WATCHING"});
});
//Login
client.login(process.env.token);
//Command Handler
client.on("message", async msg => {
let message = msg.toString();
let args = "";
if(message[0] === prefix){
args = message.substring(1).split(" ");
}
//Useful Commands
//!a
if(args[0] === "a"){
let mentioned = msg.mentions.users.first();
let server = args[1];
await mentioned.send("Hello, Would you mind joining " + server + "?\nLove you :)");
}
//!record
if(args[0] === "record" || args[0] === "rec"){
recording = true;
await msg.channel.send("#here Recoding message in " + msg.channel +" ,MIND YOUR LANGUAGE");
await client.user.setActivity("to messages in " + msg.channel, {type: "LISTENING"});
}
if(args[0] === "record-stop" || args[0] === "rec-stop"){
recording = false;
await msg.channel.send("#here stopped recoding message in " + msg.channel);
client.user.setActivity("AV",{type: "WATCHING"});
}
//help
if(args[0] === "help"){
await msg.reply("Full list of commands can be found at : \n" + documentation);
}
//Troll Commands
//!ftl
if(args[0] === "fti" || args[0] === "ftl"){
let channel = msg.member.voice.channel
if(!channel){
return msg.channel.send("You need to be in a voice channel to use this command");
}
try {
var connection = await channel.join();
} catch (e) {
console.log("Error connecting to voice channel");
}
connection.play('fti.mp3', { volume: 0.5 })
.on("finish", () => channel.leave())
.on("error", (error) => console.log(error));
}
if(args[0] === "fti-stop" || args[0] === "ftl-stop"){
if(msg.member.voice.channel){
msg.member.voice.channel.leave();
await msg.channel.send("Successfully disconnected from voice , why dont you like Fire Three Island? TAT");
}
}
//!elevator
if(args[0] === "elevator"){
await msg.channel.send("5_AvenueLocal");
}
//!innocity
if(args[0] === "inno" || args[0] === "innocity"){
await msg.channel.send("Is InnoCity Watersuno?");
}
});
//Recorder
var recording = false;
client.on("message", msg =>{
if(recording){
let server = msg.guild.name;
let appendata = "[" +Date.now()+"]" + "<" + msg.author.username + "> " + msg.toString() + "\n";
fs.appendFile(server + ".txt", appendata, (err) => {
if (err) throw err
});
}
});

ReferenceError: message is not defined - Welcome Message - embed

So I was playing with my welcome message and wanted to make it an embed, I ended up re-writing it all to work with the embed, however after I finished, I got the error message is not defined.
var welcomePath = './Storage/welcome.json';
var welcomeRead = fs.readFileSync(welcomePath);
var welcomeFile = JSON.parse(welcomeRead);
client.on('guildMemberAdd', (member) => {
var serverId = member.guild.id;
if (!welcomeFile[serverId]) {
console.log('Welcome is disabled!');
} else {
let welcomeChannel = welcomeFile[serverId].channel,
let setChannel = message.guild.channels.find(channel => channel.name === welcomeChannel);
const embed = new Discord.RichEmbed()
.setTitle("Test")
.setAuthor("Test")
.setColor(3447003)
.setDescription("Test")
.setThumbnail(message.author.avatarURL);
member.guild.channels.get(setChannel).send({
embed
});
}
});
The error pertains to this line
let setChannel = message.guild.channels.find(channel => channel.name === welcomeChannel);
I do really want to learn JS, and keep finding myself hitting this brick walls where I need to simply ask for help. I am also unsure if you fix my message is not defined that my code will actually do anything.
message is not defined, you should look for member.
let setChannel = member.guild.channels.find(channel => channel.name === welcomeChannel);
client.on('guildMemberAdd', (member) => {
var serverId = member.guild.id;
if (!welcomeFile[serverId]) {
console.log('Welcome is disabled!')
} else {
let welcomeChannel = welcomeFile[serverId].channel
let setChannel = member.guild.channels.find(channel => channel.name === welcomeChannel);
const embed = new Discord.RichEmbed()
.setTitle("Test")
.setAuthor("Test")
.setColor(3447003)
.setDescription("Test")
.setThumbnail(message.author.avatarURL)
member.guild.channels.get(setChannel).send({embed});
}
})

Categories

Resources