Cannot cast InputPeerChat to any kind of InputChannel - javascript

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.

Related

/command in one channel and post in another?

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.

How to send a message to another Discord server on a specific channel? [duplicate]

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.
client.channels.get().send()
does NOT work.
It is not a function.
I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.
I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')
What am I missing?
You didn't provide any code that you already tested so I will give you the code for your ready event that will work!
client.on('ready', client => {
client.channels.get('CHANNEL ID').send('Hello here!');
})
Be careful that your channel id a string.
Let me know if it worked, thank you!
2020 Jun 13 Edit:
A Discord.js update requires the cache member of channels before the get method.
If your modules are legacy the above would still work. My recent solution works changing the send line as follows.
client.channels.cache.get('CHANNEL ID').send('Hello here!')
If you are using TypeScript, you will need to cast the channel in order to use the TextChannel.send(message) method without compiler errors.
import { TextChannel } from 'discord.js';
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
for v12 it would be the following:
client.on('messageCreate', message => {
client.channels.cache.get('CHANNEL ID').send('Hello here!');
})
edit: I changed it to log a message.
This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)
This should work
client.channels.fetch('CHANNEL_ID')
.then(channel=>channel.send('booyah!'))
Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.
client.on("messageDelete", (messageDelete) => {
const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
});
Make sure to define it if you're not using messageDelete.

Cannot fetch or get channel?

I'm trying to make something that can detect when there is a mention, and that part works, and sending it does too, somewhat. For some reason, I cannot fetch or get the channel (833856168668561409) in any way. The bot can view the channel and send messages to it, but it can't seem to find it. I'm not sure what's happening, and I can't find anything that will find an answer for this. Thank you for your time, code is displayed below.
if (message.guild.id == 793079701929066517) {
if (message.mentions.members.size) {
var channel = client.channels.fetch("833856168668561409"); //Get log channel.
var webhooks = await channel.fetchWebhooks();
var webhook = webhooks.first();
await webhook.send(`**Ping detected:**\n${Util.cleanContent(message.content, message)}`, {
username: `${message.author.username}`,
avatarURL: `${message.author.avatarURL()}`,
})
}
}
client.channels.fetch returns a Promise. You should await the result.
Without await, channel.fetchWebhooks (channel is a Promise) returns undefined causing the channel.fetchWebhooks is not a function error.
const channel = await client.channels.fetch('833856168668561409')
There's no reason to use var anymore. Use const or let.
Forgot to make the webhook for the channel I was requesting a webhook from.
Basically:
'Bot: I need a webhook for this channel'
'Discord: There is no webhook, so I will give an empty list.'
'Bot: Now I must use this to send a message'
'Bot: I cant...?!?!'
// Basically there is no webhook for it to send with.

Message Specific PFP. (like TupperBox)

I am looking to send a message that has a different profile picture than the bot. Basically, like Tupper Bot does. I can't seem to figure out how to do it anywhere, I've read the documentation and searched around.
You can use a webhook to do what you describe. If you already have a webhook link see here on how to use it. This works from external programs too.
If you don't, you can create a webhook with your bot and then send messages to it. In this example we will create a simple webhook and send a message to it.
First we should check if the channel already has a webhook. That way you don't create a new one everytime you use the command. You do that with the fetchWebhooks() function which returns a collection if there is a webhook present. If that is the case we will get the first webhook in the collection and use that. Note: if you have more then one webhook you can of course add additional checks after the initial one.
If there are no webhooks present, we create a new one with createWebhook(). You can pass additional options after the name.
let webhook;
let webhookCollection = await message.channel.fetchWebhooks();
if (webhookCollection.first()) {
webhook = webhookCollection.first();
} else {
webhook = await message.channel.createWebhook("new webhook", {
avatar: "https://i.imgur.com/tX5nlQ3.jpeg"
});
}
Note: in this example we use the channel the message came from but you can use any channel you want
Technically we can now send messages already.
webhook.send("this is a test");
What you can also do is create a webhookClient. Thats a little like the discord client you create when you start your bot. It takes the webhook ID and token as well as additional options you can set for clients.
const hookclient = new Discord.WebhookClient(webhook.id, webhook.token);
This lets you use a few additional methods like setInterval.
hookclient.setInterval(() => {
hookclient.send("This is a timer test");
}, 1000)
This lets you use the webhook without creating or fetching one first.

Sending private messages to user

I'm using the discord.js library and node.js to create a Discord bot that facilitates poker. It is functional except the hands are shown to everyone, and I need to loop through the players and send them a DM with their hand.
bot.on("message", message => {
message.channel.sendMessage("string");
});
This is the code that sends a message to the channel when any user sends a message. I need the bot to reply in a private channel; I've seen dmChannel, but I do not understand how to use it. I have the username of the member that I want to send a message to.
An example would be appreciated.
Edit:
After looking around for a user object, I found that I can get all of the users using the .users property of the client (bot). I will try using the user.sendMessage("string") method soon.
In order for a bot to send a message, you need <client>.send() , the client is where the bot will send a message to(A channel, everywhere in the server, or a PM). Since you want the bot to PM a certain user, you can use message.author as your client. (you can replace author as mentioned user in a message or something, etc)
Hence, the answer is: message.author.send("Your message here.")
I recommend looking up the Discord.js documentation about a certain object's properties whenever you get stuck, you might find a particular function that may serve as your solution.
To send a message to a user you first need to obtain a User instance.
Obtaining a User instance
use the message.author property of a message the user sent .
call client.users.fetch with the user's id
Once you got a user instance you can send the message with .send
Examples
client.on('message', (msg) => {
if (!msg.author.bot) msg.author.send('ok ' + msg.author.id);
});
client.users.fetch('487904509670337509', false).then((user) => {
user.send('hello world');
});
The above answers work fine too, but I've found you can usually just use message.author.send("blah blah") instead of message.author.sendMessage("blah blah").
-EDIT- : This is because the sendMessage command is outdated as of v12 in Discord Js
.send tends to work better for me in general than .sendMessage, which sometimes runs into problems.
Hope that helps a teeny bit!
If your looking to type up the message and then your bot will send it to the user, here is the code. It also has a role restriction on it :)
case 'dm':
mentiondm = message.mentions.users.first();
message.channel.bulkDelete(1);
if (!message.member.roles.cache.some(role => role.name === "Owner")) return message.channel.send('Beep Boing: This command is way too powerful for you to use!');
if (mentiondm == null) return message.reply('Beep Boing: No user to send message to!');
mentionMessage = message.content.slice(3);
mentiondm.send(mentionMessage);
console.log('Message Sent!')
break;
Just an FYI for v12 it's now
client.users.fetch('487904509670337509', false).then((user) => {
user.send('heloo');
});
where '487904509670337509' is an id number.
If you want to send the message to a predetermined person, such as yourself, you can set it so that the channel it would be messaging to would be their (your) own userID. So for instance, if you're using the discord bot tutorials from Digital Trends, where it says "to: ", you would continue with their (or your) userID. For instance, with how that specific code is set up, you could do "to: userID", and it would message that person. Or, if you want the bot to message you any time someone uses a specific command, you could do "to: '12345678890'", the numbers being a filler for the actual userID. Hope this helps!
This is pretty simple here is an example
Add your command code here like:
if (cmd === `!dm`) {
let dUser =
message.guild.member(message.mentions.users.first()) ||
message.guild.members.get(args[0]);
if (!dUser) return message.channel.send("Can't find user!");
if (!message.member.hasPermission('ADMINISTRATOR'))
return message.reply("You can't you that command!");
let dMessage = args.join(' ').slice(22);
if (dMessage.length < 1) return message.reply('You must supply a message!');
dUser.send(`${dUser} A moderator from WP Coding Club sent you: ${dMessage}`);
message.author.send(
`${message.author} You have sent your message to ${dUser}`
);
}
Make the code say if (msg.content === ('trigger') msg.author.send('text')}

Categories