I want to make a report command for my discord bot, so I want it to tell me who is doing the report. In other words, who executed the command. My current code is:
const Discord = require('discord.js');
const { User, ClientUser, GuildMember, TeamMember, Message} = require("discord.js");
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "USER", "REACTION"]});
const guild = client.guilds.cache.get(process.env.DISCORD_SERVER_ID);
module.exports = {
name: 'report',
description: "report a naughty person",
execute(message, args){
message.channel.send(`#${String(GuildMember.User)} reported:`);
}}
But when you use the command it says:
#undefined reported:
How do I make it define who it is?
You can use message.author to get the user and
`${message.author}`
to mention them.
message.channel.send(`${message.author} reported:`)
This is pretty simple, you just have to create a new const, which will be the following:
const author = message.author.tag
After that, just replace this statement.
message.channel.send(`${author} reported:`);
And your code should end up looking like this:
const Discord = require('discord.js');
const { User, ClientUser, GuildMember, TeamMember, Message} = require("discord.js");
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "USER", "REACTION"]});
const guild = client.guilds.cache.get(process.env.DISCORD_SERVER_ID);
const author = message.author.tag
module.exports = {
name: 'report',
description: "report a naughty person",
execute(message, args){
message.channel.send(${author} reported:);
}
}
I hope this helped!
Related
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
const { EmbedBuilder } = require('discord.js');
// All partials are loaded automatically
//const Discord = require('discord.js');
client.on('ready', async () => {
console.log(`${client.user.username} is ready!`);
})
client.on('guildCreate', (g) => {
const channel = g.channels.cache.find(channel => channel.type === 'GUILD_TEXT' && channel.permissionsFor(g.me).has('SEND_MESSAGES'))
channel.send("Thanks for inviting flappy dank! Please run the command !run to get started!");
})
client.on("messageCreate", async (message) => {
if (message.content == '!testcmd') {
const illla = new EmbedBuilder()
.setColor(FF0000)
.setTitle('Members Generator 2.0!')
.setDescription('testing testing 123 123')
.setTimestamp()
.setFooter({ text: 'wow a footer'});
message.channel.send(illla)
}
})
I have tried the code above, when I run the command ‘!testcmd’, it does not output any embed. i have searched online for solutions, but none working. I expect and embed to be outputted, yet it doesn’t return any errors. Any advice would be appreciated!
You're using discord.js v14. There's a changes about sending Embeds
const illla = new EmbedBuilder()
.setColor(FF0000)
.setTitle('Members Generator 2.0!')
.setDescription('testing testing 123 123')
.setTimestamp()
.setFooter({ text: 'wow a footer'});
message.channel.send(illla) //The Changes is in here
The comment line should change as:
message.channel.send({embeds: [illla] });
Click here to see more about EmbedBuilder
i'm new to coding bots and i need help on how to add a command on messaging a certain person a customized message. this is what i have so far. what should i do?
const Discord = require('discord.js');
const config = require("./Data/config.json")
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
client.once('ready', () => {
console.log('Ready!');
});
client.login('TOKEN');
client.on('message', async message => {
if (message.author.bot) return;
let prefix = '??'
if (!message.content.startsWith(prefix)) return;
let args = message.content.slice(prefix.length).trim().split("/ +/g");
let msg = message.content.toLowerCase();
let cmd = args.shift().toLowerCase();
if (msg.startsWith(prefix + 'ping')) {
message.channel.send('pong');
}
if (msg.startsWith(prefix + 'hello')) {
message.channel.send('hewwo uwu');
}
});
To fetch a user by id, you can use bot.users.fetch()
To direct message a user, you can use User.send():
const user = await client.users.fetch(userId);
user.send("This is a DM!");
This simple function will easily resolve the user (you can provide a user object or user ID so it’s very helpful)
async function DMUser(resolvable, content, client) {
const user = client.users.resolve(resolvable)
return user.send(content)
}
//returns: message sent, or error
//usage: DMUser(UserResolvable, "content", client)
Related links:
UserManager.resolve
User.send
UserResolvable
I am trying to log whenever a invite is created and want this to apply per guild (meaning that whenever a invite is created in guild a, it logs it. If a guild is created in guild b, it only logs it in that server.)
I am having a issue,
Issue: ReferenceError: maxAge is not defined
Here is my code:
const Discord = require("discord.js");
let config = require("../config.json");
const { MessageEmbed } = require("discord.js");
const client = new Discord.Client();
module.exports = {
name: `printinvs`,
description: `Prints invites created.`,
async execute(message, args){
message.guild.fetchInvites()
.then
(invites => {
const printinvites = new Discord.MessageEmbed()
.setTitle(`Invite Information`)
.addField(`Max Age: ${maxAge}`)
.addField(`Max Uses: ${maxUses}`)
.addField(`Temporary: ${temporary}`)
.addField(`Invite URL: ${inviteurl}`)
.addField(`Invite created by: ${inviter}`)
.addField(`Channel: ${invite.channel}`)
.addField(`Time invited created: ${invite.createdAT}`)
.setColor(`RANDOM`)
message.client.channels.cache.get(channelid).send(printinvites);
})
}
}
This error is occuring because you never declared the latest invite, and you're using its properties without the actual invite.
Try this code:
let config = require("../config.json");
const { MessageEmbed } = require("discord.js");
const client = new Discord.Client();
module.exports = {
name: `printinvs`,
description: `Prints invites created.`,
async execute(message, args){
message.guild.fetchInvites()
.then
(invites => {
let latestInvite = invites.last();
const printinvites = new Discord.MessageEmbed()
.setTitle(`Invite Information`)
.addField(`Max Age: ${latestInvite.maxAge}`)
.addField(`Max Uses: ${latestInvite.maxUses}`)
.addField(`Temporary: ${latestInvite.temporary}`)
.addField(`Invite URL: ${latestInvite.url}`)
.addField(`Invite created by: ${latestInvite.inviter}`)
.addField(`Channel: ${latestInvite.channel}`)
.addField(`Time invited created: ${latestInvite.createdAt}`)
.setColor(`RANDOM`)
message.client.channels.cache.get(channelid).send(printinvites);
})
}
}
Bot was working fine, i was updating every now and then to make some new changes, i Redo'd everything i did back when it was working perfectly yet it kept crashing.
Code:
module.exports = {
name: 'vouch',
description: "this is a vouch command!",
execute(message, args, Discord) {
const client = message.client;
const newEmbed = new Discord.MessageEmbed()
message.channel.send(newEmbed);
if (message.member.roles.cache.has('768903864782553108')) {
const targetID = message.mentions.users.first();
return message.channel.send(new Discord.MessageEmbed()
//.setTitle
.setDescription(`👍You have vouched for **${targetID}**.`)
.setColor('GREEN')
.setFooter(client.user.username, client.user.displayAvatarURL())
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setTimestamp()
.setFooter('Bot was programmed by ~', 'https://i.gyazo.com/04b40914f14d5dba8aebb532ed3e80f3.png')
)
}
}
}
The error:
throw new DiscordAPIError(request.path, data, request.method, res.status);
^
DiscordAPIError: Invalid Form Body
embed.description: This field is required
at RequestHandler.execute (C:\Users~\Desktop\Discordbot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users~\Desktop\Discordbot\node_modules\discord.js\src\rest\RequestHandler.js:39:14) {
method: 'post',
path: '/channels/683675132677718107/messages',
code: 50035,
httpStatus: 400
}
you can just simply do this:
module.exports = {
name: 'vouch',
description: "this is a vouch command!",
execute(message, args, Discord) {
if (message.member.roles.cache.has('768903864782553108')) {
const targetID = message.mentions.users.first();
const newEmbed = new Discord.MessageEmbed()
//.setTitle
.setDescription(`👍You have vouched for **${targetID}**.`)
.setColor('GREEN')
.setFooter(client.user.username, client.user.displayAvatarURL())
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setTimestamp()
.setFooter('Bot was programmed by ~', 'https://i.gyazo.com/04b40914f14d5dba8aebb532ed3e80f3.png')
message.channel.send(newEmbed)
}
}
}
EDIT: and as a side note the error originated where you called a new embed but didn't give any values for it.
const newEmbed = new Discord.MessageEmbed()
message.channel.send(newEmbed);
I would like to log when my bot joins servers (i already have the base code) I just need to know how to define the owner of the server! if you know how to do this please help me!
Well, from the "guildCreate" event, you recieve a Guild class. From which you can access the owner GuildMember class. Then you can get the information about the owner that.
Here is an example:
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", (guild) => {
console.log(`I have been added to a new guild! ${guild.name}
Owner name: ${guild.owner.user.username}`);
})
client.login("yourToken")
It's the best to read thru the docs.
For checking who invited :
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", async guild => {
const fetchedLogs = await guild.fetchAuditLogs({
limit: 1,
type: "BOT_ADD"
});
const auditlog = fetchedLogs.entries.first();
let myChannel = client.channels.cache.find(channel => channel.id == YOUR_CHANNEL_ID);
myChannel.send(auditlog.executor.tag + " added bot in "+ guild.name+"\nOwner is:
"guild.owner.user.username);
});
Also you can check for VIEW_AUDIT_LOGS permission