I am trying to make my bot send the exact message that a user sends to a specific channel (contain the embeds) but the bot sends the text but not the attachments
Here is the code
bot.on('message', message => {
if (message.author.bot) return;
if(message.channel.id == "771695635598278677") {
message.delete()
const user = message.mentions.users.first() || message.author;
let messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null
const dark = new Discord.MessageEmbed()
.setColor('#000000')
.setDescription(message.content)
.setAuthor("Dark Chat", `${message.author.displayAvatarURL()}`)
.setImage((messageAttachment)) //message.attachments.first() || {}).url
.setFooter('Dark Chat', 'https://cdn.discordapp.com/attachments/564032243836780558/771614643159433226/Anonymous_emblem.svg.png')
if(isNaN(message.content)) {
message.channel.send(dark)
}
}
});
https://discord.js.org/#/docs/main/stable/class/MessageEmbed?scrollTo=setImage
.setImage(message.attachments.array().length == 0 ? null:message.attachments.first().url)
Related
So Im coding in javascript a discord bot and then what happens is i get an Declaration Or Statment expected error even if its not needed
const client = new Discord.Client({ intents: 32767 });
client.on("ready", () => {
console.log("✔✔ online")
})
client.on("messageCreate", message => {
const prefix = ">"
if(!message.content.startsWith(prefix)) return;
const messageArray = message.content.split(" ");
const cmd = messageArray[0];
const args = messageArray.slice(1);
if (message.content === `${prefix}test`) {
message.reply("Working")
}
if (cmd == `${prefix}kick`) {
if(!args[0]) return message.reply("Please Mention Someone or Put That Persons ID or Put That Persons Username to Kick The User Out Of The Server!");
const member = (message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(x => x.user.username.toLowerCase() === args.slice.join(" ") || x.user.username === args[0]));
if(!message.member.permissions.has("KICK_MEMBERS")) return message.reply("Since When You Have a kick members perm" || "You need to have the kick permission perm to use this command");
});
client.login("not showing token ");```
You have a wrong synatax. Your code should be
const client = new Discord.Client({ intents: 32767 });
client.on("ready", () => {
console.log("✔✔ online")
})
client.on("messageCreate", message => {
const prefix = ">"
if(!message.content.startsWith(prefix)) return;
const messageArray = message.content.split(" ");
const cmd = messageArray[0];
const args = messageArray.slice(1);
if (message.content === `${prefix}test`) {
message.reply("Working")
}
if (cmd == `${prefix}kick`) {
if(!args[0]) return message.reply("Please Mention Someone or Put That Persons ID or Put That Persons Username to Kick The User Out Of The Server!");
const member = (message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(x => x.user.username.toLowerCase() === args.slice.join(" ") || x.user.username === args[0]));
if(!message.member.permissions.has("KICK_MEMBERS")) return message.reply("Since When You Have a kick members perm" || "You need to have the kick permission perm to use this command");
};
});
client.login("not showing token ");
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "test",
description: "A test command",
run: async(client, message, args)=>{
let count = 0;
let channelId = message.channel.id;
client.on("messageCreate", async(newMessage)=>{ // dont use "message" here
if (newMessage.channel.id === channelId) {
count = count + 1;
if (count === 10) {
let embed = new MessageEmbed()
.setDescription("Hello")
.setColor("RANDOM")
return message.channel.send({ embeds: [embed] })
}
} else if (newMessage.channel.id !== channelId) {
return;
}
})
}
}
I'm trying to make a command that sends an embed to a specific channel every 10 messages are sent but the above code isn't working and I'm not getting any errors either could someone help?
New to coding and recently started making a discord bot using JS. It's a bot where a certain mp4 plays with a specific snippet.
I'm having trouble with the fact that the mp4 doesn't send when I input the command, just the embed message. Basically if I do -snip kratos the bot sends the embed message but not the mp4.
Here's what I have so far:
const fs = require('fs');
const { Client, MessageAttachment } = require('discord.js');
const config = require('./config.json');
const { prefix, token } = require('./config.json');
const client = new Client();
client.commands = new Discord.Collection();```
And here are the command events:
``` client.on('message', message => {
if (message.content === `${prefix}snip kratos`) {
if (message.author.bot) return
const kratos = new Discord.MessageEmbed()
.setColor('#ffb638')
.setTitle('Kratos')
.setDescription('Sending 1 snippet(s)...')
.setTimestamp()
.setFooter('SkiBot');
message.channel.send(kratos);
client.on('message', message => {
if (message.content === `${prefix}snip kratos`) {
if (message.author.bot) return
const attachment = new MessageAttachment('./snippets/kratos/Kratos.mp4');
message.channel.send(attachment);
}
});
}
});
client.on('message', message => {
if (message.content === `${prefix}snip johnny bravo`) {
if (message.author.bot) return
const kratos = new Discord.MessageEmbed()
.setColor('#ffb638')
.setTitle('Johnny Bravo')
.setDescription('Sending 1 snippet(s)...')
.setTimestamp()
.setFooter('SkiBot');
message.channel.send(kratos);
client.on('message', message => {
if (message.content === `${prefix}snip johnny bravo`) {
if (message.author.bot) return
const attachment = new MessageAttachment('./snippets/Johnny_Bravo/Johnny_Bravo.mp4');
message.channel.send(attachment);
}
});
}
});```
The issue is that you are nesting event listeners. Remove the nested client.on('message', ...) part and send the message attachment after sending the embed.
client.on('message', (message) => {
if (message.author.bot) {
return
}
if (message.content === `${prefix}snip kratos`) {
const kratos = new MessageEmbed()
.setColor('#ffb638')
.setTitle('Kratos')
.setDescription('Sending 1 snippet...')
.setTimestamp()
.setFooter('SkiBot')
message.channel.send(kratos)
const attachment = new MessageAttachment('./snippets/kratos/Kratos.mp4')
message.channel.send(attachment)
}
})
And you don't need multiple message event listeners. You can simplify the code by creating an object with the valid commands.
client.on('message', (message) => {
const { content, author, channel } = message
if (author.bot) {
return
}
const embeds = {
[`${prefix}snip kratos`]: {
title: 'Kratos',
attachmentPath: './snippets/kratos/Kratos.mp4',
},
[`${prefix}snip johnny bravo`]: {
title: 'Johnny Bravo',
attachmentPath: './snippets/Johnny_Bravo/Johnny_Bravo.mp4',
},
}
const embed = embeds[content]
if (embed) {
const { title, attachmentPath } = embed
channel.send(
new MessageEmbed()
.setColor('#ffb638')
.setTitle(title)
.setDescription('Sending 1 snippet...')
.setTimestamp()
.setFooter('SkiBot')
)
channel.send(new MessageAttachment(attachmentPath))
}
})
The above solution should be enough if you don't have a lot of commands. Check this guide to learn how to create individual command files to keep your code organized.
You should be able to do
message.channel.send({
files: [
"./snippets/kratos/Kratos.mp4"
]})
As referred to here https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=send
Also right here client.commands = new Discord.Collection(); right here you are calling Discord but Discord was not defined
I have this code which chooses a random user and displays their name and what they won, the problem is it can also make the user who sent the giveaway message the winner and the same with bots. How can I make an exception for the msg.author?
if(msg.content.startsWith('-giveaway'))
{
const suggestion = msg.content.slice(10);
const user = msg.guild.members.random();
let embed = new Discord.RichEmbed()
.setTitle('Prize : ' + suggestion)
.addField('Host : ', msg.author)
.addField('Nyertes : ', user)
.setFooter('Verzió 1.1')
.setTimestamp()
msg.channel.send(embed);
console.log(`${user.user}`).catch(console.error);
}
You could filter out the message author from the user list before selecting a random user:
const userList = msg.guild.members.filter(user => user.id !== msg.author.id || !msg.author.bot || msg.member.roles.some(role => role.name === 'RoleName') || msg.member.roles.some(role => role.id === 'ROLE_ID'));
const user = userList.random();
const Discord = require("discord.js");
const bot = new Discord.Client();
let prefix="!"
bot.on("ready", () => {
bot.user.setStatus("idle");
console.log(`Demon Is Online`)
const update = () => {
bot.user.setActivity("!help | residing on " + bot.guilds.size + " Servers", { type: 'WATCHING' });
};
bot.on('ready', update);
bot.on('guildCreate', update);
bot.on('guildRemove', update);
bot.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(!command.startsWith(prefix)) return;
});
if(command === `${prefix}userinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#3498db")
.setThumbnail( `${message.author.avatarURL}`)
.addField("Name", `${message.author.username}#${message.author.discriminator}`)
.addField("ID", message.author.id)
message.reply("I've Sent Your User Info Through DM!");
message.channel.send({embed});
}});
if("command" === `${prefix}help`) {
let embed = new Discord.RichEmbed()
.addField("!help", "gives you this current information")
.setTitle("Help")
.setColor("#3498db")
.addField("!userinfo", "gives you info about a user(currently being worked on)")
.addField("!serverinfo","gives you info about a server(currently working on it)")
.addField("link to support server","https://discord.gg/NZ2Zvjm")
.addField("invite link for bot","https://discordapp.com/api/oauth2/authorize?client_id=449983742224760853&permissions=84993&scope=bot")
message.reply("here's a list of commands that i'm able to do")
message.channel.send({embed});
messageArray = message.content.split("");
let command = messageArray[0];
if(command === `${prefix}serverinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#3498db")
.addField("Name", `${message.guild.name}`)
.addField("Owner", `${message.guild.owner.user}`)
.addField("Server ID" , message.guild.id)
.addField("User Count", `${message.guild.members.filter(m => m.presence.status !== 'offline').size} / ${message.guild.memberCount}`)
.addField("Roles", `${message.guild.roles.size}`);
message.channel.send({embed});
}
};
bot.login("token goes here")
I have this code that I need help with its stopping my discord bot from coming online. I use javascript and discord.js and node.js and could use help I tried searching youtube and also servers on discord and asking some friends but they all tell me to learn the language which I been trying. but anyways here's my error
output
command doesnt exist in the context that its being referenced.
you have
bot.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(!command.startsWith(prefix)) return;
});
and then youre referencing command below here. which means it is out of scope, since it is defined int eh scope of bot.on('message' ...
paste the command snippet into the bot.on('message' code and, reset ur bot and try sending it a message, i think you'll see desired results.
full example will look like this:
bot.on('ready', update)
bot.on('guildCreate', update)
bot.on('guildRemove', update)
bot.on('message', message => {
if (message.author.bot) return
if (message.channel.type === 'dm') return
let messageArray = message.content.split(' ')
let command = messageArray[0]
let args = messageArray.slice(1)
if (!command.startsWith(prefix)) return
if (command === `${prefix}userinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor('#3498db')
.setThumbnail(`${message.author.avatarURL}`)
.addField(
'Name',
`${message.author.username}#${message.author.discriminator}`
)
.addField('ID', message.author.id)
message.reply("I've Sent Your User Info Through DM!")
message.channel.send({ embed })
}
if (`${prefix}help` === 'command') {
let embed = new Discord.RichEmbed()
.addField('!help', 'gives you this current information')
.setTitle('Help')
.setColor('#3498db')
.addField(
'!userinfo',
'gives you info about a user(currently being worked on)'
)
.addField(
'!serverinfo',
'gives you info about a server(currently working on it)'
)
.addField('link to support server', 'https://discord.gg/NZ2Zvjm')
.addField(
'invite link for bot',
'https://discordapp.com/api/oauth2/authorize?client_id=449983742224760853&permissions=84993&scope=bot'
)
message.reply("here's a list of commands that i'm able to do")
message.channel.send({ embed })
messageArray = message.content.split('')
let command = messageArray[0]
if (command === `${prefix}serverinfo`) {
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor('#3498db')
.addField('Name', `${message.guild.name}`)
.addField('Owner', `${message.guild.owner.user}`)
.addField('Server ID', message.guild.id)
.addField(
'User Count',
`${
message.guild.members.filter(m => m.presence.status !== 'offline')
.size
} / ${message.guild.memberCount}`
)
.addField('Roles', `${message.guild.roles.size}`)
message.channel.send({ embed })
}
}
})
bot.login('token goes here')
Note i didnt fix ALL your issues, there seems to be a few more stragglers left, i just tried to answer your specific question :)