简体   繁体   中英

unban command with discord.js v12

I am trying to make an unban command but I get an error const member; ERROR: Missing initializer in const declaration

client.on('message', async message => {
    if (message.content.toLowerCase().startsWith(prefix + "unban"))
    if (!message.member.hasPermission("BAN_MEMBERS")) {
      return message.channel.send(`You cant use this command since you're missing "BAN_MEMBERS" perm`)
    }
    if (!args[0]) return (await message.channel.send('pls enter a users id to unban.')).then(msg => msg.delete({timeout: 5000}))
    const member;
    try {
      member = await client.users.fetch(args[0])
    } catch (e) {
      console.log(e)
      return message.channel.send(('an error accured'));
    }
const reason = args[1] ? args.slice(1).join(' ') : 'no reason';
const newEmbed = new Discord.MessageEmbed()
.setFooter(`${message.author.tag} | ${message.author.id}`, message.author.displayAvatarURL({dynamic: true}))  
message.guild.fetchBans().then( bans => {
  const user = bans.find(ban => ban.user.id === member.id);
  if (user) {
    newEmbed.setTitle(`Successfully Unbanned ${user.user.tag}`)
    .setColor('#FFFF00')
    .addField({name: 'User ID', value: user.user.id, inline: true})
    .addField({name: 'User Tag', value: user.user.tag, inline: true})
    .addField({name: 'Banned Reason', value: user.reason})
    message.channel.send(newEmbed)
}})})

const means that the variable will be immutable (constant). Thus, declaring a const -type variable and not immediately assigning it a value is pointless, and not allowed in Javascript.

To make a mutable variable, instead of const , you should use let .

So, in your code, line 7 should look like this:

let member;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM