I have been working on a discord bot and I want to send a message as soon as it gets online in a set announcement channel. I think I know how to make it work but I keep getting the same problem. I've read several articles but they don't solve the problem I have client.channels.get is not a function.
I don't just want to change it to a channel Id as I want it to be possible to be used on other servers than mine. I am a newbie in discord API and coding in general. That is why I don't use too advanced coding as I want to learn what it does instead of copying something I don't understand.
Thanks.
In order to get any properties from the client object, it needs to be loaded from within an event. Say for example inside the ready event, i.e. when the bot is fully loaded, you can then do client.channels.cache.find(channel => channel.name == "name_of_channel"), etc. To listen to an event, you do client.on("event_name_here", (parameters) => {}).
To view a list of events, take a look at: https://discord.js.org/#/docs/main/stable/class/Client
If this didn't help, please post your code so we can properly determine the issue.
Since discord.js v12 you now need to use .cache to access channels collection.
Use client.channels.cache.get() or client.channels.cache.find(channel => channel.name === 'Test') instead.
Related
I am yet to code this but actually struggling to understand how to do this
There is a private server with paid membership. I am a member. There are many channels on that server. I am particularly interested in a specific channel. Once someone posts a message in that channel, I want to immediately extract that message from a different program automatically and then process it in downstream systems. How to do that?
I know I'm a bit late to this question, but I'm going to tell you the secrets the Discord elites don't want you to know: This is completely doable, and many people do it. It's called a "self-bot".
Disclaimer: As the other answer stated, this is against ToS. You probably won't be caught, but I'd keep this on the down-low. Definitely don't go around telling this to everyone you meet.
Ok, now here's the actual coding part:
Discord.js, the most common library for discord bots in JavaScript, no longer supports self-bots, so you'll need to use an older version of discord.js. The official old versions have some unresolved bugs when used with modern discord, so I like to go with discord.js.v11.patch. Using this patched package will fix any weird bugs you get. Note: this is still discord.js v11, so if you need documentation, be sure to look at the v11 docs.
Ok, so after you run npm install discord.js.v11.patch (or npm install -g discord.js.v11.patch if you want to install it globally), you'll need to start writing code. Everything is basically the same as any old discord.js bot, but this is v11 so some stuff might be different. Here's some code to get you started. It should do everything you want it to do:
const discord = require('discord.js.v11.patch');
const client = new discord.Client();
const USER_TOKEN = 'XXXXXXXXXXXXXX'; // change this to your token
const CHANNEL_ID = 'XXXXXXXXXXXXXX'; // change this to the chanel you want to listen to.
client.on('ready', () => {
console.log('bot is running');
});
client.on('message', msg => {
if (msg.channel.id != CHANNEL_ID) return;
const message_text = msg.content;
console.log(message_text); // just an example
// send message_text somewhere to process it.
});
client.login(USER_TOKEN);
Now, all you need to do is change USER_TOKEN to your discord token and CHANNEL_ID to the ID of the channel you want to listen to.
To get your token, I recommend using this gist. It is safe and minimal. If you are worried about accessing your token, don't be. As long as you don't give it to anyone else you're fine.
To get the channel ID, simply turn on developer mode, then right-click on the channel you want to listen to. You should see a Copy ID menu button at the bottom of the context menu.
You can only do this if you have permissions to add an actual bot to that server, or have a friend that can do this for you. Using your own account as a bot would be possible, but is against discord ToS and would get your account banned.
I'm new to .JS language and have been programming a discord bot using Discord.js v12.22.4, but I cannot find a way to filter the messages that are being deleted on a channel.
The line message.channel.bulkDelete(3); works for deleting all the last three messages, but I need to delete only the bot messages and keep the user's messages on that channel.
I have searched the entire internet trying to find a way around it, including the .js documentation, but no success. All the codes I tried gave me errors because the syntax is outdated. My question is: How can I delete a specific amount of messages, from a specific user's ID, from a specific discord channel?
Thanks
I found one website explaining the syntax I was looking for. Hope it helps someone else:
message.channel.messages
.fetch({
limit: 2
})
.then((messages) => {
const botMessages = messages.filter((msg) => msg.author.bot)
message.channel.bulkDelete(botMessages)
})
Is this safe to be added on the function once you added a bot to a server or I can be abused?
I'm kinda scared of getting banned or rate limited So I want to make sure that everything is safe. Currently the fetch in Docs v12 is not that well documented and lack of infos about this. All I know is it fetch all data to the server. The reason I need this is to get all of the information like roles, members, etc because when trying to list all members that has the specific role all try to list members without roles it require me to fetch data since if I don't instead of members(100) it will only fetch like members(3) since the cache is not yet ready or data is not fully loaded to the cache.
await message.guild.members.fetch()
.then(console.log)
Currently I have this command /fetch which will fetch the data with a 1 hour cooldown. I am planning to remove this and add it to "once they added the bot" function. I use this fetch to instances/commands like listing users without roles
message.guild.members.cache.filter(member => member.roles.cache.array().length < 2).map(member => member.user.tag);
If you have like recommendation or a better way to do this you can check the project at PruneBot/commands/custom where full code of this is included:
fetch data
kick no role
kick users with specific role
list users without role
list users with specific role
if you have a large scale bot, that is joining many servers then you might have issues with this call, but if you're not having this issue, then you can pull it when the bot is added to a server, by putting it in the guildCreate event handler. Something like:
client.on('guildCreate', guild=> {
guild.members.fetch()....
I have tried using the client.on('guildDelete', () => {remove roles and other things}) , but everytime the bot is removed from my test server it says "Missing Access" (even though my bot has adminstrative privelages on my test server, and all other features work). I am assuming this is because the event is registered after the bot is removed from the server, which would make sense and is why I am tring to find away around that. Any and all help is appreciated.
Full source code: https://github.com/blb7103/sw-bot
Precisely what you said -- the event fires when the bot has officially left the server, and therefore cannot access/edit anything within that server.
There are no workarounds to this.
I have a small problem with my bot. For developing my bot, I made a new dev-bot which shares the same code as my normal bot, but has it's own token.
However, I ran into a small issue while developing.
I use this code, to get someones' avatar:
client.users.get(event.user.uid).avatarURL
This works fine on my normal but, however on my Dev-Bot I get this error message:
Error getting documents TypeError: Cannot read property 'AvatarURL' of undefined
I think it's due to the fact, that my Bot can't access the avatar of the user, because it doesn't share the same server/guild as this user.
Is there any workaround I could use?
Due to Discord.js and their way of caching, not all users are going to be cached in the bot. While there is a small possibility of it actually not knowing anything about the user, there is a large chance that the Discord API will still allow you to get information form it.
To fix this issue with caching in the latest master, we have to use Client.users, which returns a UserStore. Within the UserStore, we can use a method called fetch to get information about a user.
To fix this issue in the latest stable, we have to use a method called Client.fetchUser, which does the same thing but returns a User instead of a UserStore.
Please note this is only available using a bot account. Here's an example of its usage:
// Using Discord.js Stable
bot.fetchUser(theUsersID).then(myUser => {
console.log(myUser.avatarURL); // My user's avatar is here!
});
// Using Discord.js Master
bot.users.fetch(theUsersID).then(myUser => {
console.log(myUser.avatarURL()); // My user's avatar is here!
});
If there is an error fetching the user (say, a DiscordAPI Permissions Error), this means that there is no way for your bot to get the user's avatar without knowing who the user is first (or sharing a Guild with the user). Happy coding!