I am trying to make it so when my bot is added to another server, it will send an embed saying how many servers it's in now and the guild name and also the guild owner. I am also trying to make another embed so it tells me when it leaves a server and tells me when it joined the server first and then when it was removed and the guild name and guild owner. I use discord.js. Could someone help, please? This is my current script:
bot.on("guildCreate", guild => {
const joinserverembed = new Discord.MessageEmbed()
.setTitle("Joined a server!")
.addField("Guild name:", `${guild.name}`)
.addField("Time of join:", `${Discord.Guild.createdTimestamp()}`)
.setColor("GREEN")
.setThumbnail(guild.displayAvatarURL())
if (guilds.channel.id = 740121026683207760) {
channel.send(joinserverembed)
}
guild.channel.send("Thank you for inviting Ultra Bot Premium! Please use up!introduction and up!help for the new perks and more!")
})
bot.on("guildDelete", guild => {
const leftserverembed = new Discord.MessageEmbed()
.setTitle("Left a server!")
.addField("Guild name:", `${guild.name}`)
.addField("Time of removal:", `${createdTimestamp()}`)
.setColor("RED")
.setThumbnail(guild.displayAvatarURL())
if (guilds.channel.id = 740121026683207760) {
channel.send(leftserverembed)
}
})
I have resolved your first issue for you in the code below.
You were doing guild.channel.send(), in this case, guild represents a Discord.Guild however you're using it like it represents an instance of Message, which it does not.
You can use guild.channels.cache.find(x => x.name == 'general').send("Thanks for inviting me to this server¬!") will send a message to a channel named general in that server.
bot.on("guildCreate", (guild) => {
const joinserverembed = new Discord.MessageEmbed()
.setTitle("Joined a server!")
.addField("Guild name:", guild.name)
.addField("Time of join:", Date.now())
.setColor("GREEN")
.setThumbnail(guild.iconURL({ dynamic: true }));
bot.channels.cache.get("740121026683207760").send(joinserverembed);
guild.channels.cache
.filter((c) => c.type === "text")
.random()
.send(
"Thank you for inviting Ultra Bot Premium! Please use up!introduction and up!help for the new perks and more!"
);
});
I filter the channels in the guild, ensuring that they are not categories or voice channels, then send the welcome message to a random one.
As for your second query, you need to use a database, store the Date.now timestamp of when it was added, then once the bot has left the guild it must get the value and display its time. I haven't done this for you, but I have fixed your code:
bot.on("guildDelete", (guild) => {
const leftserverembed = new Discord.MessageEmbed()
.setTitle("Left a server!")
.addField("Guild name:", guild.name)
.addField("Time of removal:", Date.now())
.setColor("RED")
.setThumbnail(guild.iconURL({ dynamic: true }));
bot.channels.cache.get("740121026683207760").send(leftserverembed);
});
Related
im trying to do an embed, but every time i try using it, it says "DiscordAPIError: Cannot send an empty message"
Here is the embed and everything related to it:
client.on('message',(message) => {
if(message.author.bot) return;
let botMessageLog = new Discord.MessageEmbed()
.setAuthor(`Logs`, client.user.avatarURL())
.setDescription(`**${message.author.toString()}** said: **${message.content}** in channel **${message.channel.toString()}**`)
.setFooter(`Log system`, client.user.avatarURL())
.setTimestamp()
.setColor("FF0000");
bot.channels.cache.get("1018767166218182668").send(botMessageLog)
});
You are doing many things wrong if you are using discord js v 14
hare is a quick fix
const {Client, EmbedBuilder} = require("discord.js")
//define a new client
client.on('messageCreate',(message) => {
if(message.author.bot) return;
let botMessageLog = new EmbedBuilder()
.setAuthor({name:`Logs`,iconURL: client.user.displayAvatarURL()})
.setDescription(`**${message.author.username}** said: **${message.content}** in channel **${message.channel.name}**`)
.setFooter({text:`Log system`, iconURL: client.user.displayAvatarURL() })
.setTimestamp()
.setColor("FF0000");
bot.channels.cache.get("1018767166218182668").send({embeds:[botMessageLog]})
//you must define bot as client
})
it always better if you use the latest Version of discord js
I recently made a discord word filter bot and I was wondering how I could make it configurable per server? I want to be able to make a command that adds words to be filtered to the code and I want people to be able to add a custom prefix per server. I also want people to be able to set their log channel per server. Can someone help me with this please? I spent a lot of time on this bot and I dont want to just keep making more of the same bot for different servers. I'm using discord.js 12.5.3. I'll attach my code below.
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', async () => {
console.log('yay, the bot is online')
client.user.setActivity('im real', {type: 'PLAYING'})
})
client.on('message', async message => {
if(message.channel.type === 'dm' || message.author.bot) return;
const logChannel = client.channels.cache.find(channel => channel.id === '921730444498726924')
let words = [“bad”, “words”, “here”]
let foundInText = false;
for (var i in words) {
if (message.content.toLowerCase().includes(words[i].toLowerCase())) foundInText = true;
}
if (foundInText) {
let logEmbed = new Discord.MessageEmbed()
.setDescription(<#${message.author.id}> Said a bad word)
.addField('The Message', message.content)
.setColor('RANDOM')
.setTimestamp()
logChannel.send(logEmbed)
let embed = new Discord.MessageEmbed()
.setDescription('That word is not allowed here')
.setColor('RANDOM')
.setTimestamp()
let msg = await message.channel.send(embed);
message.delete()
msg.delete({timeout: '3500'})
}
})
I've tried following a tutorial but it didn't help because I need one specifically for my case. If anyone could help, that would be great.
So I am trying to create a type of command that will generate 1 single use in server 1. Now if I were to input this command into a general text in server 2, !dminvite #user, it would send the mentioned user the single created invite from server 1 and send it to the mentioned user in server 2, via DMs.
Some Notes:
1. I have the 2 server id's saved in my config.json file and this invite command requires config.json. In this commands file base
2. This command is running over discord.js v12, but if anyone wants to help out in discord.js v13, It would be amazing as well.
3. I am no way in trying to advertise anything, I own both servers
I have a code written down but it's not working, just a view point:
message.delete();
message.mentions.members.first() ||
message.guild.members.cache.get(args[0]);
const Options = {
temporary: false,
maxAge: 0,
maxUses: 1,
unique: true
};
const channel = message.guild.channels.cache.get('(Channel ID)');
let invite = message.channels.createInvite(Options).then(function(Invite) {
message.mentions.members.first().send({embed: {
title: `**INVITE**`,
description: `Invite:\nhttps://discord.gg/` + Invite.code
}});
message.channel.send(new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
.setDescription(`${message.author.tag}, invite was sent to DMs`)
.setColor('BLUE')
);
});
To accomplish your goal we do this:
Get the mentioned user from the message.
Fetch the other guild we want to invite the user to.
Create an invite to the other guild's system channel (it doesn't have to be systemChannel).
Send the invite to the mentioned user's DMs.
// Id of the other guild
const otherGuildId = "guild id";
client.on("message", async (message) => {
// Don't reply to itself
if (message.author.id == client.user.id) return;
// Check if the message starts with our command
if (message.content.startsWith("!dminvite")) {
// Get the mentioned user from the message
const user = message.mentions.users.first();
if (!user) {
message.channel.send("You need to mention the user to send the invitation to.");
return;
}
// Fetch the other guild we want to invite the user to
const otherGuild = await client.guilds.fetch(otherGuildId);
// Create invite to the other guild's system channel
const invite = await otherGuild.systemChannel.createInvite({
maxAge: 0,
maxUses: 1,
unique: true
});
// Send the invite to the mentioned user's DMs
user.send({
embed: {
title: "**INVITE**",
description: `${invite}`
}
});
}
});
Note that if you don't want to use the systemChannel, you can simply get any channel you want from the otherGuild by its id for example.
// Resolve channel id into a Channel object
const channel = otherGuild.channels.resolve("channel id");
Using discord.js ^12.5.3.
client.on("guildCreate", (guild) => {
console.log(`I'm added in ${guild.name}`); //log console
const embed = new MessageEmbed() //The embed//the embed
.setAuthor("I'm in a new server!") //the author message
.addField("server name", `${guild.name}`) //the name
.setColor("GREEN") //the colour
.addField("Actual servers", `${client.guilds.cache.size}`); //the actual servers
client.channels.cache.get("779832833320681515").send(embed); //the channel of the embed
});
How I can see the members and server Icon of the added server?
You can get the Guild's icon using the iconURL() method of Guild.
client.on("guildCreate", guild => {
console.log(guild.iconURL({dynamic: true, size: 2048}));
});
Getting the members of the Guild is pretty straightforward. You can use the memberCount property of Guild.
client.on("guildCreate", guild => {
console.log(guild.memberCount);
});
Since Guild.members.cache is a Collection, you can as well map the members by any property you'd like. (Id, username, discriminator, tag etc...)
client.on("guildCreate", guild => {
console.log(guild.members.cache.map(member => member.user.tag));
// --> ["Wumpus#0000", "entrynidy#1234"]
});
Guild#iconURL()
Returns the guild's icon URL as a string.
client.on('guildCreate', guild => {
console.log(guild.iconURL())
})
GuildMemberManager#cache
Returns a collection of all of the cached guild members.
client.on('guildCreate', guild => {
console.log(guild.members.cache)
// Keep in mind: This will quite literally spam your terminal.
})
I have this on my code but when I execute the command twice (the name command, user, and role) it doesn't return this message. It keeps on saying "I added [role name] to [user]"
if (message.guild.members.cache.some(role => role.name)) {
const embed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setDescription(`${message.mentions.users.first()} has that role already!`);
return message.channel.send(embed);
}
Did you save the code? Also, instead of
if (message.guild.members.cache.some(role => role.name))
do
if (message.guild.members.cache.some(role => role.name === 'roleName')) //insert role name.
Hope this helps :)