Skip to main content

A package for making a Discord ChatGPT bot

Project description

DiscordGPT

This is a package to use ChatGPT in Discord.

Installation

You can download the source here, or use pip to install the package:

pip install discordgpt

Usage, examples

You can use the DiscordGPT class to make a ChatGPT bot:

from discordgpt import DiscordGPT

DiscordGPT(token="your bot token", api_key="your openai api key", channel_id="a channel id for the chat")

This creates a ChatGPT bot that responds when someone sends a message in the specified channel id.

You can customize it further by creating a class with DiscordGPT as its superclass:

from discordgpt import DiscordGPT

class MyDiscordGPT(DiscordGPT):
      def __init__(self): super().__init__(token="bot token", api_key="openai api key", channel_id="a channel id", <more options if you need>)
      async def new_question(self, message):
        # you can really do anything here, a bad word checker is just an example
        if "a bad word" in message.content: return await message.channel.send("Don't say bad words to ChatGPT!")
        await super().new_question(message)

MyDiscordGPT()

This will create a bad word blocker for DiscordGPT. If you want to customize it even further, you can overwrite the whole process:

from discordgpt import DiscordGPT
import discord
class MyDiscordGPT(DiscordGPT):
      def __init__(self): super().__init__(token="bot token", api_key="openai api key", channel_id="a channel id", <more options if you need>)
      async def new_question(self, message):
        response = super().get_response(message) # gets ChatGPT's response
        await message.channel.send(embed=discord.Embed(title="ChatGPT said:", description=response))

MyDiscordGPT()

That makes an embed get sent. But keep in mind, this does not do things like log the new messages or send a typing indicator. You can do that yourself, docs are here.

Documentation

discordgpt.DiscordGPT(token: str, api_key: str, channel_id: Union[int, str], channel_name: str, model: str, prompt: str, message_to_send: str, logger: bool)

This makes a new Discord ChatGPT bot.

Parameters

  • token - Your Discord bot token.
  • api_key - Your OpenAI API key.
  • channel_id - The channel id for the bot to check for messages. Can be empty if channel_name is passed. Default is None.
  • channel_name - The channel name for the bot to check for messages. Can be empty if channel_id is passed. Default is None.
  • model - The model to use. Default is text-davinci-003 which is also the best model for completions, but you can specify yours (fine-tunes also work)
  • prompt - The prompt to give ChatGPT. Default is "User: {message}\n\nAssistant:\n\n". If you give it a custom prompt, put {message} where you want the message content to be.
  • message_to_send - The message to be sent in the channel. Default is "{response}". If you give it a custom message, put {response} where you want ChatGPT's response to be.
  • logger - Whether DiscordGPT should log events like when the bot goes up, when a new message is sent and the response. Default is True, can be turned off (by False).

Raises

  • GPTError, which is just a class for various exceptions, such as {message} or {response} missing, or channel_id and channel_name missing.

Methods

  • new_question - runs when a message is sent in the right channel. Accepts message (which is a discord.Message instance) from which it gets the content and author.
  • get_response - runs when the code needs ChatGPT's response to the message. It also accepts message as an argument, it's also a discord.Message instance, and it uses it for the content.

All of those are customizable. There's also _log which takes a colorama's Style attribute and the text to log, in case you ever need it.

That should be it!

If you don't know how to get an API key, get it here.

If you don't know how to make a bot and get its token, you can do that here. There's a lot of tutorials online, if anything's not clear.

Keep in mind that OpenAI is not free forever and even if ChatGPT is free on https://chat.openai.com, it's not free forever in the API. You get $5 for free when registering, and you should be good for quite a while. The pricing list is here (we don't use gpt-4 or gpt-3.5 turbo btw, we use davinci which is the best single reply gpt-3 model). If you don't know what a token is, you can check and test it here.

Happy GPTing!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discordgpt-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

discordgpt-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file discordgpt-0.1.1.tar.gz.

File metadata

  • Download URL: discordgpt-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for discordgpt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5e3ff9a9a76f8ac1c4130aecf7f042a77754751e9d6e63d71132a76d73b7fb03
MD5 50cf4d20ebb45c4d446ac4f6328afc4d
BLAKE2b-256 8377306cda7a94d20a84080ba294bcf0005a3af2880bee1973cc9eb7ae350629

See more details on using hashes here.

File details

Details for the file discordgpt-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: discordgpt-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for discordgpt-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4e9068e375abadae017f4ec4e8d96983808d95c103b38e718c28bad88548e72
MD5 9c4436f154e36757915c307324ae1020
BLAKE2b-256 524efd04e83475da7f842a844d1702760a9a49306692cb4f7d8ea76d5482d300

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page