Hello im trying to make a nick command for my discord bot but I have received this error.My bot is using type script.
Error: Expression expected.ts (1109)
when i replace
const mentionedMember = message? message.mentions.members?.first():
with
const mentionedMember = message? message.mentions.members?.first(),
it fixes const nickName = args.slice(1).join(" "); but gives me the error
':' expected ts(1005) [19,76]
code:
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
CodePudding user response:
It looks like you're trying to perform a ternary statement, which is a shorthand version of an if-else statement, denoted by ? and :, where ? is what should happen when the condition is true, and : is what should happen when the condition is false. For example:
message = (hasApplesauce) ? 'we have applesauce!' : 'no applesauce :(';
This code snippet will set message to "we have applesauce!" if hasApplesauce is true, and set it to "no applesauce :(" when the hasApplesauce boolean is false. The above statement is equivalent to this in normal if-else conditional code:
if (hasApplesauce)
{
message = 'we have applesauce!';
}
else
{
message = 'no applesauce :(';
}
You can replace that boolean with other conditional statements similar to any other if statement. The compiler is likely reading your message? on the error-causing line as the beginning of a ternary statement, which is why it wants you to add an equivalent : after it. If you are just trying to do a ? variable check, you want to change the line
const mentionedMember = message? message.mentions.members?.first() :
into
const mentionedMember = message?.mentions.members?.first();