So I updated my bot to V12 in V11 the +message.author.username+ mentions the user but in V12 in does not. I have a command that says Person: Donated 10 coins to #user in V12 using the +message.author.usernameonly has it say Person: DOnated 10 coins to user
Here is the code
const Jwork = require('../../beg.json');
const JworkR = Jwork[Math.floor(Math.random() * Jwork.length)];
var random = Math.floor(Math.random() * 20) + 3;
let curBal = bal[message.author.id].balance
bal[message.author.id].balance = curBal + random;
fs.writeFile('././database/balance.json', JSON.stringify(bal, null, 2), (err) => {
message.channel.send(`
**${JworkR}** has donated ${random} coins to ` +message.author.username+ `!`)
if (err) console.log(err)
});
Do I need to do something in order to have it mention a user?
"<#" + msg.author.id + ">"
`<#${msg.author.id}>`
Works, you could also do msg.reply() but it sorta has a fixed formatting
Related
The bot says that I robbed someone but does not add/substract the currency from neither of the users.
Another issue I found is that I can rob myself.
Code I used:
const Discord = require("discord.js");
const db = require("quick.db");
const ms = require("parse-ms");
module.exports.run = async (bot, message, args) => {
if(!message.content.startsWith('db!'))return;
let user = message.mentions.members.first()
let targetuser = await db.fetch(`money_${message.guild.id}_${user.id}`)
let author = await db.fetch(`rob_${message.guild.id}_${user.id}`)
let author2 = await db.fetch(`money_${message.guild.id}_${user.id}`)
let timeout = 600000;
if (author !== null && timeout - (Date.now() - author) > 0) {
let time = ms(timeout - (Date.now() - author));
let timeEmbed = new Discord.RichEmbed()
.setColor("#FFFFFF")
.setDescription(`❌ You have already robbed someone\n\nTry again in ${time.minutes}m ${time.seconds}s `);
message.channel.send(timeEmbed)
} else {
let moneyEmbed = new Discord.RichEmbed()
.setColor("#FFFFFF")
.setDescription(`❌ You need atleast 200 dabloons in your wallet to rob someone`);
if (author2 < 200) {
return message.channel.send(moneyEmbed)
}
let moneyEmbed2 = new Discord.RichEmbed()
.setColor("#FFFFFF")
.setDescription(`❌ ${user.user.username} does not have anything you can rob`);
if (targetuser < 0) {
return message.channel.send(moneyEmbed2)
}
let vip = await db.fetch(`bronze_${user.id}`)
if(vip === true) random = Math.floor(Math.random() * 200) + 1;
if (vip === null) random = Math.floor(Math.random() * 100) + 1;
let embed = new Discord.RichEmbed()
.setDescription(`✔️ You robbed ${user} and got away with ${random} dabloons!`)
.setColor("#FFFFFF")
message.channel.send(embed)
db.subtract(`money_${message.guild.id}_${user.id}`, random)
db.add(`money_${message.guild.id}_${user.id}`, random)
db.set(`rob_${message.guild.id}_${user.id}`, Date.now())
};
}
module.exports.help = {
name:"rob",
aliases: [""]
}
I tried using code from other people, but the code I used did not work and just broke the bot
To solve the db issue, you can see Elitezen's comment
To solve the issue where you can rob yourself,
you can simply check if the person getting robbed is the author of the message
if(message.mentions.members.first().id === message.member.id) return message.channel.send(errorEmbed)
Also, this is a super outdated version of discord.js and is highly recommended to update.
This is my first post here, sorry in advance if the formatting isn't great.
I'll try to keep it short and sweet. I've made a small bot with discord.js v13 and it's main purpose is to check if new people that join is part of another guild, if they are they get a role and if they had a nickname, they'll get that assigned. The bot is on both servers with full admin rights and the server members intent is enabled. I've tried all sorts of things.
Currently, the code looks like this:
const {other_server_ID, role_ID, text_channel_ID} = require("../config.json");
module.exports = {
name: 'guildMemberAdd',
async execute( member, bot ) {
//Log the newly joined member to console
console.log(member.user.tag + ' has joined the server!');
if(member.bot) return
const text_channel = member.guild.channels.cache.get(text_channel_ID)
const role = member.guild.roles.cache.get(role_ID)
text_channel.send(member.user.tag + ' has joined the server!')
const other_server = bot.guilds.cache.get(other_server_ID)
const other_server_member = other_server.members.cache.get(member.user.id)
console.log(other_server_member)
if (other_server_member === null) text_channel.send(member.user.tag + " was not a member of other_server :(")
else {
text_channel.send(member.user.tag + " was a member of other_server!")
if(!other_server_member.nickname){
text_channel.send(member.tag + " had no nickname on other_server.")
}
else {
await member.setNickname(other_server_member.nickname)
text_channel.send("Their old nickname, " + other_server_member.nickname + ", was returned to them! :)")
}
if(!member.guild.roles.cache.get(role_ID)){
text_channel.send("Role not found.")}
else{
await member.roles.add(role)
text_channel.send("They were given the role " + role.name)
}
}
}
}
As you can see, I am trying to get the other server (it works that far) and then try to grab the user from the other server's cache. Currently I try with get and the user.id but I have also tried:
const other_server_member = other_server.members.cache.find(mem => mem.user.id === member.user.id) || other_server.members.cache.find(mem => mem.user.tag === member.user.tag) || null
I found that code snippet during my search for a solution. So far I always got errors because other_server_member was always undefined/null. I also tried putting await infront of the find/get part.
Thanks for your help and time.
UPDATE:
It works fine with fetch(), code now looks like this:
const {other_server_ID, role_ID, text_channel_ID} = require("../config.json");
module.exports = {
name: 'guildMemberAdd',
async execute( member, bot ) {
//Log the newly joined member to console
console.log(member.user.tag + ' has joined the server!');
if(member.bot) return
const text_channel = member.guild.channels.cache.get(text_channel_ID)
const role = member.guild.roles.cache.get(role_ID)
text_channel.send(member.user.tag + ' has joined the server!')
const other_server = bot.guilds.cache.get(other_server_ID)
const other_server_member = null
try {
other_server_member = await other_server.members.fetch(member.user.id)
} catch (e) {
console.log(e)
return text_channel.send(member.user.tag + " was not a member of " + pluto.name + ".")
}
console.log(other_server_member)
if (other_server_member === null) text_channel.send(member.user.tag + " was not a member of other_server :(")
else {
text_channel.send(member.user.tag + " was a member of other_server!")
if(!other_server_member.nickname){
text_channel.send(member.tag + " had no nickname on other_server.")
}
else {
await member.setNickname(other_server_member.nickname)
text_channel.send("Their old nickname, " + other_server_member.nickname + ", was returned to them! :)")
}
if(!member.guild.roles.cache.get(role_ID)){
text_channel.send("Role not found.")}
else{
await member.roles.add(role)
text_channel.send("They were given the role " + role.name)
}
}
}
}
I'm working on a randominteger slash command in discord.js
It supposed to work like this :
Discord User : /randominteger minimal:1 maximal:10
Bot : (embed)
Random number
4
1 - 10
But instead the bot just says "This interaction failed"
I don't know why the bot says that though.
Can someone explain how it happened and how to fix it?
const { SlashCommandBuilder, SlashCommandStringOption } = require('#discordjs/builders');
const { MessageEmbed } = require('discord.js')
const color = "#ffed47"
module.exports = {
data: new SlashCommandBuilder()
.setName('randominteger')
.setDescription('Replies with a random integer!')
.addIntegerOption(option =>
option.setName('minimal')
.setDescription('The minimal integer for the random integer')
.setRequired(true))
.addIntegerOption(option =>
option.setName('maximal')
.setDescription('The maximal integer for the random integer')
.setRequired(true)),
async execute(interaction) {
const minimal = interaction.options.getInteger("minimal")
const maximal = interaction.options.getInteger("maximal")
minimal = Math.ceil(minimal)
maximal = Math.floor(maximal)
const randomNumber = Math.floor(Math.random() * (maximal+0.1 - minimal) + minimal);
const random = new MessageEmbed()
.setColor(color)
.setTitle("Random Number")
.setDescription(String(randomNumber))
.setFooter(String(minimal) + " - " + String(maximal))
await interaction.reply({embeds : [random]})
},
};
Sorry, I just didn't implement the the command properly xd
It worked.. with a 1 thing, you need to change the minimal and maximal variable to "var" not "const" (so the minimal and maximal variable can be used)
I am doing an economic system at my discord bot. It went well but when I use some subtract command or withdraw command or your balance will be subtract
it got to negative number like I minus my balance to 50 but my balance is only 25 now my balance is now -25 how to fix it??
My balance file:
module.exports ={
name:'bal',
description: "bal for user",
execute(message, args, Discord, db){
const target = message.mentions.users.first() || message.author
const targetid = target.id
if(db.get(`user_${targetid}.bal`) === null){
message.channel.send('you need to create an account')
}else{
let bal = db.get(`user_${targetid}.bal`)
let bank = db.get(`user_${targetid}.bank`)
let embed = new Discord.MessageEmbed()
.setTitle(`${target.username} BALANCE`)
.setColor('GREEN')
.setDescription(`coins: ${bal} Cheese Coins \nbank: ${bank} Cheese Coins`)
message.channel.send(embed)
}
}
}
My substract file:
module.exports ={
name:'sub',
description: "sub",
execute(message, args, Discord, db){
const hey = message.mentions.users.first()
const number = args[1]
if(!hey){
message.channel.send('Mention someone or ill take yours')
}else if(!number){
message.channel.send('Put a number, take it or leave it')
}else{
let embed = new Discord.MessageEmbed()
.setTitle('Poor speedrun Any%')
.setColor('YELLOW')
.setDescription(`Total of ${number} money has revoked been from ${hey}, **SAD ${hey} NOISES**`)
db.subtract(`user_${hey.id}.bal`, number)
message.channel.send(embed)
}
}
}
Verify if the userbalance is less than the amount you trying to substract.
const userbalance = db.get(`user_${hey.id}.bal`);
if(userbalance < number) return;
db.subtract(`user_${hey.id}.bal`, number)
I'm someone who just got into coding recently.
I'm trying to create a Discord bot that can do custom roles that can allow a user of that custom role to edit its hex or name.
There's also the creation of a role creator that also assigns the mentioned user the role.
My problem is that I've encountered an issue with the role creation + assignment and the commands that allow the user to edit their role.
Here are the problems:
(node:6288) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined for the role creator command
When I try to use the commands that allow the user to customize their roles, the commands don't work, or to be more specific, they don't activate.
I do believe that the codes I'm using could be outdated, therefore if that is the case, please give me the exact location and the right code.
Here are some pictures of the codes. I've pasted the codes below, but I'm a bit new to this forum, so please pay attention to the separating pieces like "Role Customization Code:" to distinguish which one's which. For some reason, the module.exports aren't going in there. Please describe and let me know a solution to these problems in a specific way since I am a newbie at this topic.
Role creation command + assignment error.
Role creation command + assignment code part 1.
Role creation command + assignment code part 2.
Role customization code part 1.
Role customization code part 2.
Role customization code part 3.
Role creation + assignment code:
const Discord = module.require("discord.js");
const guild = require("./config.json");
module.exports.run = async (bot, message, args) => {
const query = bot.db;
function embedFail(text) {
let embed = new Discord.RichEmbed().setColor("#ff0000").setDescription(text);
return embed;
}
function embedSuccess(text) {
let embed = new Discord.RichEmbed().setColor("#7CFC00").setDescription(text);
return embed;
}
if (!args[0]) return message.channel.send("Please specify a user to add!");
let toAdd = message.guild.members.get(args[0]) || message.guild.members.get(message.mentions.users.first().id);
let rolejoin = args.slice(1).join(" ");
let myRole = message.guild.roles.cache.get((val) => val.name.toLowerCase() === rolejoin.toLowerCase());
if (!toAdd) return message.channel.send("User not found!");
if (!rolejoin) return message.channel.send("Please specify a role to add!");
let botRole = message.guild.members.get(bot.user.id).highestRole;
let isPosition = botRole.comparePositionTo(myRole);
if (isPosition <= 0) return message.channel.send(embedFail("This role is higher than me, I cannot add this role!"));
let res = await query(`SELECT * FROM user_roles WHERE role_id='${myRole.id}'`);
if (res[0]) {
await query(`DELETE FROM user_roles where role_id='${myRole.id}'`);
toAdd.removeRole(myRole);
message.channel.send(embedSuccess(`Removed role ${myRole.name} from ${toAdd.user.username}`));
} else if (!res[0]) {
await query(`INSERT INTO user_roles (guild_id, user_id, role_id) VALUES ('${message.guild.id}',${toAdd.id},'${myRole.id}')`);
toAdd.addRole(myRole);
message.channel.send(embedSuccess(`Adding role ${myRole.name} to ${toAdd.user.username}`));
}
};
Role Customization Code:
const Discord = module.require("discord.js");
const guild = require("./config.json");
module.exports.run = async (bot, message, args, db) => {
const query = bot.db;
function embedFail(text) {
let embed = new Discord.RichEmbed().setColor("#ff0000").setDescription(text);
return embed;
}
function embedSuccess(text) {
let embed = new Discord.RichEmbed().setColor("#7CFC00").setDescription(text);
return embed;
}
let res = await query(`SELECT * FROM user_roles WHERE guild_id = '${message.guild.id}' AND user_id = '${message.author.id}'`);
if (!args[0]) {
if (!res[0]) return message.channel.send(embedFail("You do not have a role!"));
let myRole = message.guild.roles.cache.find(res[0].role_id);
let assignNames = "";
let embed = new Discord.RichEmbed()
.setAuthor(`Current role assigned to ${message.author.username}`, message.guild.iconURL)
.setColor(myRole.hexColor)
.addField(`Name`, `${myRole.name}`, true)
.addField(`Colour`, `${myRole.hexColor}`, true);
message.channel.send(embed);
} else {
if (!res[0]) return message.channel.send(embedFail("You do not have a role!"));
let myRole = message.guild.roles.cache.find(res[0].role_id);
if (!myRole) {
await query(`DELETE FROM user_roles where guild_id = '${message.guild.id}' AND role_id = '${res[i].role_id}' and user_id = '${res[i].user_id}'`);
return message.channel.send(embedFail("You do not have a role!"));
}
switch (args[0]) {
case "name":
let name = args.slice(1).join(" "),
oldName = myRole.name;
await myRole.setName(name);
await message.channel.send(embedSuccess(`You have changed ${oldName}'s name to ${name}!`));
break;
case "color":
case "color":
let hexCode = args[1],
oldHex = myRole.hexColor;
await myRole.setColor(hexCode);
await message.channel.send(embedSuccess(`You have changed ${myRole.name}'s color from ${oldHex} to #${hexCode}!`));
break;
}
}
};