Cannot fetch or get channel? - javascript

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.

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 do I get info from an embed?

I've been making a suggestion command and I want it so that I can use a -approve <The message id> to edit the message. The problem is when I try to fetch info from it using
const Suggestionchannel = client.channels.cache.get("833796404383973442");
const messages = Suggestionchannel.messages.fetch({ limit: 100 })
it only fetches all the messages and not the embeds. I want it to fetch the embeds so I can get the data and edit the message by its id. With the data, I could display who made the suggestion and what the suggestion was while at the same time showing that it was approved. How would I get the info from an embed using a message id?
On a side note: I know I could use async and await to easily edit it while on the same code but I want it so that it's accessible. If I do that and restart the bot, I won't be able to approve the previous suggestions, only the new ones after I restart the bot.
First, you have to either await fetching the messages from the suggestion channel, or put it into a .then to not have messages be a Promise. Awaiting it wouldn't change anything in your ability to edit the message, you just have to make sure your message cache has the actual message in it.
Fetching the messages from a channel would return a Promise<Collection<Snowflake, Message>>. Once you resolve the message, you get a collection containing messages. On the Message class, there is a array called embeds. It contains all of the embeds part of the message, normally, if sent by a bot, only contains one MessageEmbed.
Another thing is, to make it easier for you, the fetch message also takes in a message ID, and if it's provided, it would fetch the message with that ID from the channel. Doing this would change what it returns to Promise<Message>
Implementing all of these changes in your code would look something like;
const Suggestionchannel = client.channels.cache.get("833796404383973442");
// make sure that your outer scope is asynchronous
const message = await Suggestionchannel.messages.fetch('INSERT MESSAGE ID OR VARIABLE HERE')
const embed = message.embeds[0]
//would be undefined if there is no embed
//would be a MessageEmbed if there is an embed

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.

Send a DM and then reacts to the message once a new user joins

so every time a new user joins I sent the users a message using
client.on ("guildMemberAdd", member => {
member.send("WELCOME")})
Now, is there any way for the bot to react to that message?
Thanks!
Similar to Syntle's answer, but uses promises instead. (My preferred use)
member.send("WELCOME").then(function(msg){
msg.react("😀");
}
You need to contain member.send() in a variable and then use the .react() method on it.
So your solution is:
const msg = await member.send("WELCOME")
await msg.react("🥚");

Discord make channel using bot

I'm making a discord bot, and I'm trying to make use of the createChannel function shown here in the documentation. For some reason, I am getting the following error:
TypeError: bot.createChannel is not a function.
My code is within a function which I pass a message to, and I have been able to create roles and add users to roles within the same function. It's just the createChannel function that's not working. Below is the relevant portions of the code.
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createRole(data);
var newrole = server.roles.find("name", name);
message.author.addrole(newrole);
/* The above 3 lines all work perfectly */
bot.createChannel(server,name);
}
I have also tried bot.addChannel, and bot.ChannelCreate, since ChannelCreate.js is the name of the file which contains the code for this command. Also, I have attempted specifying channel type and assigning a callback function as well, but the main issue is the TypeError saying that this isn't a function at all. Any idea what I'm doing wrong?
Additionally, I plan to use ServerChannel.update() at some point in the future, so any advice on getting that to work once the previous problem is resolved would be greatly appreciated.
Alright, after a few days of trying things and going through the docs, I have discovered the solution. I am using a more recent version of Discord than the docs I was reading were written for. In the newer version, channels are created with a method in the server, not a client method. so, the code should be:
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createChannel(name, "text");
}
The "text" value is the type of channel you are making. Can be text or voice.
I'll post a link to the most recent documentation for anyone else who encounters this problem here.
The answer should update documentation link to the GuildChannelManager which is now responsible for creating new channel.
(Example from docs)
// Create a new text channel
guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
.then(console.log)
.catch(console.error);
https://discord.js.org/#/docs/main/stable/class/GuildChannelManager
#Jim Knee's I think your answer is v11, I'm new in discord.js, using Visual Studio Code's auto-code thingy. You can do all the same things except your thing must be this. If you are poor people, getting errors on doing #Jim Knee's answer, this is the place for "YOU!"
Get rid of server.createChannel(name, "text/voice");
And get it to THIS server.channels.create(name, "text/voice");
Hope I can help at least ;)
I'm just a new guy here too
I think you have not logged in with your bot.
From the docs:
const Discord = require('discord.js');
var client = new Discord.Client();
client.login('mybot#example.com', 'password', output); // you seem to be missing this
function output(error, token) {
if (error) {
console.log(`There was an error logging in: ${error}`);
return;
} else
console.log(`Logged in. Token: ${token}`);
}
Alternatively, you can also login with a token instead. See the docs for the example.

Categories