Send a IMAGE on Discord was Bot - discord

whatsup?!
I'm starting a new research on java and i'm cracking my head about how to send image when i set the command in client side.
if(result == 1 ) {
message.channel.send('4 ā˜… Great Staff', {
files: [
"./test.jpeg"
]
}); }
What give me on server side
ReferenceError: message is not defined
at Object.pull (C:\Bot\neobot\gacha.js:12:15)
at Client. (C:\Bot\neobot\index.js:17:39)
at Client.emit (events.js:315:20)
at MessageCreateHandler.handle (C:\Bot\neobot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Bot\neobot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:100:65)
at WebSocketManager.eventMessage (C:\Bot\neobot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:225:31)
at WebSocket.ws.onmessage (C:\Bot\neobot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:83:37)
at WebSocket.onMessage (C:\Bot\neobot\node_modules\ws\lib\WebSocket.js:442:14)
at WebSocket.emit (events.js:315:20)
at Receiver.ontext (C:\Bot\neobot\node_modules\ws\lib\WebSocket.js:841:10)

You can use the MessageAttachment which comes with discord.js
const { MessageAttachment } = require("discord.js")
const image = new MessageAttachment(LINK HERE)
And you can do more customizations
For more info:
https://github.com/discordjs/discord.js/blob/master/docs/examples/attachments.md

Related

Im getting Type error code valid intents must be provided for the Client when coding a crypto bot from a tutorial

This is my bot.js file
const { Client } = require('discord.js');
const dotenv = require('dotenv');
// Load environment variables
dotenv.config();
// Create a bot instance
const bot = new Client();
// Log our bot in
bot.login(process.env.DISCORD_BOT_TOKEN);
This is my .env file
DISCORD_BOT_TOKEN = my discord bot token
This is my .gitignore file
node_modules/
.env
This is the error code
C:\Users\aslat\OneDrive\Desktop\ScrtBot\crypto-discord-bot\node_modules\discord.js\src\client\Client.js:548
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Users\aslat\OneDrive\Desktop\ScrtBot\crypto-discord-bot\node_modules\discord.js\src\client\Client.js:548:13)
at Object.<anonymous> (C:\Users\aslat\OneDrive\Desktop\ScrtBot\crypto-discord-bot\bot.js:9:13)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
Try this for your bot.js file
const { Client } = require('discord.js');
const dotenv = require('dotenv');
// Load environment variables
dotenv.config();
// Create a bot instance
const bot = new Client({
// the below line is what you were missing.
intents: 32767 // you can change this if you want but that number is the code for all intents
});
// view intents here https://discord.js.org/#/docs/main/stable/class/Intents
// Log our bot in
bot.login(process.env.DISCORD_BOT_TOKEN);
Also as mentioned before, get a new discord token as posting it here has compromised the security of your bot.
Please try again using the structured file in the current working directory. e.g.:
// Importing a local module with a path relative to the `__dirname` or current
// working directory. (On Windows, this would resolve to .\path\myLocalModule.)
const myLocalModule = require('./path/myLocalModule');

Why do I keep getting this error when I try to get my discord bot online

I tried making a discord bot and I did the first steps and the command prompt when I type node . shows this:
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Users\giann\Desktop\BWKS\node_modules\discord.js\src\client\Client.js:548:13)
at new Client (C:\Users\giann\Desktop\BWKS\node_modules\discord.js\src\client\Client.js:76:10)
at Object. (C:\Users\giann\Desktop\BWKS\main.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
I tried almomst everything but this error is coming up all the time.
the code:
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready')
});
client.login('the token');
Discord needs to know what intents you want to use. So to fix your code, use:
const client = new Discord.Client({ intents: [<ARRAY WITH ALL INTENTS>] });
client.once('ready', () => {
console.log('Ready')
});
client.login('the token');
You can add intents in the array by using a string "GUILDS" or use the property Intents.FLAGS.GUILDS
A list of all intents can be found here: https://discord.js.org/#/docs/discord.js/stable/class/Intents

When I goto start up my discord bot, I get the error token is not defined

I've tried multiple fixes and I am unable to figure out what is wrong send help...
The bot at the moment is supposed to run simple commands and hopefully sometime in the near future be an application bot
Index.js
const { Client, Collection } = require("discord.js");
const client = new Client({
intents: 32767,
});
module.exports = client;
// Global Variables
client.commands = new Collection();
client.slashCommands = new Collection();
client.config = require("./config.json");
// Initializing the project
require("./handler")(client);
client.login(client.config.token);
config.json
{
"token": "My discord bot token",
"prefix": "!",
}
error
C:\Users\Hello\djs-base-handler\index.js:16
client.login(token);
^
ReferenceError: token is not defined
at Object.<anonymous> (C:\Users\Hello\djs-base-handler\index.js:16:14)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
This is not how you get your token from your json, try this instead :
//assuming the file is on the root of your project, else put the full path
const { token } = require('./config.json')
//...
client.login(token)

"ReferenceError: message is not defined" when trying to create a Discord.js bot

Just a heads up, I'm not good with code and I'm new to the coding scene. I'm trying to create a Discord bot for our server.
Right now the command is ping and I'm having some issues.
client.on('message', msg => {
if (msg.content === 'o!ping') {
ping = msg.createdTimestamp - message.createdTimestamp
msg.reply("Pong!\n\nšŸ“ The latency is" + ping + "ms.");
}
});
The error occurs at the "ping = msg.." line here's the error:
ReferenceError: message is not defined
at Client.<anonymous> (D:\Bot\bot.js:55:35)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (D:\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (D:\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (D:\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (D:\Bot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (D:\Bot\node_modules\ws\lib\websocket.js:825:20)
Line 55 is "ping = msg.createdTimestamp - message.createdTimestamp". Please help!
You passed msg in the callback function, so replace message with msg.
client.on('message', msg => {
if (msg.content === 'o!ping') {
ping = msg.createdTimestamp - msg.createdTimestamp
msg.reply("Pong!\n\nšŸ“ The latency is" + ping + "ms.");
}
});

FS Events Message.js not working in Discord.js v12

I'm trying to redo my events into an fs event system to have separate files for each event, and I'm having some issues with converting the code to Discord.JS v12.
Can someone please help me figure out what's going wrong with my message.js file?
Here is my code:
index.js
const { Client } = require('discord.js-commando');
const path = require('path');
const fs = require('fs');
const server_invite = (process.env.INVITE_URL);
const owner_id = (process.env.BOT_OWNER);
const prefix = (process.env.BOT_PREFIX);
const stripIndents = require('common-tags').stripIndents;
require('dotenv').config();
const client = new Client({
commandPrefix: prefix,
unknownCommandResponse: false,
disableMentions: 'everyone',
owner: owner_id,
invite: server_invite
})
client.registry
.registerDefaultTypes()
.registerGroups([
['admin', 'Administration'],
['mod', 'Moderation'],
['fun', 'Fun'],
['misc', 'Miscellanious'],
['util', 'Utility']
])
.registerDefaultGroups()
.registerDefaultCommands()
.registerCommandsIn(path.join(__dirname, 'commands'))
fs.readdir('./events/', (err, files) => {
if (err) return console.error;
files.forEach(file => {
if (!file.endsWith('.js')) return;
const evt = require(`./events/${file}`);
let evtName = file.split('.')[0];
console.log(`Loaded event '${evtName}'`);
client.on(evtName, evt.bind(null, client));
});
});
client.on('error', console.error)
client.login(process.env.BOT_TOKEN);
message.js
const discord = require("discord.js");
const dotenv = require('dotenv').config;
const prefix = (process.env.BOT_PREFIX);
const fs = require('fs');
module.exports = (client, message) => {
if (message.author.bot) return;
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.cache.get(command);
if (!cmd) return;
cmd.run(client, message, args);
};
Basically every time I run a command it crashes the bot. Other than that my ready.js event seems to work flawlessly.
Here is the error my message.js file throws:
/app/events/message.js:13
const cmd = client.commands.cache.get(command);
^
TypeError: Cannot read property 'cache' of undefined
at module.exports (/app/events/message.js:13:33)
at CommandoClient.emit (events.js:326:22)
at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:314:20)
at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:797:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! yuzuki#1.0.0 start: `node --harmony index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the yuzuki#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /app/.npm/_logs/2020-10-07T08_08_09_863Z-debug.log
Process exited with status 1
State changed from up to crashed
I'm running Discord.JS ^12.0.0 with discord.js-commando discord.js/Commando and node ^12.16.4 if it helps.
Thanks for the help from all who responded. Was able to fix my message.js file with the response provided.
Here is my working message.js file if anyone wants to use it:
const client = require('discord.js-commando');
const prefix = (process.env.BOT_PREFIX);
require('dotenv').config;
module.exports = (message) => {
if (message.author.client) return;
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.cache.get(command);
if (!cmd) return;
cmd.run(client, message, args);
};
Because you're using Commando, you don't need another listener for the message event. The library already does that for you. Deleting message.js should remove the error.
The error is occurring because client.commands is undefined. You seem to be using code from the main guide, which is intended for bots that are not using Commando and assumes you have already set client.commands to a Collection of your commands. You may want to take a look at the Commando guide.
Please don't ask multiple questions in one post. If you have a different issue with your other event handlers, please ask a new question.

Resources