Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

javascript - How to show info from a file (text) real time on my discord bot

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("
");

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...