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
Related
I've tried multiple ways to try to get it to send but it shows no error and doesn't send into channel.
const { MessageEmbed } = require('discord.js');
client.on("ready", async () => {
const embed = new MessageEmbed()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`)
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
})
in the latest Discord.js version (V14) the correct way is
const { EmbedBuilder } = require('discord.js');
client.on("ready", async () => {
const embed = new EmbedBuilder()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`);
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
});
If this is not solving your issue,
try to add a console.log(channel) just before channel.send({embeds: [embed]})
If the result is undefined, the problem is the bot can't get in your cache the channel you want. In that case you can fetch (Link to a post speaking about this)
In the other case the bot can't send a message in the channel, could be a permission issue but you can add a .then() / .catch() to see if error is showed or not.
Hope this can helps you
I think the problem is that you don't have the client to call.
const { MessageEmbed } = require('discord.js');
client.on("ready", async (/*client not found in here*/) => {
const embed = new MessageEmbed()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`)
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
})
So try to add client
const { MessageEmbed } = require('discord.js');
client.on("ready", async(client) => {
const embed = new MessageEmbed()
.setTitle(`Bot Status!`)
.setDescription(`${client.user.username} **Is Online!**`)
const channel = client.channels.cache.get('1006667208371490946')
channel.send({embeds: [embed]})
})
My bot don't turn online but there is a simple code:
const discord = require('discord.js')
const client = new discord.Client();
const token = ('my bot token')
client.on('ready', function(){
console.log('online')
})
client.login(token)
Try this:
const discord = require('discord.js')
const client = new discord.Client();
// you will want to define some intents if you want the bot to be able to do anything
// see https://discordjs.guide/popular-topics/intents.html#enabling-intents and https://discord.js.org/#/docs/main/stable/class/Intents
const token = 'my bot token' // took away the ()
client.on('ready', () => { //removed 'function' and added => to pass everything through
console.log('online')
})
client.login(token)
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
So basically I've been working on this one bot for my server, I want it to DM the users that join the server, Like whenever a user joins my server, they would receive a DM by my bot?
I have used this code now, but it doesn't seem to work, can anyone help?
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Bot is ready!');
bot.on("guildMemberAdd", member => {
member.send("Welcome to the server!")
.catch(console.error);
});});
client.login('<token>');
you are using wrong way it is client not bot. Cause you are initial your bot as client since const client = new Discord.Client();. And there is no need to wrap it in ready event
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Bot is ready!');
});
client.on("guildMemberAdd", member => {
console.log("new member join")
member.send("Welcome to the server!").catch(console.error);
});
client.login('<token>');
I am creating a bot for discord and I try to create a command to be able to play a role, already in several places but I can't understand it well
guild.createRole({
name: 'role',
color: 'RED'
});
putting this code tells me "guild is not defined"
if you could help me I would be very grateful
You have to define the guild to be able to call methods on it. Here are a few ways you can define the guild and create a role
Retrieve the guild from a message
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', (message) => {
let guild = message.guild;
guild.createRole(...) ///put in your data here
.then(role => console.log(`Created new role with name ${role.name}`)) //What to do when it has been created
.catch(console.error); //Handle an error
}
Get the guild from guild id
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("ready", () => {
let guild = client.guilds.get("GUILD-ID"); //Replace "GUILD-ID" with the id of the guild you want
guild.createRole(...) ///put in your data here
.then(role => console.log(`Created new role with name ${role.name}`)) //What to do when it has been created
.catch(console.error); //Handle an error
}