Replacing argument with hard-coded link in a command to play music - discord.js

I'm having trouble trying to get a case statement to work, and since this is a specific question I can't find any search terms broad enough to get any kinds of good results.
I'm trying to get my discord bot to respond to a command "countdown" by playing a specific youtube video in a voice channel.
I've never done music or voice channel work in discord bots before, so I watched a tutorial, but it showed how to use a command like "play" followed by an argument that is a youtube video link. I want the command to not have any args and play a specific youtube video each time that 'countdown' command is used.
Here is what I have now, which requires an argument after the command is used.
case "countdown":
if (!message.member.voiceChannel) {
message.channel.sendMessage("You must be in a voice channel to initiate a countdown.");
return;
}
if (!servers[message.guild.id]) servers[message.guild.id] = {
queue: []
};
var server = servers[message.guild.id];
server.queue.push(args[1]);
if (!message.guild.voiceConnection) message.member.voiceChannel.join().then(function(connection) {
play(connection, message);
})
break;
This is good for when I want the bot to play any music desired, but I just want one video associated with this command. Any help would be appreciated, thanks.

At the moment you're using an argument as the URL for the video. Instead of using args[1] you could just type your link (like 'https://www.youtube.com/watch?v=G1IbRujko-A') and ignore the argument.
Keep in mind that if you're checking the arguments before this command (to say stuff like "Hey, you need this argument to execute the command!") you should ignore this one since you won't be using any argument.
Hope this can help you :)

Related

Need custom command options for discord bot without using slash commands

Can I attach slash command options to a custom command like "?help" and not "/help"?
I’m not entirely sure that I know what you mean, but I’ll try to answer.
?help is something we call a message command. It’s basically a message and is received as a messageCreate event. The bot then filters all message events to only respond to those with the specified prefix (in this case “?”). The only thing a user can submit via a message is the message content, a string, and attachments, which are files. You can access that via .content. You can make the user submit arguments by typing space and then e. g. writing a category name (?help moderation) which you could then recognise with <Message>.content.split(“ “);.
/help is, when registered as such, a slash command (interaction). When received, the interactionCreate event is called. When you register a slash command, you can add options such as string, integer, member and so on. These are registered beforehand and only in because of that, discord knows what options to give the user. And yeah, after that, you can access these options via the interaction.
To sum up (if I understand you correctly), you can’t attach (slash command) options to message commands because they are handled differently by discord. You can, however, utilise spaces to use these options. (Slash Commands are not using spaces but rely only on these options.)
I hope this helps.

Clickable command in text Discord

I saw a YouTube video that shows you a way to display a "slash command" on Discord (see Image 1)
by sending "</any text:0>" (not clickable). I tried with any number at the end and its still working. So I tested </a slash command in my bot:MY BOT ID> and it was still working but it is not clickable.
Does anyone know a way to make a clickable slash command like in verified Discord bot profiles?
I am not sure it's possible but if someone knows how to do it.
Thanx in advance.
You can't control the 'Try my commands' section from appearing on your bot's profile. Discord will generate that section for your bot after a while of your bot being online & used.
In order to put clickable slash command buttons as you've shown you need to use the application command ID, not the bot id. You can find that by clicking on the prompt:
You can then take that ID to form </fast:972289487818334212>, which will be a clickable form of slash command.
Please note that slash command ids are changed randomly and frequently on Discord's end.
For sub-commands, you can do as such:
</cool-bot settings:2046185065514240342>
The command ID can be found under message.data.id, when your server receives a command.

How do I pull a random gif from tenor in discord.py?

I'm doing a discord bot using discord.py and one of the functions I want to add is on a command !gif it sends a random gif from tenor. Is it possible to do that? So basically what I need is an explanation as to how to fetch GIFs from tenor.
You would need to query Tenors random gif api:
https://tenor.com/gifapi/documentation#endpoints-random
This requires you to provide a search term and will return a randomised list of gif for that keyword.
You can then respond to the command with either a link to the gif or the gif itself as a file.

Discord JS - Downloading an image from attachment

Basic thing I just need fixed; wanting to download a file from attachment but i'm not sure how to describe the attachment as an object/file.
I just have this:
let jimage = message.attachments.first()
fs.writeFileSync(`./${jimage.filename}`, jimage)
Only issue is that, for FS's second argument, discord doesn't have a selection for just the file. Reason i'm trying this way is because I took this idea from here:
how to save an image from discord using discord bot node js
But "attachment.file" isn't a real thing. Other threads show just really overly complicated methods with new calls, like something called request, I just wanna do this straight off fs and discord.
I found a pretty simple solution using this line over here:
request(message.attachments.first().url).pipe(fs.createWriteStream('image.png'))
It basically finds where file is stored on the discord website and using "request" gets it from there. To keep file's name you can use message.attachments.first.name if you need to. Unfortunately I haven't found out how to save file type. If any questions, I will try to help!
P.S. don't forget to define and download the "request" module.

Sub commands in discord.py?

I’m writing a simple bot for a discord server, and I’m trying to explore a few different ideas and two of them require some kind of response and sub command based on responses. I want people to be able to poll something without it being done through reacting to a bots post. I’m unsure of how to go about this.
I’ll give one as an example.
Ideally, it could be structured something like this.
[initiate poll command] [poll name]
Then to respond, a user could do something like this:
[poll name] [yes/no]
Or for a pseudo-code example:
!start_poll Lets_do_this
!lets_do_this yes
Then if it meets a certain threshold of yes’s, a sub command would execute.
My idea would be to create a dictionary. Whenever someone starts a poll, you would add two keys to the dictionary: "[name]_yes", and "[name]_no". Each key would have an empty list. I would then use a command such as "!vote [poll] [yes/no]" to simplify the coding, and whenever somebody voted, I would add their discord ID to the corresponding list. It would be a good idea to check if their name is already on a list, however. Then if you wanted, you could take the size of each list to see how many people have voted for each. The dictionary would end up looking something like:
{
'lets_do_this_yes': ['Bob', 'Suzy'],
'lets_do_this_no': ['Dan'],
'lets_do_this_instead_yes': ['Dan','Joe'],
'lets_do_this_instead_no': ['Suzy']
}
Note: I haven't used discord.py, but I have experience with Python and Discord.JS
Hopefully this works for you.
Edit: The result example above would not actually have their Discord name, but rather their Discord ID, as that is unique to them where as their name can be changed.

Resources