4

First of all: I'm completely new to coding, all I know is just read or watched on Youtube Tuts.

I'm trying to make a Bot for my Discord Server. He works like a "If-you-get-10-points-you-get-a-new-role" thing.

I started yesterday and I'm already stuck.

Here is my code

const Discord = require('discord.js')
const fs = require('fs')

const config = JSON.parse(fs.readFileSync('config.json', 'utf8'))

const prefix = '+';

var client = new Discord.Client()

client.on('ready', () => {
    console.log(`Logged in as ${client.user.username}...`)
})

client.on('message', (message) => {

    const user = message.mentions.users.first();

    if (!message.content.startsWith(prefix)) return;

    if (message.content.startsWith(prefix + 'padawan')) {

        if (user) {
            const member = message.guild.member(user);
            if (member) {
                member.addRole('517122270158782485').then(() => {
                    message.channel.send(` ${user}, wurde zum Padawan befördert`);
                }).catch(err => {
                    message.channel.send(`${user}, ist bereits ein Padawan`);
                    console.error(err);
                });
            } else {
                message.reply('Der User gehört nicht zu diesem Server');
            }

        } else {
            message.reply('Bitte erwähne wer zum Padawan erhoben werden soll');
        }
    }
});

client.login(config.token);

The code works... sort of. I can assign the role "Padawan" on my Discord Server.
The part with someone already having the role doesn't work yet but that is not my problem.

My problem is that I don't get it to work that the bot first checks if the author of the message has a role called "Master".

I tried things like this:

if (message.member.roles.has('517326538157326336').then(() => {
                                                        ^
TypeError: message.member.roles.has(...).then is not a function

if(message.author.role.has('517326538157326336'))
                           ^
TypeError: Cannot read property 'has' of undefined

I don't really understand why this is not working.

Thanks for your help

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Daikota
  • 57
  • 7

1 Answers1

3

You can use GuildMember.roles.has(id). Please note that Map.has() returns a Boolean, not a Promise.

if (message.member.roles.has('0123456789')) // it has that role
else // it doesn't
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
  • Thank you that helped. My code looks like this now. `if(command === "padawan") { let Master = message.guild.roles.find("name", "Master"); if(message.member.roles.has(Master.id)) { if (user) { ` – Daikota Nov 28 '18 at 16:28