Distube remove command - discord.js

I want to make a command so that it removes a song from the queue. I am using discord.js v13 and distube. I get this error
TypeError: queue.splice is not a function
And here is the code for the commad:
else if (cmd == "remove" || cmd == "del"){
queue = distube.getQueue
const index = parseInt(args[0]) - 1
queue.splice(index , 1)
message.channel.send(`Removed track number ${index}`)
}

you are not accessing the song list, use queue.songs to access it
https://distube.js.org/#/docs/DisTube/stable/class/Queue?scrollTo=songs

Related

How can I get a variable into a argument?

I have the following code :
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
var number = command.length - 1;
var times = args[number ***Here I want the last position of the user input***];
If you write an command in a text channel, the last argument from the user input should get into the times variable. Here's one example: the user writes '!test some random text 3' and the last argument (in this case 3) should get into a varable. Hope you can help me
You can use args.length - 1 so in your case var times = args[args.length - 1]

How to make a discord.js bot repeat a user's message without including the prefix and command name?

Okay this most likely has a really simple answer but I couldn't find the same thing online and I can't figure it out on my own thinking.
So in the args for responding to a prefix, i have this:
case 'say':
const usermsg = message.content
message.channel.send(usermsg)
break;
Since it's the entire contents, it responds with the c!say too then it triggers itself. But the triggering isn't the issue, I want the c!say not included in the message. (I know I don't need the const for this, I just wanted to experiment different combinations of stuff in a separate line)
Update:
So I found a second method of approaching this by using the arguments part, like this:
case 'say':
message.channel.send(args[1])
message.delete(1)
break;
So this does what I want but only for the second argument, meaning it doesn't work for more than 1 word. So my current thought is subtracting args[0] (the c!say phrase) from message.content.
Found a fix to the issue, instead of sending the arguments or subtracting text I used the replace command.
case 'say':
let saymsg = message.content
message.channel.send(saymsg.replace("c!say",""))
message.delete(1)
break;
I can't believe I didn't remember the world replace sooner, but that's it, it works.
case 'say':
const usermsg = message.content.split(' ');
message.channel.send(usermsg.slice(0).join(' '));
break;
This code will take in the full user string, slice out the first argument (the command and prefix), then join the rest of the message and send it in chat.
bot.on('message', message => {
var sender = message.author;
var msg = message.content.toUpperCase();
var prefix = '>';
var cont = message.content.slice(prefix.length).split(" ");
var args = cont.slice(1);
for (x = 0; x < profanities.length; x++) {
if (message.content.toUpperCase == profanities[x].toUpperCase()) {
message.channel.send('Hey! Don\'t say that');
message.delete();
return;
}
if (msg.startsWith(prefix + 'SAY')) {
message.delete();
var saymsg = message.content;
message.channel.send(saymsg.replace(prefix + 'say', ''))
}
}
}

Detecting Guild ID

I want to make certain commands so they can only be used in a specific guild.
I've tried an if statement that checks if message.guild.id = the guild id, but the command still will execute in any guild, perhaps I'm doing something wrong.
client.on("message", msg => {
var bcid = "585676550544949248"
if (msg.guild.id = bcid) {
if (msg.content.toLowerCase().startsWith(prefix + "ping")) {
msg.channel.send("Pong! :ping_pong:");
}
}
});
One = symbol is used for assigning a value to a variable, two == or three = symbols are used for checking if a value equals another value. In if statements you use the two or thee equals symbol operator.
Thus to fix your problem, change the if to:
if (msg.guild.id === bcid) {
// Your code here
}
Check out this link to learn about the difference between == and ===.

Deleting my messages in a server using bulkDelete (discord.js)

I am new to discord.js but learned that I can delete my messages using bulkDelete and it will delete them all, even if they are older than 2 weeks. I clear my messages in a server I moderate manually once a month and needless to say it takes forever. I was wondering if anyone would be able to help me make a command that will do this automatically whenever I call it?
Thanks,
K
I would set up a recursive function that checks if there are messages in the channel (100 max every time): if there are no messages it stops otherwise it deletes them and restarts.
function clean(channel, limit = 100) {
return channel.fetchMessages({limit}).then(async collected => {
let mine = collected.filter(m => m.author.id == 'your_id_here'); // this gets only your messages
if (mine.size > 0) {
await channel.bulkDelete(mine, true);
clean(channel);
} else channel.send("The channel is now empty!").delete(5000); // this message is deleted after 5 s
});
}
You can adapt this idea to your existing command parser or, if you don't know how to implement this, try:
client.on('message', msg => {
if (msg.author.bot || msg.author != YOU) return;
// with YOU i mean your User object, to check permissions
let command = 'clean', // the name of your command
args = msg.content.split(' ');
if (args[0].toLowerCase() == command)
clean(msg.channel, !isNaN(args[1]) ? args[1] : undefined); //<-- THIS is how to use the function
// used a ternary operator to check if the other arg is a number
});
This is just a very basic implementation, there are a lot of better ways to detect commands.
I've just found a way to filter messages.
You can fetch messages and then check for each message if its yours
await message.channel.fetchMessages({
limit: 100
}).then((msgCollection) => {
msgCollection.forEach((msg) => {
if(msg.author.id == message.author.id) {
msg.delete();
}
})
});

Scala: load different files by input

My program has a lot of different functions and one of these is the command "load".
As soon as the user types as input "load" he can load in a txt file...
The problem is, that my command is not only the "load" word itself, its for example "load numbers.txt" or "load data.txt"
Now i want to open these textfiles which are located on my PC but i need the name of the files WITHOUT the "load" in front of the command. How can I fetch only the name from the entire input line?
def ProgramSelector() {
var endProgram = false
while (!endProgram) {
val userSelection = scala.io.StdIn.readLine("There is no transfer data available yet, please use the 'load' command to initialize the application!\nEnter your command or type 'help' for more information:")
if (userSelection == "help")
println("some help text here")
else if (userSelection == "load")
//else if (userSelection == "3")
//exerciseThree()
//else if (userSelection == "4")
//exerciseFour()
//else if (userSelection == "5")
//exerciseFive()
//else if (userSelection == "6")
//exerciseSix()
//else if (userSelection == "7")
//exerciseSeven()
//else if (userSelection == "8")
//exerciseEight()
else if (userSelection == "exit")
endProgram = true
else
println("Invalid command!")
So I have my function the ProgramSelector where I only make an if statement if the input is load...
I tried to make this a bit more generic.
To show how this can be helpful, I also created another command that you can call as "add 1 2" and it will print the sum of adding the two integers.
If you are serious into making a CLI interactive application, I suggest you take a look here on how to make your own interactive shell on top of sbt.
val loadCommand = """load (.*)""".r
val helpCommand = """help.*""".r
val exitCommand = """exit.*""".r
val addCommand = """add\s+(\d+)\s+(\d+)""".r
val PromptMsg = "There is no transfer data available yet, please use the 'load' command to initialize the application!\nEnter your command or type 'help' for more information: "
def programSelector() {
var endProgram = false
val fileKeeper = new scala.collection.mutable.HashSet[String]()
while (!endProgram) {
val userSelection = scala.io.StdIn.readLine(PromptMsg)
userSelection match {
case loadCommand(file) =>
println(s"Adding file $file")
fileKeeper add file
println(s"Files so far: $fileKeeper")
case helpCommand() =>
println("some help text here")
case exitCommand() =>
endProgram = true
case addCommand(a,b) =>
val sum = a.toInt + b.toInt
println(s"Sum=$sum")
case _ =>
println("Invalid command!")
}
}
}
programSelector()

Resources