I have Discord Bot which have to mention user and send data before message.
My code:
const discord = require('discord.js');
const client = new discord.Client;
const prefix = "!";
client.on('message', message => {
if (message.content == (prefix + "logs")) {
message.channel.send(message.author + " " + message.createdAt, {
files: [
"path to file",
]
});
}
});
client.login('token');
I get output:
480059129793347586 Sat Jul 25 2020 20:12:15 GMT+0000 (Coordinated Universal Time)
Why bot send id but dont mention user?
Next question...
How can I change GMT?
To mention someone you actually need their user ID. and send the following string:
<#userID>
So, to mention the user, just do:
"<#" + message.author.id + ">"
Related
Im trying to make my bot send a embed in the server that it joins (E.G a thank you for inviting and what the bot can do).This is the error that I am getting.
const channel = message.guild.channels.cache.find(channel => channel.type === 'text' && client.user.me.permissions.has("send_messages"))
^
TypeError: Cannot read properties of undefined (reading 'channels')
this is the code that i am using in the index.js file
client.on('guildCreate', message => {
const channel = message.guild.channels.cache.find(channel => channel.type === 'text' && client.user.me.permissions.has("send_messages"))
const embed = new MessageEmbed()
.setColor('GREEN')
.setTitle('Thank You')
.setDescription("Thank you for inviting Rynwin to your server. To get the command list please do /help.\n \n The default Bot prefix is ```<```")
.addFields([
{
name: "Our Support Server",
value: "If you require assistance or would like to get daily bot updates please join our support discord [here] ()"
},
{
name: "Commands",
value: "Try some of these commands; \n\n**/prefix <prefix>** - Sets the server prefix\n\n**/help** - Get the command list or information on a command \n\n**/botinfo** - Shows info on the bot"
},
channel.send({ embeds: [embed] })
])
There are a few things wrong on the first 2 lines. Firstly, guildCreate gives a Guild instance, not a message. Secondly, you aren't checking the correct channel type or permission. Here is the fixed code:
client.on('guildCreate', guild => {
const channel = guild.channels.cache.find(channel => channel.type === 'GUILD_TEXT' && guild.me.permissions.has("SEND_MESSAGES"))
// GUILD_TEXT channel type, guild.me for client guild member, and uppercase permission
// rest of code...
}
I need some code that would allow me to send a webhook across the servers my bot is in.
Like how the bot "Dank Memer" has webhooks sent to servers it's in about the status of the bot.
For example:
"Dank Memer Official #bot-status SERVER 01/18/2021
We are waiting to hear back from our host on this issue. Most of the bot is online, but performing badly.
I would suggest not using it until this is fixed, there will likely be weird bugs with this type of latency."
When I check out the discord server all the person does is send a message in the channel, and it sends that message throughout the servers in a specific channel.
All I have is:
module.exports = {
name: 'announce',
description: 'Sends a webhook annoucment to the channel that I specify',
execute(client, message, args, Discord) {
const webhookClient = new Discord.WebhookClient('ID', 'Token');
var announcement = "";
for (const word in args) {
announcement = announcement + args[word] + " ";
}
webhookClient.send(announcement)
}
}
I think your error is either from a wrong webhook or a wrong argument selection.
This code should work, let me know if not!
module.exports = {
name: 'announce',
description: 'Sends a webhook annoucment to the channel that I specify',
execute(client, message, args, Discord) {
const webhookChannelID = "Put your channel ID here";
const channel = message.guild.channels.cache.get(webhookChannelID)
const webhooks = await channel.fetchWebhooks();
const webhook = null;
if(webhooks.array().length == 0){
webhook = await channel.createWebhook('Example Webhook');
}
else{
webhook = webhooks.first();
}
webhook.send(args.join(" "))
}
}
I have tried many different ways of formatting the code, however, whenever I add code so that I must provide a reasoning to ban someone, I am always given an Internal Server Error. Here is my code.
module.exports.run = async (client, message, args) => {
const member = message.mentions.members.first();
const reason = args.slice(1).join(" ")
if (!message.member.hasPermission("BAN_MEMBERS")) {
return message.reply("you lack sufficiant permissions to execute this command.");
} else if (member.hasPermission("ADMINISTRATOR")) {
message.reply("you cannot ban this member.")
}
member.ban(reason).then((member) => {
message.channel.send(`${member} has been banned.`);
});
I use a command handler, and all my other commands work fine.
first step: Define the user
let user = message.mentions.members.first() || message.guild.members.cache.get(args.join(' '));
Second step: Create embed message or normal message
const userbanned = new Discord.MessageEmbed()
.setColor('#FF0000')
.setAuthor('User Banned')
.setDescription(`**${user.user.username}#${user.user.discriminator}** is now banned from this server`)
.setFooter(`bot_name`);
Third step: Send message
user.send(`You were banned from **${message.guild.name}** by ${message.author.username}#${message.author.discriminator}`)
return user
.ban()
.then(() => message.channel.send(userbanned))
.catch(error => message.reply("ERROR"));
Try changing
member.ban().then((member) =>//
to
member.ban({reason : args.slice(1).join(' ')}).then((member) =>//
const prefixtest = ">>"
msg = message.content.toLowerCase();
if(msg.startsWith(prefixtest + "test")) {
message.delete();
setTimeout(function() {
if{message.channel.author.id == "240254129333731328"}{ //this is the bots id
message.delete();
}, 3000);
}
Sorry if my english is bad. But I can't solve this problem, i'm trying to find it on internet but nothing. How can I delete other bot messages by using their Bot ID?
There is an issue in your code, you are trying to get .author property on a Channel instead of a Message.
So you have to modify your if statement as following :
if (message.author.id === "240254129333731328")
Also, you can delay the deletion of a Message by adding .delete() method first parameter an object with a .timeout property in it that will represent the delay in milliseconds before the message being deleted. (See docs)
message.delete({ timeout: 3000 });
So you can modify your code this way :
if(msg.startsWith(prefixtest + "test")) {
if (message.author.id === "240254129333731328") {
message.delete({ timeout: 3000 });
}
}
I searched it up and got a code with readline where it looks like this:
const Disc = require('discord.js');
const client = new Disc.Client();
const token = 'token'
const readline = require('readline');
client.login(token);
client.on('message', function(message){
if(message.channel.type === 'dm'){
console.log("[" + message.author.username + "]: " + message.content)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('REPLY TO ' + message.author.username + ': ', (answer) => {
message.author.send(`${answer}`);
rl.close();
});
}
});
But it doesn't work helpp
This is a topic I just did recently actually, so I'll walk you through it and give you some code to go along with it.
First, I would like to say when your making a post, include a clear question. From what it sounds like, your asking for a bot that logs dms to the console, or responds to them. I will just answer both questions.
The easiest way to check for a DM is to see if the message channel type is DM. Check here for more info on the channel class. You can check if a channel is a certain type by doing this:
if (message.channel.type === 'dm'){ } // change dm to the type you want
This will have to go in your on message function, so right now, if you're following along, the code would look like this:
bot.on('message', async message => {
if (message.channel.type === 'dm'){ }
});
From there it's simply adding code to the inside of the if statement. You will always want a return statement inside of it just incase nothing happens, so it doesn't try to do anything in the channels.
For what you want, this will log the DM to the console and reply to it, if it is equal to a certain message.
bot.on('message', async message => {
if (message.channel.type === 'dm'){
console.log(message.content);
if(message.content === "something"){
return await message.channel.send("Hi!");
}
return;
}
});
This should do what you want, if you have any questions, comment it on here and I'll respond as soon as possible :)
edit:
bot.on('message', async message => {
if (message.channel.type === 'dm'){
console.log(`${message.author.username} says: ${message.content}`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`REPLY TO ${message.author.username}: `, (answer) => {
message.author.send(`${answer}`);
rl.close();
});
}
});