I made in my discord.js bot, command "nuke", which makes channel with same: name, permissions, topic etc, and delete "original" channel. But there is one problem, how to make channel in same position as "original"?
Here's my code:
const Discord = require('discord.js')
module.exports = {
name: 'nuke',
execute(message) {
if (!message.member.hasPermission('ADMINISTRATOR')) {
message.channel.send('missing permissions')
}
message.channel.clone().then(msg => msg.send('nuked'))
message.channel.delete()
},
};
In the docs its stated that you can use setPosition to set the position
const Discord = require('discord.js')
module.exports = {
name: 'nuke',
execute(message) {
if (!message.member.hasPermission('ADMINISTRATOR')) {
message.channel.send('missing permissions')
}
message.channel.clone().then(channel => {
channel.setPosition(message.channel.position)
channel.send('nuked')
})
message.channel.delete()
},
};
Here is a simple piece of code that is not much. Its the nuke command crammed into a tiny piece of coding!
const { MessageEmbed } = require('discord.js')
module.exports = {
name: "nuke",
category: "",
run: async (client, message, args) => {
const link = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT9bZSle1XmET-H9Raq7HZA-7L5JH-zQnEeJKsCam2rcsZmjrLcs2nyTDds1hVNMqS11ck&usqp=CAU"
message.channel.clone().then(channel => channel.send(link + ' ☢️ Channel nuked ☢️'));
message.channel.delete();
}
}
You can also do
const Discord = require('discord.js')
module.exports.run = async (client, message, args) => {
if (!message.member.hasPermission('MANAGE_CHANNELS')) {
message.channel.send('You do not have the required Permissons to do that!')
}
message.channel.clone().then(channel => {
channel.setPosition(message.channel.position)
channel.send('https://i.gifer.com/6Ip.gif')
})
message.channel.delete()
},
module.exports.help = {
name: "nuke"
}
Related
const config = require("./config.json");
const Discord = require('discord.js');
const { Intents } = Discord;
const client = new Discord.Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
})
const { REST, Routes } = require('discord.js');
const commands = [
{
name: 'test',
description: 'test',
},
];
const rest = new REST({ version: '10' }).setToken(config.BOT_TOKEN);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationCommands(config.CLIENT_ID), { body: commands });
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'test') {
await interaction.reply('Hello boi!');
}
});
client.login(config.BOT_TOKEN)
const rest = new REST({ version: '10' }).setToken(config.BOT_TOKEN);
^
TypeError: REST is not a constructor
I followed the instructions to create a bot. I run it and then this error occurs. I 've been looking everywhere for a solution
You should probably change the following:
const { REST, Routes } = require('discord.js');
with the following lines:
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v10');
This can been verified by checking this.
I have made a all-in-one moderation bot by compiling certain codes. Recently, I was adding Ticket-System to my bot and it showed me this error. Although, I tried all the fixes I could find on the internet but still couldn't solve it.
Here is my code -->
ticket-config.js
const { Util, MessageEmbed } = require('discord.js');
const configOptions = require('../../configOptions');
module.exports = {
name: "ticketconfig",
description: "Configuration ticket system.",
options: configOptions,
permission: "ADMINISTRATOR",
run: async(interaction, client) => {
const replyMessage = {
content: "Config has been set!"
}
if (interaction.options.getSubcommand() === 'message') {
const message = interaction.options.getString('message');
const content = interaction.options.getString('content') || null;
let data = await client.db.get('config', interaction.guild.id);
if (!data) data = {};
data.message = message;
data.content = content;
await client.db.set('config', interaction.guild.id, data);
return interaction.reply(replyMessage)
}
configOptions.js
module.exports = [
{
name: "message",
description: "Configuration your ticket message",
type: 1,
options: [
{
name: "message",
description: "The message to sent in ticket",
type: 3,
required: true
},
{
name: "content",
description: "content will be appeared above embed message, use /variables command to see all available variables.",
type: 3
}
]
},
./handler/index.js
module.exports = async (client) => {
const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
eventFiles.map((value) => require(value));
const slashCommands = await globPromise(
`${process.cwd()}/SlashCommands/*/*.js`
);
const arrayOfSlashCommands = [];
slashCommands.map((value) => {
const file = require(value);
if (!file?.name) return;
client.slashCommands.set(file.name, file);
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
arrayOfSlashCommands.push(file);
});
client.on("ready", async () => {
await client.application.commands.set(arrayOfSlashCommands);
});
So I'm creating a bot with a handler online, and I'm trying to send an embed through message.reply, but it comes and error saying 'reply' is not defined. Here is the code:
const { Discord, MessageEmbed } = require('discord.js')
module.exports = {
callback: async ({ client, message, ...args }) => {
const embed = new MessageEmbed()
.setAuthor({
name: '» Ping',
iconURL: client.user.defaultAvatarURL
})
.setTitle('Calculating ping...')
.setColor('GREEN')
message.reply({
embeds: [embed]
})
}
}
And here is the handler:
const fs = require('fs')
const getFiles = require('./get-files')
module.exports = (client) => {
const commands = {}
const suffix = '.js'
const commandFiles = getFiles('./commands', suffix)
for (const command of commandFiles) {
let commandFile = require(command)
if (commandFile.default) commandFile = commandFile.default
const split = command.replace(/\\/g, '/').split('/')
const commandName = split[split.length - 1].replace(suffix, '')
commands[commandName.toLowerCase()] = commandFile
}
client.on('messageCreate', (message) => {
if (message.author.bot || !message.content.startsWith('!')) {
return
}
const args = message.content.slice(1).split(/ +/)
const commandName = args.shift().toLowerCase()
if (!commands[commandName]) {
return
}
try {
commands[commandName].callback(client, message, ...args)
} catch (err) {
console.error('[ERROR]', err)
}
})
}
I would appreciate if you found out.
Thanks!
This is my code,
The command executes perfectly, The bot joins the voice channel and also sends the name of the song its about to play, but it doesnt play the song in the voice channel.
This is my first time ever asking a question on stackoverflow so dont mind the format and stuff. But I really need help here.
Discord v13 and latest node module.
const ytsearch = require('yt-search');
const Discord = require('discord.js')
const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
NoSubscriberBehavior
} = require('#discordjs/voice');
module.exports = {
name: "play",
description: "test command",
async run(client, message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('Please connect to a voice channel!');
if (!args.length) return message.channel.send('Please Provide Something To Play!')
const connection = await joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
const videoFinder = async (query) => {
const videoResult = await ytsearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await videoFinder(args.join(' '));
if (video) {
const stream = ytdl(video.url, { filter: 'audioonly' });
const player = createAudioPlayer();
const resource = createAudioResource(stream)
await player.play(resource);
connection.subscribe(player);
await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
} else {
message.channel.send('No video results found');
}
}
}```
I would suggest you look at the music bot example at #discordjs/voice.
They do a good job of how to extract the stream from ytdl.
I'm currently still learning how this all works but the part that you will want to look at is in the createAudioResource function.
public createAudioResource(): Promise<AudioResource<Track>> {
return new Promise((resolve, reject) => {
const process = ytdl(
this.url,
{
o: '-',
q: '',
f: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
r: '100K',
},
{ stdio: ['ignore', 'pipe', 'ignore'] },
);
if (!process.stdout) {
reject(new Error('No stdout'));
return;
}
const stream = process.stdout;
const onError = (error: Error) => {
if (!process.killed) process.kill();
stream.resume();
reject(error);
};
process
.once('spawn', () => {
demuxProbe(stream)
.then((probe) => resolve(createAudioResource(probe.stream, { metadata: this, inputType: probe.type })))
.catch(onError);
})
.catch(onError);
});
}
I would like to know how to make it where if you drop an image in a certain channel, it turns into an embed.
My current code is this:
const Discord = require("discord.js");
const { MessageEmbed } = require("discord.js");
const { Client, RichEmbed } = require("discord.js");
module.exports = {
name: 'icon',
run: async(client, message, args) => {
if(message.channel.id === '708914703338045491'){
message.delete();
let usermsg = args.slice(0).join(" ");
const embed = new MessageEmbed()
.setColor("#2f3136")
.setImage(usermsg);
message.delete().catch();
message.channel.send(embed)
}
}
}
If you upload image into message it's will save into message.attachments, so you can use this collection to check. Discord attachment can be a file, or video, so you need to check file format, the simple way - it's check end of the link to the content in it of a valid format, so i create 2 function.
First: for find attachment
Second: for validate url to content an image format in url
const { MessageEmbed } = require("discord.js");
const avalibleFormats = ['png', 'gif', 'jpeg', 'jpg']
module.exports = {
name: 'icon',
run: async(client, message, args) => {
if (message.channel.id !== '708914703338045491') {
return;
}
let image = getImage(message)
if (!image) {
return;
}
let embed = new MessageEmbed();
embed.setImage(image.url)
message.channel.send(embed)
}
}
const getImage = (message) => message.attachments.find(attachment => checkFormat(attachment.url))
const checkFormat = (url) => avalibleFormats.some(format => url.endsWith(format))