/command in one channel and post in another? - javascript

My code is: (EDITED)
const channels = [];
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('mainframe')
.setDescription('bot will post to the mainframe for you.')
.addStringOption(option =>
option.setName('post')
.setDescription('What you would like posted.')
.setRequired(true)),
async execute(interaction) {
const post = interaction.options.getString('post');
let response = `"${post}".`;
await interaction.guild.channels.cache.get(channels[getRandomInt(channels.length)]).send({ content: response });
},
};
What I would like is for the bot to accept the command ephemerally and repost what you send it to another channel or randomly pick from array of channel IDs.
As I understand it, this is typically not possible in this direct fashion, according to another answer I've seen on here. However this response makes me think the way to do it is to have the bot store the URLs in a collection, and then post it on its own to some directed channel.
Is this possible?

Yes it is possible. You just need to know the id of the specific channel, the message content, embeds (if wanted) and the bot need permission to send messages into that channel. You will need to search in the guild for the channel and then just send the message into the channel like every other normal message:
interaction.guild.channels.cache.get('channelId').send({ content: 'your message' });
If you have further questions just comment on my response.

Related

Cannot cast InputPeerChat to any kind of InputChannel

I have created a bot using Telegraf. I want that when a user sends a message, the bot will send him the previous message. So I want to take the previous post on id with Gram JS but throws this error
here is my code:
bot.on("message", async (ctx) => {
const { text, message_id } = ctx.message;
const userId = ctx.from.id;
const replyToMessage = await client.invoke(
new Api.channels.GetMessages({
channel: `${ctx.chat.id}`,
id: [message_id - 1],
})
);
console.log(1234, replyToMessage);
ctx.reply(replyToMessage);
});
I was inspecting telegram telethon api for a python task. I have some thougths about your issue.
The thing is telegram says it can not find anything with that id and channel. But I have some questions about your code.
As far as I know telegram either asks for a channel_id and channel_access_hash or the channel_username.
I am seeing that you give the telegram a channel_id and message_id ?
You should check your api docs again and try to find a method you can directly use the channel's username.
Note on that username : Telegram group or chat must be public or you must be auth, and (as far as python telethon) you must add the https:// appendix to channel_username.
I hope you can find a way out. If you further detail your question we can talk it again, I have spend plenty of time with python's telethon api.

How can I add a role to user, without him sending any message first?

I've searched for answers but only found how to assign a server role to someone who either sent some message or called a command.
How can I find a specific user on a server and assign a role to them without them sending any messages or doing anything at all?
I tried some things that didn't work:
// the server bot is in
const server = client.guilds.cache.get("my server id here");
// trying to find specific user on the server
let myusr = server.members.cache.get("id", "user id here");
Any suggestions/solutions?
EDIT: Here's the code I have so far:
const { Client } = require('discord.js');
const client = new Client();
client.on('ready', () => {
console.log(client.user.tag);
// grabbing the server
const guild = client.guilds.cache.get("my server's id");
// calling a function and passing my server to it
setUsrRole(guild);
});
function setUsrRole(server) {
// grabbing my user
let myusr = server.members.fetch("My user id");
// finding my role by name
let myRole = server.roles.cache.find(myRole => myRole.name === "role name");
// trying to add the role to my user
myusr.roles.add(myRole);
// I also tried myusr.addRole(myRole);
};
// Bot login
client.login('my bot token');
There are multiple ways, but I will show you 2 ways.
client.on('guildMemberAdd', member => {
member.roles.add('someRole');
})
That way is if you want to add on member join, but you need to make sure in your discord developer portal, when you invite your bot, the "Server Members Intent" box in the "bot" section is checked. Another way, which might be based on a different event is here:
await client.guilds.fetch(); //cache guilds
const guild = client.guilds.cache.get('someGuild');
await guild.members.fetch(); //should cache users automatically
const member = guild.members.cache.get('someMember');
member.roles.add('someRole');
//MAKE SURE ITS ASYNC
If these don’t work, please comment, and tell me what error you get.

discord.js: How can I make the bot send mention, but not in an embed?

I was working on my join message and was curious how can I make the bot send mention not in an embed, like this: photo
And I Have like this:photo
My current code is:
client.on('guildMemberAdd', async(member) => { // this event gets triggered when a new member joins the server!
const Channel = member.guild.channels.cache.get('761871578178387989')
const embed = new Discord.MessageEmbed()
.setTitle(`:wave: Hello ${member.user.tag} and welcome to **${member.guild.name}**.`)
.setThumbnail(member.user.displayAvatarURL({dynamic: true, size: 512}))
.addField('Consider checking it!', '**Server IP:** play.aftnetwork.net\n**Store:** https://store.aftnetwork.net\n\n**Make sure to check <#761874528702824469>.**', true)
.setFooter(`AftNetwork`)
.setColor('#468DFF')
// sends a message to the channel
Channel.send(embed)
})
What should I add to this code to get the wanted result? Help pls <3
You can do that by using the TextChannel.send() method, which accepts an optional options argument (MessageOptions) as the second parameter, in which you can specify the embed.
Here's an example:
Channel.send(member, {
embed: embed,
});
// member is the GuildMember that joined your server and embed is your MessageEmbed.

How do I post a welcome message when my discord bot joins a server

I am new to JavaScript and I have made a small discord bot but I cannot find a way to make it have a welcome message when it joins the server. I want it to say something like "Hello my name is {bot-name}, type !help to view my commands." Does anyone know how to do this?
You can use the guildCreate event that is emitted whenever the bot joins a guild. You need to find a channel though where you can post your message.
To find a channel you can use the .find() method that returns the first channel where the given function returns a truthy value.
client.on('guildCreate', (guild) => {
const channelNames = ['general', 'welcome']
const channel = guild.channels.cache.find(ch => channelNames.includes(ch.name))
if (channel)
channel.send(
`Hello my name is ${client.user.username}, type \`!help\` to view my commands.`
)
.catch(console.error);
})

How do I make it so that a user can not post more than one message in a row, discord.js

My question is similar to This question, but the linked question is for discord.py, I am looking for an answer for discord,js.
Now the question: I am looking to create a counting channel (similar to the Countr Bot), and I would like to make it so that if a user double posts in a channel, the second message gets deleted. How would I go about doing this?
Well, it's very not so hard to forbid the user to post messages in a certain text channel. But the real problem is, how to manage this task and balance the load.
There are two ways:
(text) mute user
edit its permission to post in a certain channel
And bot should have the necessary permissions for this operations.
Here is a simple example:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('your token here').catch(e => console.error(e));
const buffer = new Set();
//listening for all the messages, actually you should listen for messages in a certain channel
bot.on('message', message => {
if (buffer.size) {
//unmute, or return permissions to normal for stored users.
buffer.clear() //clear buffer set
}
buffer.add(message.author.id) //add user with latest message to buffer
/**
* your own code implementation of
* muting or editing message.author.id permission
* in a cerain channel, which should forbid him to post
**/
})

Categories