I've seen my bot join many more servers, but it seems like some are abusing it.
I want the bot to make a one time use invite to a server that I am not on, but my bot is. Once I am on the server I can just remove it. It would be like so:
^backdoor "guild id". I am very new to coding. Thanks!
There are 2 possible ways of doing this, but both are reliant on the permissions that the bot has in that guild.
guildid has to be replaced with an ID or an variable equivilant to the ID
Way 1:
let guild = client.guilds.get(guildid):
if (!guild) return message.reply("The bot isn't in the guild with this ID.");
guild.fetchInvites()
.then(invites => message.channel.send('Found Invites:\n' + invites.map(invite => invite.code).join('\n')))
.catch(console.error);
Way 2:
let guild = client.guilds.get(guildid):
if (!guild) return message.reply("The bot isn't in the guild with this ID.");
let invitechannels = guild.channels.filter(c=> c.permissionsFor(guild.me).has('CREATE_INSTANT_INVITE'))
if(!invitechannels) return message.channel.send('No Channels found with permissions to create Invite in!')
invitechannels.random().createInvite()
.then(invite=> message.channel.send('Found Invite:\n' + invite.code))
There would also be the way of filtering the channels for SEND_MESSAGE and you could send a message to the server.
Instead of entering the guild and then removing it, it would be simpler to just make the bot leave the guild, using Guild.leave()
// ASSUMPTIONS:
// guild_id is the argument from the command
// message is the message that triggered the command
// place this inside your command check
let guild = client.guilds.get(guild_id);
if (!guild) return message.reply("The bot isn't in the guild with this ID.");
guild.owner.send("The bot has been removed from your guild by the owner.").then(() => {
guild.leave();
});
Related
There is the way to edit a message on DiscordJS
To do this, we just need to pass it to the function, the client variable.
exports.myNewFunction = async function myNewTestFunction(client)
First, in order to edit a message using the Discord.JS API, we will need to find the server's GuildID.
const guild = client.guilds.cache.get(`Your Guild ID`);
Now, we're going to need to find the channel the message is on. For this, we will use the channel ID
const channel = guild.channels.cache.find(c => c.id === `Your message Channel` && c.type === 'text');
Finally, let's go to the editing part of the message. For this, we will only need to provide the ID of the message that you want to be edited.
channel.messages.fetch(`Your Message ID`).then(message => {
message.edit("New message Text");
}).catch(err => {
console.error(err);
});
My bot has a Command that creates a Category, and inside it, a Text and Voice channel. On creating the Category I overwrite the permission to let only a certain role to see that category and its channels.
Everything until this works fine. My problem comes when I want my bot to see the text channel, so he can read and interact with messages. My bot doesn't have any extra role aside from the Discord App one.
So far, this are the solutions I tried:
Set Bot to Administrator: This solution works, but want to discart it, since I don't know if it is recommended to set your bot as Administrator, and I believed there should be other ways to solve the problem.
Add Permission to Category, setting my bot role to let him read and write messages, like this:
await category.set_permissions(bot_role, read_messages=True, send_messages=True, connect=True)
This throws an discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access error, which I don't understand since I have all permission set in my oauth page (see photo below).
I have also tried to give the same permissions to the text channel, but it throws the same error.
All I really want is my Bot to read messages in that text channel, since he won't respond to any command if the Bot can't see the channel.
Edit:
This is how I created the category and channels:
async def prepare_game(ctx):
guild = ctx.guild
role = await guild.create_role(name=role_name)
category = await guild.create_category('Game')
await category.set_permissions(role, read_messages=True, send_messages=True, connect=True, speak=True)
text_channel = await guild.create_text_channel('Board', category=category, sync_permissions=True)
voice_channel = await guild.create_voice_channel('Room', category=category, sync_permissions=True)
Then at the end, I added the await category.set_permissions(bot_role, read_messages=True, send_messages=True, connect=True)line, where bot_role is my app bot role
Try setting the permission overwrites for ctx.guild.me (the bot user itself).
From the docs, set_permissions will accept a Role or a Member.
For example:
async def prepare_game(ctx):
guild = ctx.guild
role = await guild.create_role(name=role_name)
category = await guild.create_category('Game')
await category.set_permissions(role, read_messages=True, send_messages=True, connect=True, speak=True)
await category.set_permissions(ctx.guild.me, read_messages=True, send_messages=True, speak=True)
text_channel = await guild.create_text_channel('Board', category=category, sync_permissions=True)
voice_channel = await guild.create_voice_channel('Room', category=category, sync_permissions=True)
Having trouble sending a bot message on a specific channel. I very simply want to have the bot send a message to #general when it activates, to let everyone know it's working again.
In the bot.on function, I've tried the classic client.channels.get().send(), but the error messages I'm getting show that it thinks client is undefined. (It says "cannot read property 'channels' of undefined")
bot.on('ready', client => {
console.log("MACsim online")
client.channels.get('#general').send("#here");
})
The bot crashes immediately, saying: Cannot read property 'channels' of undefined
The ready event doesn't pass any client parameter.
To get a channel by name use collection.find():
client.channels.find(channel => channel.name == "channel name here");
To get a channel by ID you can simply do collection.get("ID") because the channels are mapped by their IDs.
client.channels.get("channel_id");
Here is an example I made for you:
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.on("ready", () => {
let Channel = Client.channels.find(channel => channel.name == "my_channel");
Channel.send("The bot is ready!");
});
Client.login("TOKEN");
You need to get the guild / server by ID, and then the channel by ID, so your code will be something like :
const discordjs = require("discord.js");
const client = new discordjs.Client();
bot.on('ready', client => {
console.log("MACsim online")
client.guilds.get("1836402401348").channels.get('199993777289').send("#here");
})
Edit : added client call
The ready event does not pass a parameter. To gain the bot's client, simply use the variable that you assigned when you created the client.
From your code, it looks like the bot variable, where you did const bot = new Discord.Client();.
From there, using the bot (which is the bot's client), you can access the channels property!
Delete all users from the server with one command, how? Usually, a ban is deleted and only a specific user, but how to delete all with one command?
Heĺlo, as first, you need to get the guild, where you want to kick/ban every member:
//Get the guild by ID
const guild = client.guilds.get("ID");
//Get the guild where a message was sent
const guild = message.guild;
Now if you want to KICK every user in that guild:
//Kick every user in that guild using forEach()
guild.members.forEach(member => member.kick);
Or if you want to BAN every user in that guild:
//Ban every user in that guild using forEach()
guild.members.forEach(member => member.ban);
I want to check if the user reacting to a message has a role or not, so that if the user dont have a rank the bot will ignore the rest of the command, but I dont know how.
example: if a user with the admin rank reacted with :eggsa: emoji, the bot would continue with the command, but if i only had member rank the bot would ignore me.
client.on('messageReactionAdd', reaction => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
var embed = new Discord.RichEmbed()
.setAuthor(reaction.message.content)
.setTimestamp()
.setFooter(reaction.message.author.username, reaction.message.author.avatarURL)
kanal.send({embed});
}
});
the code works in this stage, only everyone can use it
I would be really grateful if someone could help me >:)
If you look at the messageReactionAdd docs you can see that along with the reaction, you can also get the user which added the reaction. So instead of
client.on('messageReactionAdd', reaction => {
You'd have
client.on('messageReactionAdd', (reaction, user) => {
So then you have the user and which is of type User. Because the user isn't of type GuildMember you first need to fetch the correct Guild member before you can check his/her role.
The easiest way to do this is by getting the Message to which the reaction was added with const msg = reaction.message;. Then you can get the guild from the message with const guild = msg.guild;. Now you can access the guild members with const guildMembers = guild.members;. Lastly you'd need to find the correct member with const guildMember = guildMembers.get(user.id);.
Now that you have your Guild Member you can access his/her Roles and thus check whether he/she has or does not have a certain role