Send DMed files and links to channel in server - discord

Okay. This might be a strange question, but I need help as I could not find anything online. I have a discord bot and server that is hosts a competition, and users submit their submissions by Direct Messaging me a link or file of their submission. I would like to change this to them DMing the bot instead, and the bot posting the links and files in a certain channel in the server. I have absolutely no clue how to achieve this as I am kind of a novice when it comes to this sort of thing. Please comment if I need to change my wording or need to clarify anything!

Replace channel id with the actual id of the channel that you want to send the submissions to.
For Discord.js v12/v13 (the latest version):
client.on('message', ({attachments, author, content, guild}) => {
// only do this for DMs
if (!guild) {
// this will simply send all the attachments, if there are any, or the message content
// you might also want to check that the content is a link as well
const submission = attachments.size
? {files: [...attachments.values()], content: `${author}`}
: {content: `${content}\n${author}`}
client.channels.cache.get('channel id').send(submission)
}
})
For Discord.js v11 replace
client.channels.cache.get('channel id').send(submission)
with
client.channels.get('channel id').send(submission)

In the message event, you can check if the message is in a dm channel then you can take the message content and send it to a specific channel as an embed.
Your solution would be:
client.on('message', (message) => {
if (message.channel.type === 'dm') {
const channel = client.guilds.cache.get("GUILD_ID").channels.cache.get("CHANNEL_ID");
const embed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.avatarURL())
.setColor('RANDOM')
.setDescription(message.content)
channel.send(embed);
if (message.attachments.array().length) {
message.attachments.forEach((attachment) => channel.send({ files: [ attachment ] }))
}
}
})

Related

How to send an embed message to a different channel in interactions?

How to send a separate embed message to a channel different from where the slash command was executed? My current code is the following block.
const { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder, Guild } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('arrest')
.setDescription('puts the member to jail')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to arrest')
.setRequired(true))
.addStringOption(option =>
option
.setName('reason')
.setDescription('The reason for arrest'))
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
const messageEmbed = new EmbedBuilder()
.setTitle('ARRESTED!')
.setDescription(`***${interaction.options.getUser('target').tag}*** *broke a leg. Oops.*`)
.setColor(0xBD7A21)
.setImage('https://eca.astrookai.repl.co/media/arrest.gif')
await interaction.reply({embeds: [ messageEmbed ]});
},
};
Sending a separate embed object to a different channel is intended for channel logging for moderation actions in the server and I would like to know how to achieve this, thank you.
You can use <channel>.send() for this. In which <channel> is your channel object. You can get the channel in a multiple of different ways but this is probably the easiest:
logChannel = <interaction>.guild.channels.fetch(SNOWFLAKE_ID)
Then, you can send your embed to the logchannel like so:
logChannel.send({ embeds: [YOUR_EMBED] });
To account for every command, the easiest way is to do this in your interactionCreate event
If you need more context, you can check out this example which "logs" an embed to the logChannel.

Check The Status Of Another Discord Bot

So i need a bot that tracks another bot's status. like if its online it will say in a channel (with an embed) "The Bot Is Online" And The Same if it goes offline and whenever someone does !status {botname} it shows the uptime/downtime of the bot and 'last online' date
if someone can make it happen i will really appricate it!
also i found this github rebo but it dosen't work, it just says the bot is online and whenever i type !setup {channel} it turns off
The Link to the Repo: https://github.com/sujalgoel/discord-bot-status-checker
Also uh it can be any language, i don't really want to add anything else 😅.
Again, Thanks!
First of all, you would need the privileged Presence intent, which you can enable in the Developer Portal.
Tracking the bot's status
In order to have this work, we have to listen to the presenceUpdate event in discord.js. This event emits whenever someone's presence (a.k.a. status) updates.
Add this in your index.js file, or an event handler file:
// put this with your other imports (and esm equivalent if necessary)
const { MessageEmbed } = require("discord.js");
client.on("presenceUpdate", async (oldPresence, newPresence) => {
// check if the member is the bot we're looking for, if not, return
if (newPresence.member !== "your other bot id here") return;
// check if the status (online, idle, dnd, offline) updated, if not, return
if (oldPresence?.status === newPresence.status) return;
// fetch the channel that we're sending a message in
const channel = await client.channels.fetch("your updating channel id here");
// create the embed
const embed = new MessageEmbed()
.setTitle(`${newPresence.member.displayName}'s status updated!`)
.addField("Old status", oldPresence?.status ?? "offline")
.addField("New status", newPresence.status ?? "offline");
channel.send({ embeds: [embed] });
});
Now, whenever we update the targeted bot's status (online, idle, dnd, offline), it should send the embed we created!
!status command
This one will be a bit harder. If you don't have or want to use a database, we will need to store it in a Collection. The important thing about a Collection is that it resets whenever your bot updates, meaning that even if your bot restarts, everything in that Collection is gone. Collections, rather than just a variable, allows you to store more than one bot's value if you need it in the future.
However, because I don't know what you want or what database you're using, we're going to use Collections.
In your index.js file from before:
// put this with your other imports (and esm equivalent if necessary)
const { Collection, MessageEmbed } = require("discord.js");
// create a collection to store our status data
const client.statusCollection = new Collection();
client.statusCollection.set("your other bot id here", Date.now());
client.on("presenceUpdate", async (oldPresence, newPresence) => {
// check if the member is the bot we're looking for, if not, return
if (newPresence.member !== "your other bot id here") return;
// check if the status (online, idle, dnd, offline) updated, if not, return
if (oldPresence?.status === newPresence.status) return;
// fetch the channel that we're sending a message in
const channel = await client.channels.fetch("your updating channel id here");
// create the embed
const embed = new MessageEmbed()
.setTitle(`${newPresence.member.displayName}'s status updated!`)
.addField("Old status", oldPresence?.status ?? "offline")
.addField("New status", newPresence.status ?? "offline");
channel.send({ embeds: [embed] });
// add the changes in our Collection if changed from/to offline
if ((oldPresence?.status === "offline" || !oldPresence) || (newPresence.status === "offline")) {
client.statusCollection.set("your other bot id here", Date.now());
}
});
Assuming that you already have a prefix command handler (not slash commands) and that the message, args (array of arguments separated by spaces), and client exists, put this in a command file, and make sure it's in an async/await context:
// put this at the top of the file
const { MessageEmbed } = require("discord.js");
const bot = await message.guild.members.fetch("your other bot id here");
const embed = new MessageEmbed()
.setTitle(`${bot.displayName}'s status`);
// if bot is currently offline
if ((bot.presence?.status === "offline") || (!bot.presence)) {
const lastOnline = client.statusCollection.get("your other bot id here");
embed.setDescription(`The bot is currently offline, it was last online at <t:${lastOnline / 1000}:F>`);
} else { // if bot is not currently offline
const uptime = client.statusCollection.get("your other bot id here");
embed.setDescription(`The bot is currently online, its uptime is ${uptime / 1000}`);
};
message.reply({ embeds: [embed] });
In no way is this the most perfect code, but, it does the trick and is a great starting point for you to add on. Some suggestions would be to add the tracker in an event handler rather than your index.js file, use a database rather than a local Collection, and of course, prettify the embed messages!

discord.js - Send image on member ban?

Hi so I would like my bot to send an image in the general chat when someone gets banned by for example, dyno, but I do not know how to do that, if anyone could help, I would appreciate it!
You'd create a listener for guildBanAdd, and send a message to the particular channel when a user is banned.
client.on('guildBanAdd', (guild, user) => {
if(guild.id === 'GuildID') {
const notificationChannel = guild.channels.cache.find(c => c.name === 'general');
notificationChannel.send('Message', {files: ['image address/url']});
}
});

How do you make some bot commands only work in some discord channels

This is my first time using js. I need to ban a certain word in a certain channel. I don't want the message to be deleted if it is not in the specific channel.
For Example:
I want to ban the word "goodbye" in the #greeting channel
BUT
I don't want to ban the word "goodbye" In the #farewell channel
How would I go about this?
Thanks.
By the way I wanted to use example code, but none of it made any sense.
I wrote a simple example for you on how to achieve this:
const Discord = require("discord.js");
const Client = new Discord.Client();
const ForbiddenWords = {
"ChannelID": ["goodbaye", "bye"] // Greeting channel
};
Client.on("ready", () => {
console.log(`${Client.user.tag} is ready!`);
});
Client.on("message", (message) => { // Fired when the bot detects a new message.
if (message.author.bot) {return false}; // Checking if the message author is a bot.
if (ForbiddenWords[message.channel.id]) { // Checking if the current channel has any forbidden words in the ForbiddenWords Object
if (ForbiddenWords[message.channel.id].some(word => message.content.toString().toLowerCase().includes(word))) { // Checking if the message contains any forbidden words
message.delete(); // Deleting the message.
};
};
});
Client.login(process.env.DISCORD_AUTH_TOKEN);

How to know the inviter of the bot ? discord.js

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
})
})

Resources