I want to create a &imitate [user] [sentence] command in discord.js, and I want to delete the webhook when it's finished with the command. I have this code:
message.channel.createWebhook("Webhook Test", "https://images.app.goo.gl/2rvCG9hndnTYbQqU9")
.then(webhook => webhook.edit({channel: message.channel})
.then(wbhk => wbhk.send('Testing message'))
.then(wb => wb./*What do I put here to delete the webhook?*/).catch(console.error)
I was looking around for hours and couldn't find anything about this except for the discord.js docs.
To delete a Webhook, you would use Webhook.delete():
message.channel.createWebhook("Webhook Test", "https://images.app.goo.gl/2rvCG9hndnTYbQqU9")
.then(webhook => webhook.edit({channel: message.channel})
.then(webhook => {
return webhook.send('Testing message').then(message => webhook); // returns the webhook, rather than the message to the next .then call
})
.then(webhook => webhook.delete()).catch(console.error)
Related
I have a kick command in my discord.js bot, and I wanted to make the bot send a DM to the person that got kicked. I can not do it like this:
user.send(message)
target.kick(reason).then((m) => {
// do the other stuff here
});
With this code, the DM does not get sent.
This is what I did instead:
user.send(message).then((msg) => {
target.kick(reason).then((m) => {
// do the other stuff here
});
});
Now the issue is that if the target blocks the bot, the bot can not DM them, making the code to kick them not run.
How can I solve this?
You can use .finally, which runs regardless if the promise is fulfilled or rejected.
user.send(message).finally(() => {
target.kick(reason).then((m) => {
// do the other stuff here
});
});
i am trying to make a code that makes sorta ghost accounts and what it does is create a webhook with the users username and avatar then sends what i specify then delete the webhook this is what i have so far but it doesnt seem to work
const Discord = require('discord.js');
module.exports = {
name: 'say',
cooldown:5,
description: 'says anything incliding nitro emotes',
execute(message, args) {
msg = args.join(" ")
message.channel.createWebhook(`${message.member.username}`, message.author.avatarURL())
.then(webhook => webhook.send `${msg}`)
webhook.delete()
},
};
message.member.createWebhook's secone parameter is an object, so replace it with this:
{avatar: message.author.avatarURL()}
then, message.member doesn't have a username attribute. It is in the message.author. So replace message.member.username to message.author.username.
Then, there is a missing () at webhook.send. So replace it with webhook.send(msg).
Finally, webhook.delete is outside the promise callback, so webhook is not defined there. So move it inside the promise callback will resolve it.
So replace the code from message.channel.createWebhook to webhook.delete to the following code:
message.channel.createWebhook(message.author.username, {avatar: message.author.avatarURL()}).then(webhook => {
webhook.send(msg).then(() => {
webhook.delete();
});
});
So I was trying to make my bot send me a DM to every server it joins but I keep getting the API error: Unkown Channel
My code:
bot.on("guildCreate", async guild => {
guild.channels.first().createInvite().then(inv =>
bot.users.get(ownerID).send(`I have been added to **${guild.name}** | ${inv.url}`)
)
});
Okay, this is your problem. I have made the same mistake a few months ago, here's how to fix it.
Since you are using discord.js version 11, guild.channels is indeed a Collection, which you can use .first() on. In this case, you can't do that.
Here's my workaround:
bot.on("guildCreate", async guild => {
var channel;
guild.channels.forEach(c => {
if (c.type === "text" && !channel) channel = c;
});
channel.createInvite({ maxAge: 0 }).then(inv => bot.users.get(ownerID).send(`I have been added to **${guild.name}** | https://discord.gg/${inv.code}`));
});
This basically loops through each channel and finds a valid TextChannel to create an invite.
Hope this helps.
I have had reports off my support server of my discord js bot of my bot being abused in multiple ways.
I want to have a way upon launch of my bot to see a list of servers as well as invite links to those servers. I do not know the id of server or anything.
The most I've managed to find out how to do is this
var server = client.guilds.get("idk the id part");
console.log('I am in the following servers:');
server.createInvite().then(invite =>
console.log(server.name + "-" + invite.url)
);
});```
In your ready event (client.on('ready', () => {}), add the following lines:
client.guilds.tap(guild => {
console.log(`Name: ${guild.name} ID: ${guild.id}`);
})
This should output
Name: Test1 ID: 00000000000000
in the console for each server.
Also, considering that making a lot of invites might clog up your bot, and is generally more of a hindrance than help to server admins, you might consider making a createinvite command:
const svID = args[0];
const guild = client.guilds.find(guild => guild.id === svID);
const channel = guild.channels.find(channel => channel.position === 1);
channel.createInvite()
.then(invite => message.reply(invite.code));
You can either wait for the bot to emit it's ready event and loop through the guild collection:
client.once('ready', () => {
// client.guilds should be ready
});
or handle each guild individually:
client.on('guildCreate', (guild) => {
// The guild the bot just joined/connected to
// Should also be emitted when the bot launches
});
Either should work, but my recommendation would be the second approach, as this will also allow you to track join events whilst the bot is running.
I want to know who is the inviter of the bot but I don't find anything on the documentation or on forum.
If someone has an idea :/
EDIT
Recently discord added an entry in guild audit logs that log everytime someone adds a bot into the server, so you can use it to know who added the bot.
Example:
// client needs to be instance of Discord.Client
// Listen to guildCreate event
client.on("guildCreate", async guild => {
// Fetch audit logs
const logs = await guild.fetchAuditLogs()
// Find a BOT_ADD log
const log = logs.entries.find(l => l.action === "BOT_ADD" && l.target.id === client.user.id)
// If the log exits, send message to it's executor
if(log) log.executor.send("Thanks for adding the bot")
})
Old Anwser
The discord API doesn't allow this.
But, you can send a message to the Owner of the guild using the property guild.owner to get it
client.on('guildCreate', guild => {
guild.owner.send('Message here').catch(e => {
// Can't message to this user
})
})