Adding Emojis to server with discord.py - 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

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

How I can load a font file with PIL.ImageFont.truetype in replit

I'm trying to make this welcome card thingy in discord.py and I'm running my bot on replit.com as of now.
font = ImageFont.truetype("arial.ttf", 28)
I got some examples and it worked great as long as I'm running it on my PC but wen I get to replit.com it gives the error saying
Command raised an exception: OSError: cannot open resource
How should I go about correcting this?
I don't know discord or replit but presume the issue is that you can't upload your binary font file.
If so, you have a couple of options:
find your desired font online somewhere and use requests.get(URL) to grab it in your code on replit, or
make a base64 variable in your code and decode it
The first option is covered here.
Let's look at the second. Say your font is called /Fonts/funky.ttf on your PC. Now you want that in base64 which you can do with a commandline tool on your local PC:
base64 < /Fonts/funky.txt
That will make a long string of characters. Copy it, and in your Python code add a string called font64 and set it equal to the pasted string, i.e.
font64 = 'PASTED STRING'
Now in your code you can convert the string back from base64 to binary, then wrap it in a BytesIO to make it look like a file and load it:
import base64
import io
from PIL import ImageFont
font64 = 'PASTED STRING'
# decode from base64 to binary
binary = base64.b64decode(font64)
# wrap in BytesIO to make file-like object
FileLike = io.BytesIO(binary)
# load font
font = ImageFont.truetype(FileLike, 28)

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

Ignore formatting in Slack App when reading messages

I'm building a Slack App which is only interested in actual user input in message, regardless if it's bold, italic, or a web link.
What would be the best way to remove all the formatting characters, such as _, *, etc, and leave actual text only?
The best solution is actually to ignore the text param entirely, as it is inherently lossy, and to instead parse what you want from the blocks param, which gives an exact representation of what the user saw in their slack client message field when they posted their message.
To demonstrate this, here's a simple example
what you type
WYS
text
blocks[0]["elements"]["elements"]
parsing text from blocks
[a][space][*][b][*]
​a b
a *b*
[{"type":"text","text":"a "},{"type":"text","text":"b","style":{"bold":true}}]
a b
[a][*][b][*][←][←][←][space]
a *b*
a *b*
[{"type":"text","text":"a *b*"}]
a *b*
Unfortunately for now, the Slack API does not give you the blocks param in command events, only message events. Hopefully they will fix this, but until then you may wish to use regular messages instead of slash commands.
If you are using node.js there is a module for this:
https://openbase.io/js/remove-markdown/documentation
Note that this is for markdown and not Slack's variant mrkdwn so it might not work.
Managed to get the unformatted message in python by looping through the array.
def semakitutu(message, say):
user = message['user']
# print(message)
hasira = message['blocks']
hasira2 = hasira[0]['elements'][0]['elements']
urefu = len(hasira2)
swali = ''
for x in range(urefu):
swali=swali+hasira2[x]['text']
print(swali)
say(f"Hello <#{user}>! I am running your request \n {swali}")```

How to extract the values from a string from slack ruby bot

I am trying to create a slackbot which can save incoming data to a database.
I have used the gem slack-ruby-bot and I'm able to receive my text, but I want to extract specific parts of the text, to save in a database.
Given the following received text:
I worked on 'stackoverflow' for 10 hours.
I must be able to extract the project name, stackoverflow, and the hour, which is 10. Is there someway to do it?
command 'activity', '/^Activity/' do |client, data, match|
client.say(text: "#{data.text}", channel: data.channel)
end
This is just a sample code which gets the command activity.
Wherever you receive the string, you might use the regular expression and String#scan to get the data.
"I worked on 'stackoverflow' for 10 hours.".scan /'[^']+'|\d+/
#⇒ ["'stackoverflow'", "10"]

Resources