Discord won't turn on - node.js

I need to create bridge from telegram to my discorcd channel, however i will receive error when i will run the .js script.
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (\dev\tele-discord-bot\node_modules\discord.js\src\client\Client.js:544:13)
at new Client (\dev\tele-discord-bot\node_modules\discord.js\src\client\Client.js:73:10)
at Object.<anonymous> (\dev\tele-discord-bot\bridge.js:19: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:79:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
This is the error and here is the code i used:
const TelegramBot = require('node-telegram-bot-api'); // https://github.com/yagop/node-telegram-bot-api
const Discord = require('discord.js'); // https://github.com/discordjs/discord.js
/* Values:
token: Telegram bot token (logging into Telegram's API, you can get this from #BotFather on Telegram)
token2: Discord bot token (logging into Discord's API, you can get this from Discord's developer docs > my apps > app)
channelid: Discord channel ID (channel the Discord bot can access, right clicking a channel and clicking copy ID with developer mode enabled)
*/
const token = 'TELEGRAMTOKEN';
const token2 = 'DISCORDTOKEN';
const channelid = 'DISCRODCHANNELID';
/* Bots:
bot: Telegram bot
bot2: Discord bot
*/
const bot = new TelegramBot(token, {polling: true});
const bot2 = new Discord.Client();
// Matches "/echo [whatever]" in Telegram chat
bot.onText(/\/echo (.+)/, (msg, match) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
const chatId = msg.chat.id;
const resp = match[1]; // the captured "whatever"
// send back the matched "whatever" to the Telegram chat
bot.sendMessage(chatId, resp);
});
// Listen for any kind of message in Telegram. There are different kinds of messages.
// Check out node-telegram-bot-api's GitHub & the Telegram API docs for more info
// https://github.com/yagop/node-telegram-bot-api & https://core.telegram.org/api
bot.on('message', (msg) => {
const chatId = msg.chat.id;
console.log("we got a message from telegram");
bot2.channels.get(channelid).send("[Telegram] **" + msg.from.first_name + " (#" + msg.from.username + "):** " + msg.text);
console.log("sent that message to discord");
});
bot2.login(token2);
Source: https://gist.github.com/Ahe4d/866ef3b42cb5ca6ca7c84ff7da70828c
Any idea how to fix this or what is wrong? I never worked with discord.js before
Regards..

I'm guessing that you have a v8/9 gateway based discord bot....
https://discord.com/developers/docs/topics/gateway#privileged-intents
Gateway Intents
Intents are optionally supported on the v6 gateway but required as of v8
This is another great place to read:
https://discordjs.guide/additional-info/changes-in-v13.html#before-you-start
So you need to declare them as part of the client call, for example:
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);

Related

DiscordJS - Send Message

Im Trying To Send A Message Through DiscordJS
Can Someone Help Me
I Tried This Code
const channel = client.channels.cache.get('example discord guild');
channel.send('content');
But It Doesent Work
index.js
// Credetials
const { token } = require('./json/token.json');
const { guild } = require('./json/guild.json');
const { client } = require('./json/client.json')
// Init
const { Client, Intents } = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_>
bot.login(guild);
console.log("Logged In As DeltaBOT");
const channel = client.channels.cache.get(guild);
channel.send('content');
Error
const channel = client.channels.cache.get(guild);
^
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.<anonymous> (/storage/emulated/0/Download/node/index.js:15:33)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975: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
You should wait for your client to login before attempting to send a message / fetching a channel. You can do this by using the ready event. For example:
bot.on('ready', async () => {
console.log('Logged in as: ' + bot.user.tag);
const channel = await bot.channels.fetch(guild);
await channel.send('content');
})
Another thing I noticed is that you use client instead of bot. client is a json object but you defined your discord bot object as the bot variable. So use the bot variable and not the client one.
Make sure that the guild is a valid guild ID. I don't know what is in your client json file but you don't appear to be using it.

How do I fix my Discord bot being offline?

I got recently interested in discord bots coding and I don't know what the problem is with this error, I tried different solutions I can find but all of them are not working and my bot is still offline on the server.
Error: Invalid transport, must be an object with a log method.
at new LegacyTransportStream (C:\Users\redacted\Desktop\NinyaBot\node_modules\winston-transport\legacy.js:18:11)
at DerivedLogger.add (C:\Users\redacted\Desktop\NinyaBot\node_modules\winston\lib\winston\logger.js:345:11)
at Object.winston.<computed> [as add] (C:\Users\redacted\Desktop\NinyaBot\node_modules\winston\lib\winston.js:110:68)
at Object.<anonymous> (C:\Users\redacted\Desktop\NinyaBot\bot.js:6:8)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
this is the code i used that i found online.
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0];
args = args.splice(1);
switch(cmd) {
// !ping
case 'ping':
bot.sendMessage({
to: channelID,
message: 'Pong!'
});
break;
// Just add any case commands if you want to..
}
}
});
The error showed in console is about line 6, however maybe is not what makes the
bot appears offline.
logger.add(logger.transports.Console, {
colorize: true
});
And should be fixed with something like
logger.add(new logger.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
}));
Watch to new word. Now transports require a new object to work and for colorize you can set it combining the formats.
Here you can find more info about it https://www.npmjs.com/package/winston#common-transport-options
I recommend using discord.js https://discordjs.guide/creating-your-bot/#creating-the-bot-file
Use this code and it work just fine:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.login('your-token-goes-here');

Discord Webhooks Channel returns undefined

I'm new to using webhooks and I've pored over the discord documentation to help set me up with a very basic bot. At this point all I want to do is for the bot to come online and use a webhook to make a post. I've even directly copied the basic code from The Discord Webhooks documentation and I cant get that to work.
Using some console logs I've determined that client.channels.cache.get(config.webhookID); returns undefined and therefore channel.fetchWebhooks() is not a function. Is this still the way to get webhooks that are in the channel or is the documentation behind?
I'm turning to any experienced user that could potentially help me with this problem. I would really appreciate any help you can provide.
const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();
const embed = new Discord.MessageEmbed()
.setTitle('Some Title')
.setColor('#0099ff');
client.once('ready', async () => {
const channel = client.channels.cache.get(config.webhookID); //This is the problem.
try {
const webhooks = await channel.fetchWebhooks(); //This will not execute because channel is undefined.
const webhook = webhooks.first();
await webhook.send('Webhook test', {
username: 'some-username',
avatarURL: 'https://i.imgur.com/wSTFkRM.png',
embeds: [embed],
});
} catch (error) {
console.error('Error trying to send: ', error);
}
});
client.login(config.token);
The error in the console is this:
Error trying to send: TypeError: Cannot read property 'fetchWebhooks' of undefined
at Client.<anonymous> (C:\Users\me\OneDrive\Desktop\Discord Bot\index.js:13:34)
at Object.onceWrapper (events.js:421:28)
at Client.emit (events.js:315:20)
at WebSocketManager.triggerClientReady (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:433:17)
at WebSocketManager.checkShardsReady (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:417:10) at WebSocketShard.<anonymous> (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:199:14)
at WebSocketShard.emit (events.js:315:20)
at WebSocketShard.checkReady (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:467:12)
at WebSocketShard.onPacket (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:439:16)
at WebSocketShard.onMessage (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
I figured out the answer. I was using the field "id" when I should have been using the field "channel_id".

I'm getting "SyntaxError: Missing initializer in const declaration" while writing a discord bot

I'm trying to write m first discord bot and while coding an embed I get this:
SyntaxError: Missing initializer in const declaration
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
Here is my code:
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = "I've hidden it";
bot.on('ready', () =>{
console.log('This bot is online');
})
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Map')
.setImage('Hidden')
.setTimestamp()
.setFooter('Bot by MuffinLover42'),
channel.send(exampleEmbed);
bot.login(token);
I'm using node.js and discord.js
What's wrong with it?
I'm using the code provided by discord with a slight modification.
const token = 'I've hidden it';
Try escaping the second ` in your declaration. Right now the compiler is seeing
const token = 'I'
with a bunch of garbage after it.
Additionally,
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Map')
.setImage('Hidden')
.setTimestamp()
.setFooter('Bot by MuffinLover42'),
should likely be terminated by a semicolon rather than a comma.

I was writing Discord bot with index.js and .env and I messed with this error while running this in terminal "node index.js"

I want develop my own discord bot here's the contents:
Index code
const { Client } = require("discord.js");
const { config } = require("dotenv");
// Declares our bot,
// the disableEveryone prevents the client to ping #everyone
const client = new Client({
disableEveryone: true
});
config({
path: __dirname + "/.env"
})
// When the bot's online, what's in these brackets will be executed
client.on("ready", () => {
console.log(`Hi, ${client.user.username} is now online!`);
// Set the user presence
client.user.setPresence({
status: "online",
game: {
name: "me getting developed",
type: "WATCHING"
}
});
})
// When a message comes in, what's in these brackets will be executed
client.on("message", async message => {
console.log(`${message.author.username} said: ${message.content}`);
});
// Login the bot
client.login(process.env.TOKEN);
Output
disableEveryone = true
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
Invalid shorthand property initializer indicates that you used an invalid property initializer. In this case, it looks like you used "=" instead of ":" to declare the property value "true" on property "disableEveryone".
Saving the code as shared and running it again should solve your problem.

Resources