如何在Discord机器人上实时显示文件(文本)中的信息

时间:2020-08-24 14:22:25

标签: javascript node.js discord.js

我正在为SAMP(在线多人游戏服务器)创建Discord机器人。因此,我想向我的dicord机器人添加一个cmd,该cmd将从文本文档中获取文本并在不和谐的服务器通道中发送它。因此,重要的是,.. text文件是游戏服务器日志(这意味着,随着游戏中发生的任何事情,它都会每秒更新一次),因此,游戏中发生的所有事情都会自动写入日志中。我确实有该文本文件,无需照顾它,因为它自己完成了。我需要的是一个cmd,它可以显示不和谐服务器通道之一中文本文件中不断出现的确切信息(实时更新。该机器人将继续发布该文档中出现的所有内容)。我不知道要使用这样的命令从文档中获取信息并且不做任何更改,一旦使用该命令,就继续在服务器通道中发布。 (可以提供文本文档,但是请注意,在游戏运行时,它将持续每秒更新)。因此,我希望有人能提出这样的命令。

机器人是用javascript编写的,因此cmd应该用js(discord.js,node.js)

如有必要,我将提供文本文档。我的想法很简单,但是我不知道如何制作这样的cmd。抱歉,谢谢您<3

根据请求提供了我的代码(到目前为止我所做的事情)-

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

1 个答案:

答案 0 :(得分:1)

注意:这更多是评论,而不是答案。

看来,您并未尝试通过Google搜索该问题。让我为你用谷歌搜索。

让我们看一下此搜索:

您可以在Google中使用discord.js send message at specific time搜索字符串来搜索更多示例。

想法是发送消息不是对用户请求的响应,而是在特定时间(例如每1秒)发送一次。

有不同的方法来做到这一点。最受欢迎的是:cronsetTimeout

Cron (来自first答案)

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

在这里,我们创建了一个cron作业,每隔X分钟运行一次(因此,在一分钟内运行1次是最高速度,但我不是cron的专家,google中有很多建议如何每秒运行一次) )。

setTimeout (来自second答案)

只是正常的JS超时:

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

要发送什么?

现在唯一的问题是如何读取日志并仅发送新行。

现在,我不知道如何有效地执行此操作,稍后我会考虑。但首先,您可以在Google上搜索node js read file lines into array

这是first searches之一:

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

因此,您的getLogs()函数将文件读取为字符串数组,并将每个字符串转换为 Discord.MessageEmbed(),然后将其发送到频道。

P.S。我当然不会为您编写所有代码:),但我希望这足以开始。如果没有,请随时发表评论。