Discord.js未发送随机响应



我一直在开发一个discord-js机器人。由于某种原因,当我的机器人使用这样的随机响应时,应用程序不会也确实会随机响应。

我甚至试着用一个数组来回应,但discord-js也给我带来了麻烦。

除此之外,无论出于何种原因,当我试图删除回复旁边的捕获(承诺版本(时,它都不起作用,而且当它起作用时,捕获总是会触发。

8ball文件:

const { SlashCommandBuilder } = require('discord.js');
const { logging } = require('../index')
module.exports = {
data: new SlashCommandBuilder()
.setName('8ball')
.setDescription('Ask me a question')
.addStringOption(opt => opt.setName('question').setDescription('question you have for meh').setRequired(true)),

async execute(interaction) {
logging.info(`Log:8ball command was called printing a random message`);
let randomnumber = Math.floor(Math.random() * 4);
if(interaction.options.getString('question').includes('?') === true) {
await interaction.reply({ content:'Sorry, but this command does not support question marks', ephemeral:true })
.catch(logging.error(`Log:A error occurred while trying to send error message in 8ball`))

return;
}
if (randomnumber === 1) {
log.info(`Log:A response 0 was selected, printing response 0`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + 'nA: yes, i suppose');
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error( 'A error occurred while sending response 1, returning'))
return;
}
}
else if (randomnumber === 2) {
log.info(`Log:A response 2 was selected, printing response 2`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + 'nA: maybe, unsure how should I know.');
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error( 'A error occurred while sending response 2, returning'))
return;
}
}
else if (randomnumber === 3) {
log.info(`Log:A response 3 was selected, printing response 3`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + 'nA: definitely not, I am not sure why you would even ask');
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error( 'A error occurred while sending response 3, returning'))
return;
}
}
},
}

另一部分是在生产模式关闭时打印响应的功能:

const logging = {
info: function(msg) {
if(process.env.NODE_ENV === "production") {
return 0;
}
else {      
log.info(msg);
}
return 0;
},
error: function(msg) {
if(process.env.NODE_ENV === "production") {
return 0;
}
else {
log.error(msg);
}
return 0;
}
}

我正在使用nodeJS v16.17.0discord.js v14和Winston记录

编辑:

很抱歉我说得很含糊,而且代码量很大,这个函数的预期输出应该做以下

  • 从getString((函数加载问题
  • 记录到调用命令/8ball的文件(debug.log(
  • 生成一个随机数
  • 使用基于randomnumber的值而更改的响应进行回复
  • 如果回复用户时出现错误,则记录
  • ccb成功回复的日志
  • 退货

但文件所做的是这个(在更糟糕的情况下(

  • 记录8ball已被调用
  • 发送deferReply()
  • 暂停,不向debug.log写入任何内容,没有错误消息,没有成功消息,什么都不做,并保持";建行正在思考">

还有:我知道randomnumber没有处理程序为0,原因是当我第一次创建/8ball时,randomnumber变量似乎从未用0响应,所以我发出它是为了性能

还要记住,每次生产模式都没有在上

再次提前感谢

而不是使用

let randomnumber = Math.floor(Math.random() * 4);

为什么不使用数组?

let randomNumber = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
let randomPercent = Math.floor(Math.random() * randomNumber.length);
let getRandomNumber = randomNumber[randomPercent];

看看这个片段,知道这是否是你需要的。

let randomNumber = ["1", "2", "3", "4", "5", "6", "7", "8"];
let randomPercent = Math.floor(Math.random() * randomNumber.length);
let getRandomNumber = randomNumber[randomPercent];
console.log(getRandomNumber);

编辑:

你使用的是if (randomnumber === 1),当你得到一个随机数时,机器人会响应一个随机日志。你期待什么?

不要多次使用if-else-if-else!请改用变量!

在"执行:"中尝试此代码

async execute(interaction) {
logging.info(`Log:8ball command was called printing a random message`);
let responses = ['yes, i suppose', 'maybe, unsure how should I know.', 'definitely not, I am not sure why you would even ask']; // All responses are here
let randomnumber = Math.floor(Math.random() * responses.length); // Outputs an array between 0 and responses.length -1
let randomresponse = responses[randomnumber]; // Get the random response
if (interaction.options.getString('question').includes('?') === true) {
await interaction.reply({ content: 'Sorry, but this command does not support question marks', ephemeral: true })
.catch(logging.error(`Log:A error occurred while trying to send error message in 8ball`))
return;
}
log.info(`Log:A response ${randomnumber} was selected, printing response ${randomnumber}`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + `nA: ${randomresponse}`);
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error(`A error occurred while sending response ${randomnumber}, returning`))
return;
}
}

希望这能帮助^-^

[+]:您可以在响应数组中添加无限个响应!

我很感激你花这么多时间,即使现在修复看起来很明显

@Tachanks和@Kodeur_Kubic是正确的,

我发现修复方法是按照@Kodeur_Kubic的回答使用数组,而按照@Tachanks的建议,我忘记了0 的处理程序

因此,对于其他人未来的参考,这里是修复的结果代码

const { SlashCommandBuilder } = require('discord.js');
const { logging } = require('../index')
module.exports = {
data: new SlashCommandBuilder()
.setName('8ball')
.setDescription('Ask me a question')
.addStringOption(opt => 
opt.setName('question')
.setDescription('question you have for meh')
.setRequired(true)),

async execute(interaction) {
//defines the invoker
const invokerRaw = interaction.user.tag;
//defines the invoker as a string
const invoker = invokerRaw.toString();
//defines the question as questionRaw
const question = interaction.options.getString('question');
//creates a array for responces
const responce = ['yes, i suppose', 'maybe, idk how should i know?', 'defininately not, im not sure why you would even ask', 'ask me later, im busy!'];

//logs to debug.log that the 8ball commands was called and to genorate a random message
logging.info(`8ball command was called printing a random message`);
//random number object
let randomNumber = Math.floor(Math.random() * 4);
//try to see if the question has a ? in it
if(question.includes('?') === true) {
//prints to the user that the question contained a question mark
await interaction.reply({ content:'Sorry, but this command does not support question marks', ephemeral:true })
.catch((err) => {
//logs the error to debug.log
logging.error(err);
})
.then(() => {
//logs to debug.log that we sent the error message
logging.info('Error message sent to ' + invoker);
})
//returns
return 1;
}
await interaction.reply(`Q: ${question}nA: ${responce[randomNumber]}`)
.catch((err) => {
//logs the error to debug.log
logging.error(err.name);
})
.then(() => {
//logs to debug.log that we sent the reply
logging.info('sent reply successfully')
})
},
}

最新更新