Discord.py suggestion command - discord.py

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

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

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

What's a reasonable way to read an entire text file as a single string?

I am sure this is an easy one; I just couldn't find the answer immediately from Google.
I know I could do this (right?):
text = ""
File.open(path).each_line do |line|
text += line
end
# Do something with text
But that seems a bit excessive, doesn't it? Or is that the way one would do it in Ruby?
IO.read() is what you're looking for.
File is a subclass of IO, so you may as well just use:
text = File.read(path)
Can't get more intuitive than that.
What about IO.read()?
Edit: IO.read(), as an added bonus, closes the file for you.
First result I found when searching.
I wanted to change the mode, which doesn't seem possible with IO.read, unless I'm wrong?
Anyway, you can do this:
data = File.open(path,'rb',&:read)
It's also good for when you want to use any of the other options:
https://ruby-doc.org/core/IO.html#method-c-new

Resources