node.jsdiscordbotstelegrambridge

Discord won't turn on


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..


Solution

  • 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);