Get channel ID of newly created channel - Discord.js - javascript

I am trying to get the channel ID of a channel that gets created when a user reacts to a message with a specific emoji. The creation of the channel works perfectly, the only problem is that I have trouble getting the ID of that created channel.
I use the following code for creating the channel:
await message.guild.channels.create(`🎫│${name}`, {
type: 'text',
parent: 'CATEGORY_ID',
permissionOverwrites: [{
id: reactor,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
}]
});
Now I'd like to get that channels ID so that I can send a message in it after creation. I've been looking for answers but haven't found one that actually works.

channels.create returns a promise with a GuildChannel which has an id property. But you don't even need that ID, you can use the .send method on the newly created channel:
const createdChannel = await message.guild.channels.create(`🎫│${name}`, {
type: 'text',
parent: 'CATEGORY_ID',
permissionOverwrites: [{
id: reactor,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
}],
});
// createdChannel.id is the new channel's id
const { id } = createdChannel;
// but you can simply send a message with .send()
createdChannel.send('This is a message in the new channel');

Related

How do I create a text channel discord.js 14

I wanted to ask how to create channels in discord.js 14 (i tried to researched but found nothing) I want to make a simple text channel! :)) Also, with the stuff i found, it worked ( no errors) it just didnt create anything / post errors in debug log
It's literally under Discord.js documentation. Please, next time before asking a question read the docs.
https://discord.js.org/#/docs/discord.js/main/general/welcome
guild.channels.create({
name: "hello",
type: ChannelType.GuildText,
parent: cat[0].ID,
// your permission overwrites or other options here
});
//If the command is slash type
const { Permissions } = require("discord.js")
interaction.guild.channels.create({
name: "new-channel",
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: interaction.guild.id,
accept: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
});
//If the command is message type
const { Permissions } = require("discord.js")
message.guild.channels.create({
name: "new-channel",
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: message.guild.id,
accept: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
});

Slack API : how to update a user message from a button in ephemeral message

I exposed to you my problem with ephemeral message and update of user message
A simplified user case can :
when you write "hi" in a channel, that triggers an ephemeral message with a button "click", and when you click that button, it updates your first "hi" message, to "goodbye" for instance.
This is the code I tried. Unfortunatly, I think I have the wrong message_ts for the update. It seems to be the message_ts of the ephemeral message. Not the one of the original message. What do you think about it?
Have you an idea about how to achieve my goal?
const { WebClient } = require("#slack/web-api");
const slack_bot_token = process.env.SLACK_BOT_TOKEN;
const signing_secret = process.env.SLACK_SIGNING_SECRET;
const webClient = new WebClient(process.env.SLACK_BOT_TOKEN);
const { App } = require("#slack/bolt");
const app = new App({
token: slack_bot_token,
signingSecret: signing_secret,
});
app.message("hi", async ({ message, channel, say }) => {
// Respond to action with an ephemeral message
let channelID = message.channel;
let userID = message.user;
let ts = message.timestamp;
await webClient.chat.postEphemeral({
channel: channelID,
user: userID,
text: `👋 Hi <#${message.user}>`,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `👋 Hey <#${message.user}>`,
},
accessory: {
type: "button",
text: {
type: "plain_text",
text: "Click me",
emoji: true,
},
action_id: "click",
},
},
],
});
});
// when the button with action_id "click" is clicked, the ephemeral message is discarded, and the user message is updated
app.action("click", async ({ ack, body, client, respond }) => {
// Acknowledge action request before anything else
await ack();
let channelID = body.channel.id;
let userID = body.user.id;
let message_ts = body.container.message_ts;
// erase original ephemeral message
respond({
response_type: "ephemeral",
text: "",
replace_original: true,
delete_original: true,
});
// update the original "hi" message
console.log (body.container);
webClient.chat.update({
token: slack_bot_token,
channel: body.container.channel_id,
ts: body.container.message_ts,
text: "goodbye",
replace_original: true,
delete_original: true,
});
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log("⚡️ Bolt app is running!!");
})();
Thanks for your help
You're right that the message_ts you're trying to use right now is not the correct one. You'll need to save the message_ts of the original "hi" message instead.
You can do that in a number of ways, one would be to use a database, store the message_ts value and retrieve it when you need it again. However, if you want to avoid doing that, a neat thing you can do is set the value of the button you're generating in the ephemeral message as the message_ts of the original message.
// Keep in mind I've removed the code that isn't relevant
app.message("hi", async ({ message, channel, say }) => {
...
let ts = message.timestamp;
await webClient.chat.postEphemeral({
...
type: "button",
text: {
type: "plain_text",
text: "Click me",
emoji: true,
},
action_id: "click",
value: `${ts}` // <- store the message_ts here
},
},
],
});
});
So then in the response to the users button click, you'll receive a payload which will, among other things, contain the value that you set earlier when you generated the button. You can then use this value to update the original message, as you are doing right now.
An example of action response payload: https://api.slack.com/reference/interaction-payloads/block-actions#examples

Discord.js Make channel not sync with category

I got a problem where I am making a channel and setting the parent of it to a category and it syncs the permissions from the category which I don't want. Is there a way to stop it so I can give it my own permissions?
Here is my code
message.guild.channels
.create('test', {
type: 'text',
parent: '868148689644957726',
})
.then((channel) => {
channel.permissionOverwrites = [
{
type: 'member',
id: message.member.id,
allow: ['VIEW_CHANNEL'], // is overwritten by the sync of the category
},
];
});
channel.lockPermissions() doesn't accept any parameters, it simply locks in the permission overwrites from the parent channel. Even if you pass false to it, it just ignores it and still "copies" its parent's permissions.
Also, you can't just update the permissions using channel.permissionOverwrites, you need to use the channel.overwritePermissions() method:
message.guild.channels
.create('test', {
type: 'text',
parent: '868148689644957726',
})
.then((channel) => {
channel.overwritePermissions([
{
type: 'member',
id: message.member.id,
allow: ['VIEW_CHANNEL'],
},
]);
});

Discord.js converting Javascript with console into embeds for discord

So I'm working on a bot right now and when looking at the api I get this as the example of how it'll work.
(async () => {
console.log(await bookwebsite.getHomepage(1))
})()
{
results: [ { bookId: 'web id',
thumbnail: 'thumbnail link',
title: 'book title' },
{ bookId: 'web id',
thumbnail: 'thumbnail link',
title: 'book title' },
{ bookId: 'web id',
thumbnail: 'thumbnail link',
title: 'book title' },
...
],
}
Can anyone lead me in the right direction on how to translate this from a console log script to running it within discord embeds? API WARNING NSFW
I'm not extremely sure what you're meaning as of translating console into embeds but I'm guessing you're trying to format the data returned in the api in to a embed in Discord.
const bookwebsite = require('nhentai-js');
(async () => {
var data = await bookwebsite.getHomepage(1);
data = data.results.slice(0, 25);
if(!data) return message.channel.send('Failed to retrieve data from api. ')
// slices to avoid discord embeds going beyond 25 fields
var embed = new Discord.MessageEmbed()
.setTitle('Results found');
// For each the data that's received(after its trimmed)
data.forEach(d=>{
// For every object inside of data, it takes the name and sets it as a field title and sets the description of the field as the id and thumbnail link.
embed.addField(`Name: ${d.title}`, `ID: ${d.bookId}\nThumbnail: ${d.thumbnail}`)
})
// Embeds done, now need to send into the channel
message.channel.send(embed)
})()
If you need any help further on, please comment below.

Create a role with Discord JS

I am making a Discord JS bot which is supposed to have a mute function that assigns a role to a member so they can't text.
I tried looking all over the web for how to create a role (even the Discord JS documentation) but to no avail.
I've tried the code below but it doesn't work (pulled straight from https://discord.js.org/#/docs/main/stable/class/RoleManager?scrollTo=create).
guild.roles.create({
data: {
name: 'Super Cool People',
color: 'BLUE',
},
reason: 'we needed a role for Super Cool People',
})
.then(console.log)
.catch(console.error);
Thanks in advance!
I'm pretty sure that guild is not defined within your code. roles is a property of Guild, so you need a Guild class to access RoleManager and create a Role.
If your code is executed within a command, you can use message.guild to get the Guild, otherwise, you'll need to get the Guild manually.
Here's a simple example of how to use it:
First Scenario
client.on("message", message => {
if (message.author.bot) return false;
if (message.author.id !== message.guild.ownerID) return false;
message.guild.roles.create({
data: {
name: "Muted",
permissions: [],
color: "RED"
},
reason: "Created the mute role."
}).catch(console.log)
});
Second Scenario
const Guild = client.guilds.cache.get("1234567890123456789");
Guild.roles.create({
data: {
name: "Muted",
permissions: [],
color: "RED"
},
reason: "Created the mute role."
}).catch(console.log)
as long as you have a role manager you should have the create function according to the docs
so my advice to fix this issue is see where it goes wrong. I've done it with the createGuild event in typeScript. this will create the roles when the bot joins a new guild.
client.on('guildCreate', async guild => {
await guild.roles.create({ data: { name: 'roleName' } });
});
also be aware that you need high enough permissions to actually create a role.
ps: providing errors when you have an error would be useful (the full error)
If you will try to do guild.create.roles the console will give you an error with message: guild <= is not defined!
You need to write message.guild.create.roles not the guild.create.roles, after this, you will get the created role.
Example code creating the role, with the "debug messages":
message.channel.send('creating a role')
message.guild.roles.create({
data: {
name: 'Testing Role',
color: 'GREY'
},
reason: 'Stackoverflow.com - created for user14470589'
})
.then((res => {
message.channel.send(`debug result:\n${res}`)
})).catch((err => {
message.channel.send(`error:\n${err}`)
}))

Categories

Resources