如何使此 Discord.js v14 斜杠命令不会为每个选项返回 “undefined”?

huangapple go评论75阅读模式
英文:

How can i make this discord.js v14 slash command not return "undefined" for each option?

问题

这个命令的想法是将用户添加到包含客户信息的.txt文件中,以便其他部分的机器人可以使用它。

问题是无论我在这里弹出的选项中放入什么,都会始终返回像这样的“undefined”。

命令示例

它将始终返回每个选项的“undefined”,就像这样。

示例回复

我认为问题在于在执行函数中请求该信息时,它实际上并没有返回任何内容。但我不太确定为什么会这样。

以下是相关代码。

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

module.exports = {
  data: new SlashCommandBuilder()
    // 命令名称
    .setName('addfrozencustomer')
    // 命令描述
    .setDescription('添加冻结客户帐户/ Discord')
    .addStringOption(option =>
		option.setName('id')
			.setDescription('客户的ID')
			.setRequired(true))
    .addStringOption(option =>
	    option.setName('username')
			.setDescription('客户的用户名')
			.setRequired(true))
    .addStringOption(option =>
	    option.setName('email')
			.setDescription('客户的电子邮件')
			.setRequired(true)),
    async execute(interaction) {
        const { id, username, email } = interaction.options;
        const data = `${id}:::${username}:::${email}`;
        
        fs.appendFile('users/frozencustomers.txt', `${data}\n`, (error) => {
            if (error) {
            console.error(error);
            return;
            }
        
            console.log('数据已成功写入文件。');
        });
        interaction.channel.send(`成功添加冻结客户:\n ID: ${id}\n 用户名: ${username}\n 电子邮件: ${email}`);
        }
              
};

我尝试的一切中,上面的代码是我最接近预期工作的。

英文:

The idea of this command is to add a user to the .txt file full of customers info so it can be used by other parts of the bot.

The problem is no matter what i put into the options that pop up here

Command Example

it will always return "undefined" for each option like so.

Example Reply

i think the issue is its not actually returning anything from the interaction when asking for that information inside the execute function. im not quite sure why though.

here is the code in question.

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

module.exports = {
  data: new SlashCommandBuilder()
    // command name
    .setName('addfrozencustomer')
    // command description
    .setDescription('Add a frozen customer account/discord')
    .addStringOption(option =>
		option.setName('id')
			.setDescription('The ID of the customer')
			.setRequired(true))
    .addStringOption(option =>
	    option.setName('username')
			.setDescription('The username of the customer')
			.setRequired(true))
    .addStringOption(option =>
	    option.setName('email')
			.setDescription('The email of the customer')
			.setRequired(true)),
    async execute(interaction) {
        const { id, username, email } = interaction.options;
        const data = `${id}:::${username}:::${email}`;
        
        fs.appendFile('users/frozencustomers.txt', `${data}\n`, (error) => {
            if (error) {
            console.error(error);
            return;
            }
        
            console.log('The data was successfully written to the file.');
        });
        interaction.channel.send(`Successfully added frozen customer:\n ID: ${id}\n User: ${username}\n Email: ${email}`);
        }
              
};

Out of everything i have tried the code above is the closest iv come to getting this to work as intended.

答案1

得分: 0

你不能直接从interaction.options对象中获取交互选项的值,它有单独的方法来获取每种类型的值,在你的情况下:

const
  username = interaction.options.getString("username"),
  id = interaction.options.getString("id"),
  email = interaction.options.getString("email")

你可以在这里阅读更多相关信息:https://discordjs.guide/slash-commands/parsing-options.html#command-options

英文:

You can't grab interaction options values directly from the interaction.options object, it has seperate methods to grab each type of value, in your case:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
  username = interaction.options.getString(&quot;username&quot;),
  id = interaction.options.getString(&quot;id&quot;),
  email = interaction.options.getString(&quot;email&quot;)

<!-- end snippet -->

You can read more about this here: https://discordjs.guide/slash-commands/parsing-options.html#command-options

huangapple
  • 本文由 发表于 2023年1月7日 09:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75037547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定