How to make a discord command with arguments? - image

I'm new to the coding and stuff and I want to know if there are any tutorials or pages where I can solve this:
I want to make a discord command that looks like this !acz map Lasius flavus where: !acz is the prefix, map is the command and Lasius flavus are two arguments.
Bot responds with message that is a link with the two arguments, that should look like this: https://antmap.coc.tools/images/Lasius.flavus.png
that means that we get a reply on discord that is a picture.
I want to be able to type in my discord !acz map Genus species - and bot responds with https://antmap.coc.tools/images/Genus.species.png where the first letter of Genus is capital, just like in the example.
I'm adding a picture with what I have in mind, where ? is prefix, map is command and Lasius flavus is Genus species
(no, I already asked for the code)
Thanks for your help!

#bot.command(brief="Za map napiš Rod a druh a vyskočí ti mapa výskytu!")
async def map(ctx, *args):
await ctx.channel.send('https://antmap.coc.tools/images/{}.png '.format('.'.join(args)))
this was the way and reading through https://www.programiz.com/python-programming/methods/string/replace helped me! eve tho I didn't use the replace()! I just had to change a few pieces in a code that sent noarguments and arguments:
#bot.command()
async def map(ctx, *args):
await ctx.channel.send('{} arguments: {}'.format(len(args), ', '.join(args)))

Related

How to use options in discord.py?

I'm working on a discord bot that can send random memes, and I wanted to add a command for continuous feed according to a the amount of memes the user wants, for example /feed number:15, would send 15 memes.
The problem is how to make the option input work on the command, I've find some videos that helped me a little, but due to the other commands being híbrido commands, I can figure out how to make this one working.
This is what's I have so far:
#bot.hybrid_command(name = "feed", with_app_command = True, description = "Feeds a specific number of memes")
#app_commands.guilds(discord.Object(id = guild_id))
async def test1(interaction: discord.Interaction, number: int):
await interaction.response.send_message("It's working!")
The await line, obviously won't do any feed this way, I know, I did the feed part separated and it works, but I just did it this way for testing, so if I type the command and enter a number, it should return me "It's working!", But that never happens, I suppose it's maybe something to do with híbrido commands, but I really don't know, what am I missing?
I don't think the input option is the problem, because I see nothing wrong with it, but the thing that is surely wrong is that you're naming the first argument of the hybrid command interaction, which is wrong and can create confusion; the actual argument it should be is context or ctx. And that object is a discord.ext.commands.Context object.
You should rename it to ctx to avoid confusion.
#bot.hybrid_command(name = "feed", with_app_command = True, description = "Feeds a specific number of memes")
#app_commands.guilds(discord.Object(id = guild_id))
async def test1(ctx: commands.Context, number: int):
await ctx.send("It's working!")
You may want to see the official example here

Separating index values from an array inside discord embed

I'm looking for a way to spit out an embed that lists all the "suggestions" in my database with their corresponding index values like this. So far I can send an embed with all of the information like this but that just adds the information as 2 strings after each other (suggestions then the index values).
I need a way to display the "suggestions" in the embed with the index values separately like this:
[#1] suggestion 1
[#2] suggestion 2
[#3] suggestion 3
[#4] suggestion 4
Ideally this would all be done within one embed field.
I'm sorry if this was poorly worded but if I knew how to describe what I'm looking for, I might have found the answer sooner. Thanks for any advice in advance.
(I'm using the replit database - if any more of the code is needed please let me know)
#client.command()
async def suggestions(ctx):
suggestions = []
if "suggestions" in db.keys():
suggestions = db["suggestions"]
joined = '\n'.join([str(elem) for elem in suggestions])
index_number = '\n'.join([str(i) for i in range(len(suggestions))])
channel = client.get_channel(0000000000000)
suggestionsEmbed=discord.Embed(title="Server Suggestions", description="Vote for suggestions with **$vote #** Remove suggestions with **$del #**", color=0x15e538)
suggestionsEmbed.set_thumbnail(url="https://www.startup.pk/wp-content/uploads/2020/07/ideaideabulblightbulbicon-1320144733751939202.png")
suggestionsEmbed.add_field(name='Suggestions Created', value=f'{joined} **#{index_number}**')
await channel.send(embed=suggestionsEmbed)

Adding Emojis to server with discord.py

I wanted to create a website for discord emojis, and I got an idea to make the bot put emoji on the server. Now I did not find any help on youtube so I am here to ask. Hope I get some help
With the vague details you said, I've come up with this
#client.command()
async def add_emoji(ctx)
emoji_path = "" # put your desired file path to get the emoji from
with open(emoji_path, "rb") as f:
await ctx.guild.create_custom_emoji(name="emoji_name", image=f.read())
This opens the emoji in both read mode and binary mode using 'rb'. So it can read the raw bytes of the image since Guild.create_custom_emoji only takes bytes and then passes that raw bytes and a name to uild.create_custom_emoji

discord.py - how to make a command that takes many words with spaces as one argument

I'd like to make a command like this:
async def offer(self,ctx,firstName=None,lastName=None,pitch=None):
and I want pitch to take every character including spaces after the lastName argument.
An example: I want !offer Ocean Man Hi, join my team! to assign "Ocean" to firstName, "Man" to lastName, and "Hi, join my team!" to pitch. I have been thinking this over for days now with no progress, and I can't seem to google the right thing as I usually do when I get stuck. Any help is appreciated!
You can add * like this: async def offer(self,ctx,firstName=None,lastName=None, *, pitch=None):. This will work for you.

Discord.py suggestion command

I hae looked everywhere and haven't found the answer. I'm pretty new to discord.py. I want to make a /suggestion command and whatever comes after it gets stored into a JSON file.
Maybe this is something that you are looking for?
#bot.command()
async def suggest(msg,*,user_suggestion):
file=open(r"E:\Test12\Testing\hello.json",'r')
data=json.load(file)
data.append(user_suggestion)
with open(r"E:\Test12\Testing\hello.json",'w')as f:
f.write(str(json.dumps(data)))
f.close()
await msg.message.add_reaction(emoji='\U00002705')

Resources