【问题标题】:Making a Discord bot with ts but im getting this error使用 ts 制作 Discord 机器人,但我收到此错误
【发布时间】:2022-01-04 19:09:49
【问题描述】:

您好,我正在尝试为我的不和谐机器人创建一个 nick 命令,但我收到了这个错误。我的机器人正在使用类型脚本。

错误: 表达式expected.ts (1109)

当我替换时

const 提到的成员 = 消息? message.mentions.members?.first():

const 提到的成员 = 消息? message.mentions.members?.first(),

它修复了 const nickName = args.slice(1).join(" ");但给了我错误

':' 预期 ts(1005) [19,76]

代码:

import  "discord.js";
import { ICommand } from "wokcommands";


export default{

    name: 'nickname',
    category: 'Moderation',
    aliases: ['nick'],
    description: 'Nicks a user',
   
    permissions: ['MANAGE_NICKNAMES'], 

    slash: 'both',
    testonly: true,

    callback: async({ client, message, args}) => {

        const mentionedMember = message? message.mentions.members?.first() : 
        
        const nickName = args.slice(1).join(" "); 
        //gets the nickname

        if (!args[0]) return message.reply('You did not mention a user for me to change there nickname!'); 
        if (!mentionedMember) return message.reply('Please mention a user for me to change there nickname \`>nickname @user nickname\`');
        if (!nickName) return message.reply('Please mention a nickname for me to change this users nickname');
        //returns an error message if there isn't a user mentioned or the new nick is not specified

        if (!mentionedMember.kickable) return message.reply('This User Has a senior rank then me i cant change his nickname') 
        //checks if the user that has been mentioned is below the bot in rank of roles, if not the bot won't be able to change the nickname
        
        await mentionedMember.setNickname(nickName) && await message.reply(`Successfuly Changed ${mentionedMember} Nickname to ${nickName}`); 
        //changes the nickname to the specified nickname and sends a message Successfuly
    
    },

}as ICommand

【问题讨论】:

  • 你做了? ,但应该是?.

标签: typescript discord discord.js


【解决方案1】:

看起来您正在尝试执行三元语句,它是 if-else 语句的简写版本,用 ? 表示和:,在哪里?是条件为真时应该发生的情况, : 是条件为假时应该发生的情况。例如:

message = (hasApplesauce) ? 'we have applesauce!' : 'no applesauce :(';

这段代码 sn-p 会将消息设置为“我们有苹果酱!” if hasApplesauce 为真,当 hasApplesauce 布尔值为假时,将其设置为“no applesauce :(”。上面的语句相当于普通 if-else 条件代码中的这个:

if (hasApplesauce)
{
    message = 'we have applesauce!';
}
else
{
   message = 'no applesauce :(';
}

您可以使用与任何其他 if 语句类似的其他条件语句替换该布尔值。编译器可能正在阅读您的消息?在引起错误的行上作为三元语句的开头,这就是为什么它希望您在其后添加等效的 : 。如果你只是想做一个?变量检查,你想换行

const mentionedMember = message? message.mentions.members?.first() : 

进入

const mentionedMember = message?.mentions.members?.first();

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2022-12-18
    • 2022-06-13
    • 2017-09-16
    • 2020-11-02
    • 2019-01-12
    • 2021-11-30
    • 2021-07-24
    • 2018-05-11
    相关资源
    最近更新 更多