I'm hosting my bot on glitch.com, but it restarts every night.
When my bot gets started, it reads a message from a specific channel and then writes in the others. The problem with this is that I can't use any guild or channel until the bot receives a message.
How can I avoid doing this?
You can use guild and channel IDs. Every element in Discord has a unique ID (called Snowflake) that identifies it.
If you want to get a specific guild or channel, you can save its ID and use it in the code. IDs are public, so there's no risk using them (they're not like passwords or tokens).
Guilds and channels are stored in Collections and mapped by their IDs, so you can use them like this:
let guild = client.guilds.get('guild id as a string');
let channel = guild.channels.get('channel id as a string');
To get your guild's ID (or the ID of pretty much any element in Discord) you can enable the Developer mode in the settings, then right-click on the guild and select "Copy ID".
Related
I'm creating a bot for the purpose of Discord moderation, one of the components of this bot is the following task:
"When every new user comes to server, his role by default will be #NotMember, but when he changes his server nickname to the "Nickname | (Real name)", for example, CoolPerson (Alex), then his role is automatically changing to the #Member".
The one way of doing this that I can see is to check if the username contains the brackets, if not then his role is old #NotMember.
Is there any another way to detect if server members have changed their name to a nickname? And is this actually possible?
I'm making this bot in JavaScript, but Python is also welcome here.
You can use the guildMemberUpdate event that is triggered whenever a member changes their name, with this you will have access to the guild member and their old/new username. Note that your bot will need to have the Server Members Intent
enabled on the developer portal.
Example in Discord.js:
client.on("guildMemberUpdate", function(oldMember, newMember) {
// oldMember is the old guild member object before the change, you
// can use it to fetch their old username.
// Add a role to the newMember object.
newMember.roles.add(YOUR_ROLE_HERE);
});
I want to make a bot that can scan every available text channel ID in a server to object, then select the channel based on the name and channel ID. I already have the code to select channel ID from an object, but the objects are loaded from JSON file.
I know there are codes that can search channel ID from a specific channel name, but I have my reasons.
Is it possible to scan available text channel IDs in a server? Thanks.
Yes, there are many ways to achieve this, assuming you have a variable named client that is an instance of discord.js's Client then you can do the following.
First, you need to get the guild.
// if you have not disabled caching
const guild = client.guilds.cache.get('<GUILD_ID>')
// no cache
const guild = await client.guilds.fetch('<GUILD_ID>')
// if you are responding to a message / interaction
const guild = message.guild
Then you get all the channels in the guild
// if you have not disabled caching
let channels = guild.channels.cache
// no cache
let channels = await guild.channels.fetch()
this will give you a Collection of GuildChannel objects. If you want to have only text channels then you can do:
channels = channels.map((c) => c.type === "GUILD_TEXT")
You can convert the Collection into an array doing the following if you find it easier
channels = [...channels.values()]
DISCORD.JS
Hey! So, recently, I was on a server that contained an amazing bot. That was an approval or denial system. So, what would happen for example, somebody would sign a google form and the google script will send the response via a webhook (I already know that code) in an embed to a private channel named "awaiting-result", now, the bot will automatically add reactions to the message, for example, ✅ and ❌. Then, a staff member will react with either one of those emojis and it will send to two different channels. If the reaction was a ✅, then the bot will remove all reactions from the original message, copy the exact embed from the google form response, and send it to a channel named "accepted-logs" with a message above it "Your log has been accepted by ${person}". If it was an ❌, it will do the exact same thing as the approved one. I have been trying hard, but cant find it. All I ready need is the bot code, not the form script. So basically, you react, copy the exact embed, send to another channel. Itll be very helpful, thanks!
List of useful links:
https://discordjs.guide/popular-topics/reactions.html#unicode-emojis
https://discordjs.guide/popular-topics/collectors.html#reaction-collectors
https://discordjs.guide/popular-topics/embeds.html#using-the-embed-constructor
I'm pretty sure you could just store the embed contents in an Object, then you can wait for the collector to collect a ✅ or ❌, check if the user has admin role (e.t.c), and then find the channel the embed needs to be sent too
let channel = client.channels.cache.find(channel => channel.name === "name");
and then you can send the embed to the channel via
channel.send(embed);
In order to make an embed, you just do:
let embed = new Discord.MessageEmbed();
And then you can add fields to it (see link #3). Then you can simply just do
let approvalChannel = client.channels.cache.find(channel => channel.name = "admin-approval");
approvalChannel.send(embed);
// Code for reaction collector
I want to find the user id of the person who DMs the bot. Is there any way to do it? I am using Discord.js
I tried by storing member author and member Id but it didn't work. But when I store channel, it store it as authors tag. But the id for that channel does not matches with the id of the user who DM the bot. I am trying to make support mail bot. But it requires the user id so that I can continue the thread by DMing the user. But it's not possible until I get the user id or server member object. And I can't store that DMchannel in my database because i use json for storing datas.
Due to my low reputation, I can't comment, sorry if this does not answer your question.
You can get the ID of the person who DM'd your bot by message.author.id (keep in mind, message will need to change to whatever variable your message is stored in).
You can also get the channel ID with message.channel.id.
The channel ID is not the same as the user's ID (they are two different things), which I assumed you misunderstood from id for that channel does not matchs with the id of the user who DM the bot.
Try this:
const client = new discord.Client();
client.login('token here');
/* On message event since you want to
* recieve DM and get ID from user who sent DM to your bot.
*/
client.on("message", (msg) => {
// checks if the message's channel type is 'DM'.
if(msg.channel.type === "dm") {
// you can do anything you want here. In my case I put console.log() function.
// since you wanted user ID, you can use msg.author.id property here.
console.log(`Recieved DM from ${msg.author.tag}, DM content is`, msg.content);
}
});
Please remember, author/member ID and dm message channel ID is completely separate thing.
Also storing member related data in JSON or an SQL is not a really good idea here. I suggest you only doing that for custom data you've generated, or that would be wasting a lot of memory.
I have statistics. I am downloading from the User Id database and I am replacing the ID with its name, but if the user has left the server, there is an error because there is no such user.
I have bot.users.get(id) but when a user leaves the server, an error pops up.
Can I get the username with the ID differently if the user is not on the server?
Sorry for being late, like literally. You can perform a request to get a user from Discord by a UserResolvable with Client#fetchUser(<UserResolvable>);.
In practice, the code should look like this;
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.login("YoUr.nIcE.toKe.n");
Client.on("ready", function () { // Should do that when the client is ready.
const User = Client.fetchUser("123456789012345678");
console.log(User); // Some user object.
});
Hope I helped you nonetheless.
~Q
Since client.users stores only cached users, you won't be able to retrieve their username in a reliable way from there.
DISCLAIMER: This method is really inefficient, but discord.js hasn't been made for this kind of work. If you want to make it easier, just write Unknown user (client_id)
If you're not able to get the username from a user list you could try to use messages: use channel.fetchMessages() on every channel of your guild until you find a message in which message.author.id == your_id, when you find it you can get the username with message.author.username
There's also another solution: a self-bot. Keep in mind that this one is not supported by Discord itself, and could result in a permanent ban for the account you're using.
With that said, if you use a self-bot you can use the TextChannel.search() method to quickly find a message with your author id and then grab the username from the author.