Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS discordbot.js (bridge and sockets)

hdscripts

New Coder
Hi guys.
My code consists of: send a discord message to the game, and vice versa...
But I'm having some problems with "encoding" from UTF-8 to Unicode.
Here's a video that explains more of what I want to do:


Message received from game -> discord OK
Message received from discord -> game FAIL (with encoding)

My code:

JavaScript:
/*
     Discord bot v1.0 using node.js to create a "bridge" between C++ and Discord.
     Created by @Normynator, and upgraded by HD Scripts 2023
*/

const net = require('net');
const { Client, GatewayIntentBits, Collection, InteractionType } = require('discord.js');
const { token } = require('./config.json');
var Iconv = require('iconv').Iconv;

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildIntegrations
  ]
});

module.exports = client
let sockets = [];

/* Status when bot starts */
const status = [
  '🥇 !help to help',
];

/* Set status and message to bot when online */
client.on('ready', () => {
  client.user.setActivity(status[0]);
    client.user.setStatus('online');
  console.log(`Ready! Logged in as ${client.user.username}`);
});

/* receive from game to discord */
var server = net.createServer(socket => {
  socket.setEncoding("latin1");
  socket.on("data", (msg) => {
    client.channels.cache.get('1049483595061932102').send(msg).then(() => {
      console.log('Message online sent');
      console.log(msg);
    }).catch(err => {
      console.log(err);
    });
  })
  sockets.push(socket);
})
server.listen(1337, "127.0.0.1");

/* Send from discord to game */
client.on("messageCreate", msg => {
  //var iconv = new Iconv('utf-8', 'ISO-8859-1');
  //var buffer = iconv.convert(msg);
  if (msg.channel.name === "status-servidor" && !msg.author.bot) {
    console.log(msg.author.username, msg.content);
    sockets[0].write(`${msg.author.username} : ${msg.content}\0`);
  };
})

client.slashCommands = new Collection()
require('./Handler')(client)

// Register slashcommands
client.on('interactionCreate', (interaction) => {
  if(interaction.type === InteractionType.ApplicationCommand){
      const cmd = client.slashCommands.get(interaction.commandName);
      if (!cmd) return interaction.reply(`Error`);
      interaction["member"] = interaction.guild.members.cache.get(interaction.user.id);
      cmd.run(client, interaction)
   }
})

//Token bot
client.login(token);

Somebody can help me to do this?
 
Hi guys.
My code consists of: send a discord message to the game, and vice versa...
But I'm having some problems with "encoding" from UTF-8 to Unicode.
Here's a video that explains more of what I want to do:


Message received from game -> discord OK
Message received from discord -> game FAIL (with encoding)

My code:

JavaScript:
/*
     Discord bot v1.0 using node.js to create a "bridge" between C++ and Discord.
     Created by @Normynator, and upgraded by HD Scripts 2023
*/

const net = require('net');
const { Client, GatewayIntentBits, Collection, InteractionType } = require('discord.js');
const { token } = require('./config.json');
var Iconv = require('iconv').Iconv;

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildIntegrations
  ]
});

module.exports = client
let sockets = [];

/* Status when bot starts */
const status = [
  '🥇 !help to help',
];

/* Set status and message to bot when online */
client.on('ready', () => {
  client.user.setActivity(status[0]);
    client.user.setStatus('online');
  console.log(`Ready! Logged in as ${client.user.username}`);
});

/* receive from game to discord */
var server = net.createServer(socket => {
  socket.setEncoding("latin1");
  socket.on("data", (msg) => {
    client.channels.cache.get('1049483595061932102').send(msg).then(() => {
      console.log('Message online sent');
      console.log(msg);
    }).catch(err => {
      console.log(err);
    });
  })
  sockets.push(socket);
})
server.listen(1337, "127.0.0.1");

/* Send from discord to game */
client.on("messageCreate", msg => {
  //var iconv = new Iconv('utf-8', 'ISO-8859-1');
  //var buffer = iconv.convert(msg);
  if (msg.channel.name === "status-servidor" && !msg.author.bot) {
    console.log(msg.author.username, msg.content);
    sockets[0].write(`${msg.author.username} : ${msg.content}\0`);
  };
})

client.slashCommands = new Collection()
require('./Handler')(client)

// Register slashcommands
client.on('interactionCreate', (interaction) => {
  if(interaction.type === InteractionType.ApplicationCommand){
      const cmd = client.slashCommands.get(interaction.commandName);
      if (!cmd) return interaction.reply(`Error`);
      interaction["member"] = interaction.guild.members.cache.get(interaction.user.id);
      cmd.run(client, interaction)
   }
})

//Token bot
client.login(token);

Somebody can help me to do this?
Hi there,
Are you getting any errors in console?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom