-2

I am creating a discord bot for a SAMP ( a online multiplayer server ). So I wanna add a cmd to my dicord bot that takes the text from a text document and sends it in a discord server channel. So, the important thing is that..the text document is the game server log ( which means, it will keep on getting updated every second as anything happens in game) so every thing that happens in game will be written inside the log automatically. I do have the text file and no need to take care of it cause its does on its own. All I need is a cmd that shows the exact things that keeps coming in the text document in one of the discord server channels (A real time update. The bot will keep on posting anything that comes up in that doc). I don't know to make such a command that obtains info from the document and without any changes, keep on posting in the server channel once the command is used. (The text document can be provided, but please note that it will keep on updating each second when the game is active). So, I hope someone can come up with such a command.

The bot is in javascript, so ofc the cmd should be in js ( discord.js, node.js)

I will provide the text doc if necessary. My mind says its pretty simple, but I don't know how to make such a cmd. Sorry and thank you <3

My code (what I have done till now ) provided as per request -

client.on('message', message => {
  const help = new Discord.MessageEmbed()
    .setColor('#f0000')
    .setTitle('INFORMANT BOT HELP')
  .setAuthor('Informant')
  .setDescription('***Hey there! Given below are the list of available `commands`.More interesting commands will be added in the `future`.***')
 .addField('``!info``-',"To see the Infomante Bot Info")
 .addField('``!gangs``-',"To all see the the current gangs in HZG and their leader(s)")
 .addField('``!factions``-',"To see all the current factions and their leader(s)")
 .addField('``!guides``-',"Useful guides for new player. The guides are handpicked from HZG forums help center.")
 .addField('``!quiz``-',"HZG quiz (Only a few questions have been added and more will be coming soon.")
 .addField("``!gangsug``-", "Suggests a cool gang for you according to your interests. (from the current gang, some are selected | This cmd will be updated with more gangs soon!))")
 .addField('``!helpme``-',"Provides some basic help regarding HZG. (This cmd will be updated soon)")

  if (message.content === `${prefix}help`) {
    message.channel.send(help);
  }
  });

  client.on('message', message => {
    const sug1 = new Discord.MessageEmbed()
    .setColor('#f0000')
    .setTitle('HORIZON GAMING - GANG SUGGESTIONS')
    .setAuthor('Informant')
    .setDescription('Hey there! Lets get it started. Are you a person who loves to roleplay? Do you prefer roleplay rather than heavy gun fights? If so, then Sentinel is the best gang for you! The Sentinel is a gang who runs a casino in LS.The like to keep a low profile and mostly engage in RP than regular gun fights like some other gangs. ')
    .setImage('https://cdn.discordapp.com/attachments/744143472503554072/746584514754773112/Untitled_35.png')
    .setFooter(' The bot is in the development stage! More gangs and info will be added to this cmd soon! ')   
    const sug2 = new Discord.MessageEmbed()
    .setColor('#f0000')
    .setTitle('HORIZON GAMING - GANG SUGGESTIONS')
    .setAuthor('Informant')
    .setDescription('Hey there! As you replied, if you are a person who likes to engage in heavy fights more than `RP`, but still want to have some RP fun, then we got gangs like Gsteet,Black Hand Traids and many more gangs! Make sure you check em out ICly! ')
    .setImage('https://cdn.discordapp.com/attachments/744143472503554072/747009277129916436/images_9.jpg')
    .setFooter('The bot is in the development stage! More gangs and info will be added to this cmd soon! ')
    const sug3 = new Discord.MessageEmbed()
    .setColor('#f0000')
    .setTitle('HORIZON GAMING - GANG SUGGESTIONS')
    .setAuthor('Informant BHT')
    .setDescription('The Black Hand Traids, HQ - Verdant Bluffs.  - ')
    .setImage('https://cdn.discordapp.com/attachments/744143472503554072/746623452223635526/Untitled_36.png')
    .setFooter('The bot is in the development stage! More gangs and info will be added to this cmd soon! ')
    const sug4 = new Discord.MessageEmbed()
    .setColor('#f0000')
    .setTitle('HORIZON GAMING - GANG SUGGESTIONS')
    .setAuthor('Informant Yakuza')
    .setDescription('Yakuza Organisation. HQ - Temple, Good with both RP and minimal gun fights.  - ')
    .setImage('https://cdn.discordapp.com/attachments/744143472503554072/747019287402774528/959758414.png')
    const sug5 = new Discord.MessageEmbed()
    .setColor('#f0000')
    .setTitle('HORIZON GAMING - GANG SUGGESTIONS')
    .setAuthor('Informant Golden Palm Crips')
    .setDescription('Golden Palm Crips. Good with fights and basic RP.  - ')
    .setImage('https://i.imgur.com/wqyDp9o.png')
     
    if (message.content === `${prefix}gangsug`) {
    
            
                    message.reply('Hey there! We send the suggestions based on your choise, so please choose what you prefer from the given options-.\n'
                            + 'Confirm with `rp`(roleplay) or with `fight` or `both`.');

                    message.channel.awaitMessages(m => m.author.id == message.author.id,
                            {max: 1, time: 40000}).then(collected => {
                                   
                                    if (collected.first().content.toLowerCase() == 'rp') {
                                            message.reply(sug1);
                                            message.reply(sug4)
                                  
                                    }
                                   if (collected.first().content.toLowerCase() == 'fight'){
                                       message.reply(sug2);
                                       message.reply(sug3)
                                       message.reply(sug5)
                                      }
                                  if (collected.first().content.toLowerCase() == 'both'){
                                    message.reply(sug1);
                                    message.reply(sug3)
                                    message.reply(sug2)
                                  }                                    
                            }).catch(() => {
                                    message.channel.send('request canceled due to no reply.');
                            });
                          }
                        })
                 
  
                        client.on('message', message => {
                          const hzgdonation = new Discord.MessageEmbed()
                          .setColor('#f0000')
                          .setTitle('HORIZON GAMING - SUPPORT!')
                          .setAuthor('Informant BOT')
                          .setDescription('***Check out the donations perks mate!***')
                          .addField ("``CHECK OUT THE DONATION PERKS!``-", "https://www.hzgaming.net/fullpage.php?id=donate")
                          .addField ("``Make sure you fill out this form after donating!`` -", "https://forums.hzgaming.net/forms.php?do=form&fid=16")
                          .setImage('https://cdn.discordapp.com/attachments/744143472503554072/747320822405791905/Untitled_39.png') 
                          const commhelp = new Discord.MessageEmbed()
                          .setColor('#f0000')
                          .setTitle('HORIZON GAMING - SUPPORT!')
                          .setAuthor('Informant BOT')
                          .setDescription('**Feel free to post any help request in `community help` - forums**')
                          .addField ("``COMMUNITY HELP QUESTION FORM``-", "https://forums.hzgaming.net/forms.php?do=form&fid=9")
                           
                          const hzforums = new Discord.MessageEmbed()
                          .setColor('#f0000')
                          .setTitle('HORIZON GAMING - SUPPORT!')
                          .setAuthor('Informant BOT')
                          .setDescription('**Check out HZG Forums using the link below!**')
                          .addField ("`HZG FORUMS`-", "https://forums.hzgaming.net/forum.php")
                          const hzcomp = new Discord.MessageEmbed()
                          .setColor('#f0000')
                          .setTitle('HORIZON GAMING - SUPPORT!')
                          .setAuthor('Informant BOT')
                          .setDescription('**Complaint section and player complaint link are given below!**')
                          .addField ("`Complaint section on forums`-", "https://forums.hzgaming.net/forumdisplay.php/7-Complaints")
                          .addField ("`Player complaint form`-", "https://forums.hzgaming.net/forms.php?do=form&fid=4")
               
                          if (message.content === `${prefix}helpme`) {
                          
                                  
                                          message.reply('Hey there! your help support has arrived! Choose from the given help options and reply.-.\n'
                                                  + 'Confirm with `donation`, `ig questions`, `complaints`or `forums`.');
                      
                                          message.channel.awaitMessages(m => m.author.id == message.author.id,
                                                  {max: 1, time: 40000}).then(collected => {
                                                         
                                                          if (collected.first().content.toLowerCase() == 'donation') {
                                                                  message.reply('**Hey there! You can donate for HZG and get many perks `in game`,`forums` and `HZG discord`.There are different perks for `Ruby donator`, `Saphire donator` and ` Diamond Donator`. Moreover you can donate to recive some induvitual ig/forum items like - `cars`,`house`, `business` and more! Check out all the perks and price using the `link` provided!**');
                                                                  message.reply(hzgdonation)
                                                        
                                                          }
                                                         if (collected.first().content.toLowerCase() == 'ig questions'){
                                                             message.reply('***Hey there! You can always feel free to use `/newb` in game to ask any server related questions! There is also a section called `community help center` on HZG forums where you can ask any question. Using `/gethelp` in game also serves the same purpose.We will try to have a section in this bot where you can ask questions `soon`! You can use `!guides` to see a list of guides that may come handy.***');
                                                             message.reply(commhelp)
                                                          
                                                            }
                                                        if (collected.first().content.toLowerCase() == 'forums'){
                                                          message.reply('Hey mate! There are many things you can do in HZG forums like, general discussions,ask for help,debates,apply for factions, apply for creating a gang and much more! Make sure you check it out!');
                                                          message.reply(hzforums)

                                                          
                                                        }    
                                                        if (collected.first().content.toLowerCase() == 'complaints'){
                                                          message.reply('Hey there! If you saw any player breaking server rules, you can always post a player complaint against that player! (There are even more like - Hacking complaints, Scam complaints)It may take some time for admins to review your complaint once posted. Make sure you got some evidence (prefer a video) and post a complaint using the link provided! ');
                                                          message.reply(hzcomp)
                                                          
                                                          
                                                        }                                                        
                                                  }).catch(() => {
                                                          message.channel.send('request canceled after 40 seconds.');
                                                  });
                                                }
                                              })
                                      


client.login(token);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lolan
  • 1
  • 5
  • Can you show us some code examples? What is already working in your bot? – Anton Aug 24 '20 at 14:34
  • @Anton Hey, I have edited the question and the code has been provided. Also, I don't know to make code like what I have asked in the question. But, its basicaly - I want text from a text document to be posted (real time) in a discord server channel. The text document will get updated on its own, so all I need is a command that gets the text and keeps posting it in a channel where the command is used.Thank you mate! – Lolan Aug 24 '20 at 15:42
  • perhaps, the text dc can be considered as a notepad file – Lolan Aug 24 '20 at 15:45
  • is this code in your question - is what you have now? I'm asking because it seems it is missing some code in the beginning... – Anton Aug 25 '20 at 09:23

1 Answers1

1

Note: this is more a comment, than an answer.

It seems, that you are not tried to google the issue. Let me google for you.

Let's take a look at this searches:

You can google more examples with discord.js send message at specific time search string in Google.

The idea is to send a message as not a response to user request, but at a specific time (every 1 second, for example).

There are different ways to do it. Most popular are: cron and setTimeout.

Cron (from the first answer)

const cron = require('cron');

client.on('message', ...); // You don't need to add anything to the message event listener

let scheduledMessage = new cron.CronJob('00 30 10 * * *', () => {
  // This runs every day at 10:30:00, you can do anything you want
  let channel = yourGuild.channels.get('id');
  channel.send(getLogs()); // <----- here we need a correct message with our logs
});

// When you want to start it, use:
scheduledMessage.start()
// You could also make a command to pause and resume the job

Here we create a cron job to run every X minutes (so 1 run in a minute is maximum speed, but I am not a specialist in cron, there are lots of advices in google how to run every second).

setTimeout (from the second answer)

Just a normal JS timeout:

client.on('ready', () => {
  setTimeout(function(){
    sendMessage(getLogs()); // <----- again, here we need a correct message with our logs
  }, 1000); // 1000 = every second
})

What to send?

The only question now is how to read the log and send only new lines from it.

Right now I do not know how to do this in effective way, I will think about it later. But for the start you can google for node js read file lines into array.

This is one of the first searches:

var fs = require('fs');
var array = fs.readFileSync('file.txt').toString().split("\n");

So your getLogs() function will read file as array of strings and convert each string to Discord.MessageEmbed(), then send it to the channel.

P.S. I will not write all the code for you of course :) but I hope this is enough to start at. If not, feel free to comment.

Anton
  • 2,669
  • 1
  • 7
  • 15
  • Hey, thanks...that was a great explaination! I am right now trying it out... – Lolan Aug 25 '20 at 02:11
  • Im sorry to ask silly questions, but its because idk... For this line - var fs = require('fs'); var array = fs.readFileSync('file.txt').toString().split("\n"); How will I select the exact text file that I want? Copy its path? Or type the file's name? Please explain that too mate – Lolan Aug 25 '20 at 02:15
  • Also, do I need to add something else to this? I mean, how to start this? any cmd that I have to type in the channel I want the bot to send msges? – Lolan Aug 25 '20 at 02:20
  • idk why, a lot of errors are coming. It will be very much aprreciated if you could provide me the code for this cmd, plz..im not getting it – Lolan Aug 25 '20 at 02:28
  • @Lolan maybe I did not understand you correctly. Sorry for that. I thought you need automatic log updater... But if you need a command for getting log (so you send a command to bot and bot responses with a log), then it somewhat other topic. – Anton Aug 25 '20 at 07:58
  • @Lolan so you need to send a command to bot so it will send you logs back, right? – Anton Aug 25 '20 at 07:59
  • correct, I want the cmd when I use it, the bot gets all the text from a txt file ( which we can provide the path to or smth) and it keeps n posting anything that is there in the file on the channel where the cmd is used – Lolan Aug 25 '20 at 08:26