Mon Jun 05 2023
Adding a Lewd Image Feature to a Discord Bot
Adding a Lewd Image Feature to a Discord Bot
Last post we built the most basic bot possible. Now it's time to give it some features.
And the most important feature for any chat bot is, obviously, lewd pics!
Our requirement: run a single /涩图 command and get back a lewd image, with a few optional parameters.
Calling the API
I'm using the lolicon API here (big thanks to the API author).
The API takes several parameters. I'm exposing three to the user: r18 tags exclude_AI, sent as a POST request.
First, a bare-bones request function
def RequestLolicon(tags: list[str], r18: bool, excludeAI: bool) -> dict:
This defines a basic function for requesting image data: three parameters in, a result out.
Downloading the image
I want the image downloaded locally first, then uploaded to Discord's CDN, so I need a download function.
Let's write a simple image download function.
def Download(pid: str, url: str) -> str:
return f'{pid}.{ext}'
This function takes two parameters, pid and url.
url comes from the result of the API call above. Since this is just a utility function, it doesn't need to care about the data the API returns.
pid also comes from that result, and is used for the filename.
The function downloads the image to ./cache/setu/{pid}.{ext}, where ext is determined by the suffix of the download url.
Putting it together
Next, wire these two functions together and hook them up to Discord.
async def HandleRequest(ctx: SlashContext, args, r18, excludeAI):
os.remove(f'./cache/setu/{filename}')
This takes a SlashContext, which the Python library discord-py-interactions hands us — it's the command's context.
These four parameters are the ones we pass in where we register the command.
Roughly put, this function does it all: send the request, download the image, upload the image, send the message, and delete the local file.
Registering the Discord slash command
Now let's register the Discord slash command.
scopes = [XXX]
There are a few things to explain here:
-
scopes: the command's scope, an array holding the IDs of the servers where you want the command enabled.
If you don't set this, Discord defaults to enabling it in all servers, but the command takes about an hour to take effect. If you do set it, it registers instantly.
If you set the correct server ID and it still doesn't register, chances are you haven't granted the permissions.
To get the server ID:
turn on Developer Mode->right-click the server name->Copy Server ID. Here's how:

-
Registering a Slash Command
@interactions.slash_command( )The decorator that registers a basic command.
-
Command parameters: Slash Option
@interactions.slash_option( )nameis the parameter's name,descriptionis the short description.requiredsays whether the parameter is mandatory. If it isn't, you must give it a default value in the function's parameter list, liketags = ""in the code below.opt_typeis the parameter's type, taken from theinteractions.OptionTypeenum. -
Inside the function:
await ctx.defer() await Setu.HandleRequest(ctx, tags, r18, exclude_ai)await ctx.defer()makes the bot show aresponding...message. You usually need this for time-consuming operations, and downloading images takes a while, so I added it.
Testing
If everything above went smoothly, the feature should work. Here's the result:

