I'm porting my commands from vanilla discord.js to commando framework. I had a command that, when the argument was missing, it warned the user.
const { Command } = require('discord.js-commando');
module.exports = class ServerCommand extends Command {
constructor(client) {
super(client, {
name: 'say',
aliases: ['s'],
group: 'mod',
memberName: 'say',
description: 'Make the bot speak for you.',
userPermissions: ['MANAGE_CHANNELS'],
examples: [client.commandPrefix + 'say <text>',client.commandPrefix + 'say hello world'],
args: [{
key: 'text',
prompt: 'What should the bot say?',
type: 'string',
}]
})
}
run(message, { text }) {
if (text == '' || !text)
message.say("You didn't specify what the bot should say.")
message.say(text);
message.delete();
}
}
Now, with commando, it automatically warns the user with a reply when the arg is missing.
My question is, how can I overwrite it?
Thanks in advance!
I handled this adding default property 'default': 'isempty' and then I catch it in the command's code. So, the code looks like it:
const { Command } = require('discord.js-commando');
module.exports = class ServerCommand extends Command {
constructor(client) {
super(client, {
name: 'say',
aliases: ['s'],
group: 'mod',
memberName: 'say',
description: 'Make the bot speak for you.',
userPermissions: ['MANAGE_CHANNELS'],
examples: [client.commandPrefix + 'say <text>',client.commandPrefix + 'say hello world'],
args: [{
key: 'text',
prompt: 'What should the bot say?',
type: 'string',
'default': 'isempty',
}]
})
}
run(message, { text }) {
if (text == 'isempty') return message.say("You didn't specify what the bot should say.");
message.say(text);
message.delete();
}
}
Related
I'm trying to create a simple add slash command with discord.js, but it seems that interaction.options.getNumber just doesn't exists (look at https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/structures/CommandInteractionOptionResolver.js#L181)
This is a piece of my code:
...
commands?.create({
name: 'add',
description: 'add two numbers',
options: [
{
name: 'num1',
description: 'The first num',
required: true,
type: DiscordJS.ApplicationCommandOptionType.Number
},
{
name: 'num2',
description: 'The second num',
required: true,
type: DiscordJS.ApplicationCommandOptionType.Number
} ]
})
})
...
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) {
return
}
const { commandName, options } = interaction
if (commandName === 'add') {
console.log(options)
const num1 = options.getNumber('num1')!
const num2 = options.get('num2')!
// idk why but "get" method exists. So I was trying to use it,
// but js says that you couldn't add a string | number | boolean value to an another one
console.log(num1.value + num2.value)
console.log(typeof(num2.value)) // this print "number" btw
interaction.reply({
content: `The sum is ${num1.value}`
})
}
}
There is a solution, but it's intricated:
...
} else if (commandName === 'add') {
console.log(options)
const raw_num1 = options.get('num1')!
const raw_num2 = options.get('num2')!
const num1 = raw_num1.value!
const num2 = raw_num2.value!
interaction.reply({
content: `The sum is ${+(num1) + +(num2)}`
})
}
})
I'm trying to make a discord slash commands filter using distube but when I use the slash command it didn't apply the filter that I picked but it sent the embed when filter is applied
Node: v17.7.2
Discord: ^13.2.0
can someone help me or tell me why the filter is not applying to the current music in vc?
thank you
const { MessageEmbed } = require('discord.js');
const ee = require('../../config.json');
module.exports = {
name: 'filter',
description: 'Add filter.',
usage: 'filter',
options: [
{
name: 'preset',
description: 'Filters to add.',
type: 'STRING',
required: true,
choices: [
{
name: 'BassBoost',
value: 'bassboost'
},
{
name: 'Nightcore',
value: 'nightcore'
},
{
name: 'Vaporwave',
value: 'vaporwave'
}
]
}
],
run: async(client, interaction, args) => {
const filterss = interaction.options.getString('preset');
const queue = client.distube.getQueue(interaction);
let player = client.distube.filters;
if(!queue) {
return interaction.followUp({embeds: [
new MessageEmbed()
.setColor(ee.color)
.setAuthor({name: 'Error', iconURL: 'https://i.imgur.com/81ig9jl.jpg'})
.setDescription('No songs are playing!')
]})
}
let thing = new MessageEmbed().setColor(ee.color);
if (filterss == "nightcore") {
thing.setDescription("✅ | Nightcore filter is now active!");
player.nightcore = true;
} else if (filterss == "bassboost") {
thing.setDescription("✅ | BassBoost filter is now on!");
player.bassboost = true;
} else if (filterss == "vaporwave") {
thing.setDescription("✅ | Vaporwave filter is now on!");
player.vaporwave = true;
}
return interaction.followUp({ embeds: [thing] });
}
}
From what I can find in the docs, it is recommended to use .setFilter() instead of calling client.distube.filters.<filter> = true.
I'd try changing your run code to use this instead and see if that works:
run: async(client, interaction, args) => {
// Your queue check code here
let thing = new MessageEmbed().setColor(ee.color);
if (filterss == 'nightcore') {
thing.setDescription('✅ | Nightcore filter is now active!');
client.distube.setFilter(interaction, filterss);
} else if (filterss == 'bassboost') {
thing.setDescription('✅ | BassBoost filter is now on!');
client.distube.setFilter(interaction, filterss);
} else if (filterss == 'vaporwave') {
thing.setDescription('✅ | Vaporwave filter is now on!');
client.distube.setFilter(interaction, filterss);
}
return interaction.followUp({
embeds: [thing],
});
}
I want the main.js loop to be re-selectable when the user has selected option.
Here is the code.
main.js
const prompts = require('prompts');
var option = require('./option.js');
(async () => {
const response = await prompts({
type: 'select',
name: 'value',
message: 'choice',
choices: [
{ title: 'option1', description: 'option1 description', value: '1' },
{ title: 'exit', description: 'exit from script',value: '0' }
],
initial: 1
}
).then(response => {
if(response.value == 1){
console.info('you select option1');
option.option1()
// i want to run main.js again here
}
else {
console.info('you select exit');
// i want to exit from main.js
}
});
})();
option.js
module.exports = {
option1:function() {
console.log("You have selected option 1")
}
}
When the user has selected option1, after the option.js script's function is done, I would like it to return to the new options page.
I've tried many ways but I still can't find a way. What can I try?
Is this the solution to your problem? Also notice when using await, dont use then.
const prompts = require('prompts');
var option = require('./option.js');
async function prompt () {
const response = await prompts({
type: 'select',
name: 'value',
message: 'choice',
choices: [
{ title: 'option1', description: 'option1 description', value: '1' },
{ title: 'exit', description: 'exit from script',value: '0' }
],
initial: 1
})
return response.value === '1'?option.option1() & prompt():false
}
prompt()
I'm having some issues with giving the message.author and staff permission to see the channel right after it's created, the issue is, when the channel's parent (category) is changed, it syncs the permissions of the parent (category), which disallows the user and staff to see the channel, I'm not sure how to fix this, I wish I explained well, if you have any questions, please ask.
Here is my code:
module.exports = {
name: 'new',
category: 'Ticket',
description: 'Creates a new ticket.',
aliases: ['newticket'],
usage: 'New',
userperms: [],
botperms: [],
run: async (client, message, args) => {
if(message.author.bot) return;
if(!message.channel.id == process.env.COMMAND_T) return;
if(!client.userTickets) {
client.userTickets = new Map();
const channels = message.guild.channels.cache.filter(channel => {
if(channel) return channel.name.startsWith('t-');
});
if(channels) {
for (i in Array.from(channels)) {
client.userTickets.set(i, +i + 1);
}
}
}
console.log(client.userTickets)
if(client.userTickets.has(message.author.id)) {
return message.channel.send('<#' + message.author.id + 'You already have a ticket, please close it then run this command again!').then(m => m.delete({timeout: 10000}));
}
message.guild.channels.create(`t-${client.userTickets.size + 1}`, {
permissionOverwrites: [
{
id: message.author.id,
allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
},
{
id: process.env.ROLE_STAFF,
allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
},
{
id: process.env.ROLE_MEMBER,
deny: ['VIEW_CHANNEL']
}
],
type: 'text',
}).then(async channel => {
channel.setParent(process.env.TICKET_C);
client.userTickets.set(message.author.id, client.userTickets.size + 1);
message.channel.send(`<#` + message.author.id + `>, Done, go to your ticket! ${channel}`).then(m => m.delete({timeout: 10000}));
client.users.cache.get(message.author.id).send(`Your ticket has been opened, go take a look: ${channel}`)
channel.send(`Hi <#` + message.author.id + `>, Hello and welcome to your DwaCraft ticket!`);
channel.send(`https://tenor.com/view/is-there-something-i-can-help-you-with-dan-levy-david-david-rose-schitts-creek-gif-20776045`);
const logchannel = message.guild.channels.cache.find(channel => channel.id === process.env.TICKET_L);
if(logchannel) {
logchannel.send(`There was a new ticket created by <#${message.author.id}>! Channel: <#${channel.id}>`);
}
});
}
}
Note: I've researched but I didn't find any answer.
Thanks for taking the time to read this.
Try this
message.guild.channels.create(`t-${client.userTickets.size + 1}`, {
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: process.env.ROLE_STAFF,
allow: ['VIEW_CHANNEL'],
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
],
parent: process.env.CATEGORY
});
This code is mine, but it is shortened, if you like the complete one send me DM
P.S I just have it for my ticket system
I have been using an online guide to set up a currency bot but when I use the command to add items to the shop the terminal throws out this error and while the bot says the item has been added when I check the shop it has nothing in it. this is the exact error that i get I'm new to coding and a have little to no idea what it means any help or point towards a guide that might explain it would be greatly appreciated
DiscordAPIError: Unknown interaction
at RequestHandler.execute (c:\Users\danie\Desktop\CurrencyBot\node_modules\discord.js\src\rest\RequestHandler.js:349:13)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (c:\Users\danie\Desktop\CurrencyBot\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
at async CommandInteraction.reply (c:\Users\danie\Desktop\CurrencyBot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:99:5)
====================
Here is the code for the command
const Discord = require('discord.js')
const CurrencySystem = require("currency-system");
const cs = new CurrencySystem;
exports.run = async (client, message, args) => {
await message.deferReply();
if (message.options.getInteger('price') < 1) return message.editReply("You can't add an item for less than 1$!");
let result = await cs.addItem({
guild: message.guild,
inventory: {
name: message.options.getString('name'),
price: message.options.getInteger('price'),
description: message.options.getString('description') || 'No Description'
}
});
if (result.error) {
if (result.type == 'No-Inventory-Name') return message.editReply('There was a error, Please enter item name to add.!')
if (result.type == 'Invalid-Inventory-Price') return message.editReply('There was a error, invalid price!')
if (result.type == 'No-Inventory-Price') return message.editReply('There was a error, You didnt specify price!')
if (result.type == 'No-Inventory') return message.editReply('There was a error, No data recieved!')
} else return message.editReply('Done! Successfully added `' + message.options.getString('name') + '` to the shop!')
}
exports.help = {
name: "additem",
data: {
name: 'additem',
description: "A way to additem to shop",
options: [{
name: 'name',
type: 'STRING',
description: 'Name of Item.',
required: true,
}, {
name: 'price',
type: 'INTEGER',
description: 'Price of item',
required: true,
},
{
name: 'description',
type: 'STRING',
description: 'Description of the item. (Can\'t be Changed later.)',
required: false,
}
]
}
};
exports.conf = {
aliases: [],
cooldown: 5 // This number is a seconds, not a milliseconds.
// 1 = 1 seconds.
}