You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人模态框提交发私信报错:TypeError: user.send is not a function

问题原因

你遇到的TypeError: user.send is not a function错误,是因为你把**用户ID(字符串类型)**赋值给了user变量,而不是Discord.js提供的User对象。send()是User对象的专属方法,字符串ID没有这个方法,自然调用失败。

修复步骤
  1. 替换用户变量的赋值:把代码中两处const user = interaction.user.id改成const user = interaction.user,直接保存用户对象而非ID;
  2. 直接调用用户对象的send方法:或者更简洁,直接用interaction.user.send()或modalInteraction.user.send()(模态框交互对象里也能拿到当前用户);
  3. 处理私信发送失败的情况:建议给send()添加catch捕获错误(比如用户关闭了私信权限)。
修改后的代码示例
const invoke = async (interaction) => {
    // 保存用户对象而非ID
    const user = interaction.user;

    const modal = new ModalBuilder({
        customId: `createChar-${interaction.user.id}`,
        title: 'Create Character',
    });

    const CharacterName = new TextInputBuilder({
        customId: 'characterName',
        label: 'Input your (firstname_lastname)',
        style: TextInputStyle.Short,
    });

    const PasswordInput = new TextInputBuilder({
        customId: 'characterPassword',
        label: 'Input your Password',
        style: TextInputStyle.Short,
    });
    
    const firstActionRow = new ActionRowBuilder().addComponents(CharacterName);
    const secondActionRow = new ActionRowBuilder().addComponents(PasswordInput);

    modal.addComponents(firstActionRow, secondActionRow);

    await interaction.showModal(modal);

    const filter = (interaction) => interaction.customId === `createChar-${interaction.user.id}`;

    interaction
        .awaitModalSubmit({ filter, time:30_000 })
        .then(async (modalInteraction) => {
            const CharacterName = modalInteraction.fields.getTextInputValue('characterName');
            const PasswordInput = modalInteraction.fields.getTextInputValue('characterPassword');

            // 使用用户对象调用send,并捕获发送失败的错误
            await modalInteraction.user.send(`ASDASD`)
                .catch(err => console.log(`无法发送私信:${err}`));

            modalInteraction.reply(`Your character name: ${CharacterName}\nYour Password: ${PasswordInput}\nMy discord ${interaction.user.id}`);
        })
        .catch((err) => {
            console.log(`Error: ${err}`);
        });
}

内容的提问来源于stack exchange,提问作者Abiel Al Farel Syah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 00:59:54