-1

Go To Bottom For More Info

warn file below
const { MessageAttachment } = require("discord.js");

module.exports = { name: "warn", description: "Warn A Person", async execute(client,message,args,Discord){ if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send(${message.author}You Do Not Have Permission To Use This Command); if (!message.guild.me.hasPermission("KICK_MEMBERS")) return message.channel.send('I require `KICK_MEMBERS`\ permission to manage user warinings');

    const warnRole1 = message.guild.roles.cache.find(role => role.name == '[Warning: 1]');
    const warnRole2 = message.guild.roles.cache.find(role => role.name == '[Warning: 2]');
    const warnRole3 = message.guild.roles.cache.find(role => role.name == '[Warning: 3]');
    const mentiondMember = message.guild.members.cache.get(args[0]) || message.mentions.members.first();
    let punishment = 1;
    let reson = args.slice(2).join(" ");
    if (!warnRole1) await message.guild.roles.create({
        data: {
            name: '[Warning: 1]',
            color: 'RED'

        }
    }).catch(err => console.log(err));

    if (!warnRole1) await message.guild.roles.create({
        data: {
            name: '[Warning: 2]',
            color: 'RED'

        }
    }).catch(err => console.log(err));

    if (!warnRole1) await message.guild.roles.create({
        data: {
            name: '[Warning: 3]',
            color: 'RED'

        }
    }).catch(err => console.log(err));

    if (mentiondMember.role.cache.has(warnRole1.id)) punishment = 2;
    if (mentiondMember.role.cache.has(warnRole2.id)) punishment = 3;
    if (mentiondMember.role.cache.has(warnRole3.id)) punishment = 4;

    if (!args[0]) return message.channel.send(`You need to state a member along with if you are just checking, adding or removing warnings.`);
    if (!mentionedMember) return message.channel.send(`The Member Stated Is Not In The Server`);
    if (!reason) reason = 'No Reason Provided';

    if (!['add', 'remove'].includes(args[1])) {
        if (punishment == 1) {
            return message.channel.send(`${mentiondMember.user.tag} has no warnings.`);
        } else if (punishment == 2){
            return message.channel.send(`${mentiondMember.user.tag} has one warnings.`);
        } else if (punishment == 3){
            return message.channel.send(`${mentiondMember.user.tag} has two warnings.`);
        } else if (punishment == 4){
            return message.channel.send(`${mentiondMember.user.tag} has three warnings.`);
        }
    
    
    }else {
        if (args[1] == 'add') {
            if (punishment == 1) {
                 await mentiondMember.roles.add(warnRole1.id).catch(err => console.log(err));
                 return message.channel.send(`${mentiondMember}, you have been warned for: ${reason}`);
                
            } else if (punishment == 2){
                await mentiondMember.roles.add(warnRole2.id).catch(err => console.log(err));
                await mentiondMember.roles.remove(warnRole1.id).catch(err => console.log(err));
                return message.channel.send(`${mentiondMember}, you have been warned for: ${reason}`);
            } else if (punishment == 3){
                await mentiondMember.roles.add(warnRole3.id).catch(err => console.log(err));
                await mentiondMember.roles.remove(warnRole2.id).catch(err => console.log(err));
                return message.channel.send(`${mentiondMember}, you have been warned for: ${reason}`);
            } else if (punishment == 4){
                message.channel.send(`THIS USER HAS REACHED MAX WARNINGS, CONTACT ADMIN/MANAGER/OWNER`)
                client.channels.cache.get(`822551294957846528`).send(`${mentiondMember}, HAS REACHED MAX WARNINGS, IF YOU SEE THIS CONTACT AN ADMIN FOR PUNISHMENT`);
            }
        } else if (args[1] == 'remove') {
            if (punishment == 1) {
                return message.channel.send(`${mentiondMember.user.tag} has no warnings to remove.`);
                
            } else if (punishment == 2){
                await mentiondMember.roles.remove(warnRole1.id).catch(err => console.log(err));
                return message.channel.send(`I Removed a warning from ${mentionedMember.user.tag}`);
            } else if (punishment == 3){
                await mentiondMember.roles.remove(warnRole2.id).catch(err => console.log(err));
                return message.channel.send(`I Removed a warning from ${mentionedMember.user.tag}`);
            } else if (punishment == 4){
                await mentiondMember.roles.remove(warnRole1.id).catch(err => console.log(err));
                return message.channel.send(`I Removed a warning from ${mentionedMember.user.tag}`);
            }

        }
    }

 }

}

//!!warn @member [add, remove] [reason]

full error: 

    (node:21096) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'role' of undefined
    at Object.execute (E:\Software\Github Repository shit\casey-discord-bot\commands\warn.js:41:28)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21096) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21096) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

info on thing: I tried to make a warn command by using this tutorial: https://www.youtube.com/watch?v=vk_FF887N0o. I followed it step by step twice and it creates a role but it does not fix after that. It makes the role, and then it shows that my other stuff is undefined. If I Type !!warn (!! is the prefix) It would make the 3 roles I stated in the code, but then it gives the error. I try doing !!warn again and it makes more roles. Im a bit confused on why this is happening and if you have an idea please tell me. This bot is also hosted with heroku (if that helps with anything). I also tried to make the role command without a database and looks like its working, just where it says role & roles is undefined for some strange reason.

Bot Info: This is a simple bot that includes a command and event handler (ignore the parts until after the execute ontop of the code).

Bombo43453
  • 81
  • 3

1 Answers1

0

After reviewing a bit of your code and the error message.

I came into this conclusion that the issue was on this line :

if (mentiondMember.role.cache.has(warnRole1.id)) punishment = 2;
if (mentiondMember.role.cache.has(warnRole2.id)) punishment = 3;
if (mentiondMember.role.cache.has(warnRole3.id)) punishment = 4;

Issue 1 :

The variable "mentiondMember" is undefined, you might need to check your :

const mentiondMember = message.guild.members.cache.get(args[0]) || message.mentions.members.first();

and ensure that they are returning a valid GuildMember objects.

Tips : If you are using a debugger, you can put a breakpoint near that "mentiondMember" to easily inspect variables !

Issue 2 :

You are trying to access a non-existant object "role" from "mentiondMember" !

I'm not sure about your Discord.JS version, but in the documentation for Discord.JS version 12.5.3 :

A GuildMember doesn't have "role" properties, instead they have the "roles" properties !

So I think you need to change that line into :

if (mentiondMember.roles.cache.has(warnRole1.id)) punishment = 2;
if (mentiondMember.roles.cache.has(warnRole2.id)) punishment = 3;
if (mentiondMember.roles.cache.has(warnRole3.id)) punishment = 4;