【问题标题】:Setting up chatbot command in discord.js 12在 discord.js 12 中设置聊天机器人命令
【发布时间】:2022-01-10 07:53:43
【问题描述】:

我想制作一个聊天机器人命令,例如命令示例 ?bsetchatbot [频道名称] 这是命令的语法,这里是我使用的代码 注意 - 我已经引入了所需的模块

const Chat = require("easy-discord-chatbot");
const chat = new Chat({ name: "Blabbermouth" });
if (message.content === "?bsetchatbot") {
  channelname = message.mentions.channels.first()
 if(!channelname) { 
 return message.channel.send("Pleease Mention A channel!")
 }

 if(message.channel.name === `${channelname}` && !message.author.bot) {    
    let reply = await chat.chat(message.content)
    message.channel.send(reply)
  }
  message.channel.send(`Chat Bot Channel is set as ${channelname}`) 
}

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: node.js discord.js


【解决方案1】:

问题在于您没有保存人们可以与机器人聊天的频道。我建议使用ValueSaver 包保存频道,因为您可以保存它并在服务器关闭时再次导入它。这是一个例子

const { ValueSaver } = require('valuesaver');
const channels = new ValueSaver();
channels.import(`channels`); // Import the ValueSaver named 'channels' if a save exists with this name

const { Client } = require('discord.js');

const Chat = require('easy-discord-chatbot');
const chat = new Chat({name: 'Blabbermouth'});

const client = new Client();

client.once('ready', () => {
   console.log(`Logged in as ${client.user.tag} on ${new Date().toString()}`);
});

client.on('message', async message => {
    if(message.author.bot || message.channel.type === `DM`) return;

    let args = message.content.substring(1).split(" ");

    const _channelid = channels.get(message.guild.id); // Get the channel id
    if(_channelid){ // If there has been set a channel
         if(message.channel.id === _channelid){ // If the channel is the chat channel
              let reply = await chat.chat(encodeURI(message.content));
              message.channel.send(reply).catch(console.log)
         }
    }

    if(message.content.startsWith('?')){
        switch(args[0].toLowerCase()){
            case 'bsetchatbot':
                const channel = message.mentions.channels.first();
                if(!channel) return message.channel.send(`Please mention a channel to set the chat channel`).catch(console.log);
                channels.set(message.guild.id, channel.id); // Access the channel id by the guild id
                channels.save(`channels`); // Create a new save with the name 'channels'
                break;
        }
     }
});

client.login('Your Token');

ValueSaver:https://npmjs.com/package/valuesaver

【讨论】:

    猜你喜欢
    • 2022-12-23
    • 2018-09-18
    • 2020-08-21
    • 2017-07-08
    • 1970-01-01
    • 2020-08-27
    • 2020-01-22
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多