How to use options in discord.py? - 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

Related

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)

Discord.py Question with weird betting winnings

As the title suggests, I have some trouble with the titles of my betting command. Sometimes it doesn't show up, while at other times it shows up as something with a lot of 1's and 0's. I think it might be binary, but I don't know.
Code:
https://replit.com/#jsun3/Betting-cmd#main.py
Stacks was being annoying on formatting so I did it in replit.
Ceres' answer helped, but caused another error. When I won sometimes it counted -10 won(meaning i lost 10).
IMG:
New update for code:
https://replit.com/#jsun3/Betting-cmd#main.py
The problem is that amount is a string, and in python you can multiply a string to repeat it. That is exactly what is happening.
Solve it with
#client.command(aliases=["gamble"]) # ASSUME I HAVE CLIENT DEFINED BC IN MY REAL CODE IT IS DEFINED.
async def bet(ctx, amount: int = None):
Since discord.py implements type-converters for arguments, your amount is converted to an integer, and will be multiplied like one.

How to make a discord command with arguments?

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)))

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')

What is the best way to get keyboard events (input without press 'enter') in a Ruby console application?

I've been looking for this answer in the internet for a while and have found other people asking the same thing, even here. So this post will be a presentation of my case and a response to the "solutions" that I have found.
I am such new in Ruby, but for learning purposes I decided to create a gem, here.
I am trying to implement a keyboard navigation to this program, that will allow the user use short-cuts to select what kind of request he want to see. And in the future, arrow navigations, etc.
My problem: I can't find a consistent way to get the keyboard events from the user's console with Ruby.
Solutions that I have tried:
Highline gem: Seems do not support this feature anymore. Anyway it uses the STDIN, keep reading.
STDIN.getch: I need to run it in a parallel loop, because at the same time that the user can use a short-cut, more data can be created and the program needs to show it. And well, I display formated text in the console, (Rails log). When this loop is running, my text lost the all the format.
Curses: Cool but I need to set position(x,y) to display my text every time? It will get confusing.
Here is where I am trying to do it.
You may note that I am using "stty -raw echo" (turns raw off) before show my text and "stty raw -echo" (turns raw on) after. That keeps my text formated.
But my key listener loop is not working. I mean, It works in sometimes but is not consistent. If a press a key twice it don't work anymore and sometimes it stops alone too.
Let me put one part of the code here:
def run
# Two loops run in parallel using Threads.
# stream_log loops like a normal stream in the file, but it also parser the text.
# break it into requests and store in #requests_queue.
# stream_parsed_log stream inside the #requests_queue and shows it in the screen.
#requests_queue = Queue.new
#all_requests = Array.new
# It's not working yet.
Thread.new { listen_keyboard }
Thread.new { stream_log }
stream_parsed_log
end
def listen_keyboard
# not finished
loop do
char = STDIN.getch
case char
when 'q'
puts "Exiting."
exit
when 'a'
#types_to_show = ['GET', 'POST', 'PUT', 'DELETE', 'ASSET']
requests_to_show = filter_to_show(#all_requests)
command = true
when 'p'
#types_to_show = ['POST']
requests_to_show = filter_to_show(#all_requests)
command = true
end
clear_screen if command
#requests_queue += requests_to_show if command
command = false
end
end
I need a light in my path, what should I do?
That one was my mistake.
It's just a logic error in another part of code that was running in another thread so the ruby don't shows the error by default. I used ruby -d and realized what was wrong. This mistake was messing my keyboard input.
So now it's fixed and I am using STDIN.getch with no problem.
I just turn the raw mode off before show any string. And everything is ok.
You can check here, or in the gem itself.
That's it.

Resources