如何在我的不和谐机器人上实时显示来自文件(文本)的信息

Posted

技术标签:

【中文标题】如何在我的不和谐机器人上实时显示来自文件(文本)的信息【英文标题】:How to show info from a file (text) real time on my discord bot 【发布时间】:2020-12-13 04:56:21 【问题描述】:

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

bot 是 javascript,所以 cmd 应该是 js(discord.js,node.js)

如有必要,我将提供文本文档。我的头脑说它很简单,但我不知道如何制作这样的cmd。对不起,谢谢

我的代码(我到目前为止所做的)根据要求提供 -

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 === `$prefixhelp`) 
    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 === `$prefixgangsug`) 
    
            
                    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 === `$prefixhelpme`) 
                          
                                  
                                          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);

【问题讨论】:

你能给我们看一些代码示例吗?您的机器人中已经有什么功能? @Anton 嘿,我已经编辑了问题并提供了代码。另外,我不知道要像我在问题中提出的那样编写代码。但是,它基本上 - 我希望将文本文档中的文本(实时)发布到不和谐的服务器频道中。文本文档会自行更新,所以我只需要一个命令来获取文本并继续将其发布到使用该命令的频道中。谢谢伙计! 或许,文本dc可以看成记事本文件 您的问题中是否包含此代码 - 您现在拥有的是什么?我问是因为它似乎在一开始就缺少一些代码...... 【参考方案1】:

注意:这更像是一个评论,而不是一个答案。

看来,您没有尝试用谷歌搜索该问题。让我帮你google一下。

让我们看看这个搜索:

How can I send a message every day at a specific hour? Send scheduled message

您可以在 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
)

要发送什么?

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

目前我不知道如何有效地做到这一点,我稍后会考虑。但一开始你可以用谷歌搜索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(),然后发送到频道。

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

【讨论】:

嘿,谢谢...这是一个很好的解释!我现在正在尝试... 很抱歉提出愚蠢的问题,但这是因为 idk... 对于这一行 - var fs = require('fs'); var array = fs.readFileSync('file.txt').toString().split("\n");我将如何选择我想要的确切文本文件?复制它的路径?或者输入文件名?也请解释一下 另外,我还需要添加其他内容吗?我的意思是,如何开始?我必须在频道中输入我希望机器人发送消息的任何 cmd? idk 为什么会出现很多错误。如果你能给我提供这个 cmd 的代码,那将是非常感谢的,plz..im 没有得到它 @Lolan 也许我没有正确理解你。对此感到抱歉。我以为您需要自动日志更新程序...但是如果您需要获取日志的命令(因此您向机器人发送命令并通过日志响应机器人),那么它就有点其他主题了。

以上是关于如何在我的不和谐机器人上实时显示来自文件(文本)的信息的主要内容,如果未能解决你的问题,请参考以下文章

我将如何使用 python 在我的不和谐机器人上添加冷却时间

我试图向我的不和谐机器人添加播放命令,但是

如何让我的不和谐机器人在聊天中显示,例如“机器人正在打字”

如何让我的不和谐机器人回复“跟进”消息

如何将自定义表情符号上传到我的不和谐机器人

我的不和谐机器人上的数据库(Discord.js + mysql)