izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

Get server ID of a message #231

Closed FinlayDaG33k closed 6 years ago

FinlayDaG33k commented 6 years ago

Hii there,

I'm trying to create a bot for 3 servers that I maintain and help maintain. This bot is gonna do a set of things, of which one is a system where people can give other users reputation within that server. Now, of course, this comes with a slight problem of having to keep track of the different servers. My idea was to use the server's ID to keep track of the different servers to that records don't get mixed up between servers. I've read the docs, but I couldn't seem to find a way to get the server ID out of the message being sent. Am I missing something?

I've already looked at https://github.com/izy521/discord.io/issues/182 to see if it could help me, which unfortunately, it couldn't as for that example I already needed the ID.

cloudrac3r commented 6 years ago

If you want to get the server ID of the message as it is being sent, it's as easy as instead checking bot.channels[channelID].guild_id in your bot.on("ready" event.

If you have a message ID stored from some time ago... I don't think it's possible to get the server ID just from that. You'll have to store the server ID alongside the message ID at the time when you first receive the message as detailed above.

Is this useful to you?

FinlayDaG33k commented 6 years ago

@cloudrac3r I did not really understand what you meant. I want it to "show" the serverID as it's being send.

Unfortunately, I do still not understand how to get the server ID from the message. Could you provide me with some example code so I can visualise better on what I should do?

EDIT: what you meant is that I could use bot.channels[channelID].guild_id to see what the serverID from which the message is being send is?

FinlayDaG33k commented 6 years ago

Apparently, using bot.channels[channelID].guild_id to get the serverID seems to work :)

bot.on('message', function (user, userID, channelID, message, evt) {
    var ServerID = bot.channels[channelID].guild_id;
});
cloudrac3r commented 6 years ago
bot.on("message", function(user, userID, channelID, message, event) {
    if (message.includes("award reputation")) {
        console.log("Reputation will be awarded to the user with ID "+event.d.mentions[0].id+" in the server with ID "+bot.channels[channelID].guild_id);
    }
});

Obviously, in the actual bot code, you'd use the correct command word for awarding reputation, include some error checking so the bot doesn't crash if no user is mentioned, prevent self-awarding, add some kind of cooldown, and have it store the rep change in a database instead of console.log-ing it. However, this code will provide a practical example of fetching the server ID. I have not tested this code, but I have done similar things in the past and it appears to be accurate.

I hope my formatting is good.

cloudrac3r commented 6 years ago

oh you posted while I was

nevermind

FinlayDaG33k commented 6 years ago

Haha, no problem :) Thanks for the effort anyways <3

Cyberman-tM commented 6 years ago

Sorry to thread-necromancy, but I have to ask - I've tried this code:

var ServerID = bot.channels[channelID].guild_id;

but every time I call the bot, I get an error saying that "channels" is undefined. I've had some more luck by going through servers first - as in bot.servers["id"].channels[channelID] , but I still end up not getting anything.

And every time I ask google, I land here...

Is this outdated, or am I doing something wrong?

FinlayDaG33k commented 6 years ago

@Cyberman-tM ~~your bot variable doesn't have a channels object. your code should look something like this:~~

const Discord = require('discord.js');
const bot = new Discord.Client();

/* some irrelevant code here */

bot.on('message', function (user, userID, channelID, message, evt) {
    var ServerID = bot.channels[channelID].guild_id;
});

EDIT: wrong bot code...

cloudrac3r commented 6 years ago

@FinlayDaG33k Uh, no, your code should really not look like that.

You've imported discord.js. This is not discord.js. This is discord.io. Nothing is compatible between them. Nothing. Your sample code will not work, at all.

FinlayDaG33k commented 6 years ago

@cloudrac3r uh, ye, now that you say that :3 mb :3 realized that I gave a copy-pasta sample from the wrong bot I wrote :3