Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Write a python discord bot that using / commands will send pictures from the imgur api,…

  1. User

    Write a python discord bot that using / commands will send pictures from the imgur api, you have the bomb option if true 10 pictures wil be send and the type option which shouds send that specific type of chair if no type is selected it wil just send a regelur chair. I already made the commands:

    import requests

    url = "https://discord.com/api/v10/applications/{bot id}/commands"

    json_data = { "name": "chair", "type": 1, "description": "Get a chair image", "options": [ { "name": "bomb", "description": "Bomb option", "type": 5, "required": False }, { "name": "type", "description": "Type of chair", "type": 3, "required": False, "choices": [ { "name": "massage chair", "value": "massage chair" }, { "name": "kitchen chair", "value": "kitchen chair" }, { "name": "comfy chair", "value": "comfy chair" } ] } ] }

    For authorization, you can use either your bot token

    headers = { "Authorization": "Bot {bot token}" }

    or a client credentials token for your app with the applications.commands.update scope

    headers = {

    "Authorization": "Bearer <my_credentials_token>"

    }

  2. Assistant

    To implement the desired functionality, you'll need to use Discord's Python packages, such as discord.py and the command extension library discord-py-slash-command. First, you need to install both packages. You can install them using pip:

    pip install discord-py
    pip install discord-py-slash-command
    

    Then, you can implement the bot like this:

    import os
    import discord
    from discord_slash import SlashCommand
    from discord_slash.utils.manage_commands import create_option
    from imgurpython import ImgurClient
    
    # Set your bot token and Imgur API credentials here
    DISCORD_BOT_TOKEN = 'your_bot_token'
    IMGUR_CLIENT_ID = 'your_client_id'
    IMGUR_CLIENT_SECRET = 'your_client_secret'
    
    client = discord.Client(intents=discord.Intents.default())
    slash = SlashCommand(client, sync_commands=True)
    
    imgur_client = ImgurClient(IMGUR_CLIENT_ID, IMGUR_CLIENT_SECRET)
    
    @client.event
    async def on_ready():
        print(f'We have logged in as {client.user}')
    
    @slash.slash(
        name='chair',
        description='Get a chair image',
        options=[
            create_option(
                name='bomb',
                description='Toggle bomb option',
                option_type=5,
                required=False
            ),
            create_option(
                name='type',
                description='Type of chair',
                option_type=3,
                required=False,
                choices=[
                    {'name': 'massage chair', 'value': 'massage chair'},
                    {'name': 'kitchen chair', 'value': 'kitchen chair'},
                    {'name': 'comfy chair', 'value': 'comfy chair'}
                ]
            )
        ]
    )
    async def chair(ctx, bomb: bool = False, _type: str = 'regular chair'):
        search_query = f"{_type} chair"
    
        items = imgur_client.gallery_search(q=search_query, sort='top')
        filter_images = list(filter(lambda x: x.is_album == False, items))
    
        if bomb:
            images_to_send = filter_images[:10]
        else:
            images_to_send = filter_images[:1]
    
        for image in images_to_send:
            await ctx.send(content=f"{search_query.capitalize()} Image", embed=discord.Embed().set_image(url=image.link))
    
    client.run(DISCORD_BOT_TOKEN)
    

    Note: This code assumes you have valid Imgur API credentials (IMGUR_CLIENT_ID and IMGUR_CLIENT_SECRET) and a valid Discord bot token (DISCORD_BOT_TOKEN). Replace them with your own credentials.

    This bot will work with your already created slash commands and send the specified type of chair image. If the bomb option is True, it will send 10 images.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.