-1

I developed an algorithm that when u type in discord server's text channel : "-nuke @tag" that member will get a "Muted" role and his id will be added to an array. If he leaves the server and joins back, the bot will compare the new member's id with all the ids in the array and if they are matching it will automatically give that person the "Muted" role. The problem is, the bot.on doesn't seem to work inside something else than index.js. I dont really want to go inside all the event handlers and stuff, just to get this one working nuke.js

cricri
  • 1
  • 1
  • what you could do is use a global map in which you place the ID and then simply compare the IDs from that map with incoming ones and proceed from there. – Worthy Alpaca Jan 18 '21 at 21:10
  • I do not really understand how should i do that. Do you have some code sample to show me? – cricri Jan 18 '21 at 21:37

1 Answers1

0

This answer is in reference to a comment I made. This will show you how to create and access a global map.

To start you need to extend the client class by the map you want to use. You do that like this.

const { Client } = require('discord.js');

module.exports = class extends Client {
    constructor(config) {
        super({});

        this.muteIDs = new Map(); // you can name it whatever you like

        this.config = config;
    }
};

You should do that in a separate file. Lets call that file clientextend.js. Now you can create your bot client like you usually would, however you need to require the file that you just created. This example has both the file in which you extend the client and the file you create the client in, in the same directory. Note: In your example you have bot instead of client.

const Client = require('./clientextend.js')

const client = new Client();

You now have access to the map you created anywhere you have your client available to you. You can access it like this.

muteIDs = client.muteIDs;
Worthy Alpaca
  • 1,235
  • 1
  • 6
  • 13
  • so, i tried to do what u said here, but when i just put a console.log inside the index.js "guildMemberAdd" it doesnt outputs anything in the console when someone joins – cricri Jan 19 '21 at 07:02
  • ah yes, that is a different thing altogether. See if [this](https://stackoverflow.com/questions/64739350/discord-js-bot-welcomes-member-assign-a-role-and-send-them-a-dm/64739684#64739684) helps you out. – Worthy Alpaca Jan 19 '21 at 21:13
  • hello! i am sorry i didnt respond earlier but i was kinda busy. I manged to fix the guildMemberAdd problem, but i still have some problems. My variable name is " nukedids " and whenever i call it in the index file i get the error: "TypeError: nukedids is not a function" even though i typed that `nukedids = bot.nukedids` at the beginning of my index file. Yes, i typed `const Client = require(...)` for the client extend in both the index and the nuke.js file and it still doesnt work [code](https://pastebin.pl/view/27b9a26e) inside guildMemberAdd in index.js : – cricri Jan 22 '21 at 19:12
  • it looks like you didn't use it correctly. Make sure that you initiate the client correctly. The client in your case is `bot`. If you want to use client in the `nuke.js` then you need to either pass it to your command handler or initialize it in such a way that you can access it from anywhere. Also keep in mind that a `map` is not an array. – Worthy Alpaca Jan 22 '21 at 19:21
  • i think i got it to work, the problem is the id itself. The id i get inside guildMemberAdd is different than the one i get in my nuke.js: the code in guildmemberadd: `member.guild.id;`, nuke.js: `const member = message.mentions.members.first(); ..... member.id` – cricri Jan 22 '21 at 19:35
  • that sounds unlikely. The userID is always the same. Thats how IDs work – Worthy Alpaca Jan 22 '21 at 23:06
  • maybe it is the method i use to get the id? when the member joins, i get the id in a different way than i get in nuke.js (the id of the person i tagged). I dont find any answers online – cricri Jan 23 '21 at 08:39
  • it looks like you are comparing guildIDs with userIDs in your event. That doesn't work for obvious reasons. – Worthy Alpaca Jan 23 '21 at 18:16