Package: discordgpt
DiscordGPTThis is a package to use ChatGPT in Discord.InstallationYou can download the source here, or usepipto install the package:pip install discordgptUsage, examplesYou can use theDiscordGPTclass to make a ChatGPT bot:fromdiscordgptimportDiscordGPTDiscordGPT(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:fromdiscordgptimportDiscordGPTclassMyDiscordGPT(DiscordGPT):def__init__(self):super().__init__(token="bot token",api_key="openai api key",channel_id="a channel id",<moreoptionsifyouneed>)asyncdefnew_question(self,message):# you can really do anything here, a bad word checker is just an exampleif"a bad word"inmessage.content:returnawaitmessage.channel.send("Don't say bad words to ChatGPT!")awaitsuper().new_question(message)MyDiscordGPT()This will create a bad word blocker for DiscordGPT.
If you want to customize iteven further, you can overwrite the whole process:fromdiscordgptimportDiscordGPTimportdiscordclassMyDiscordGPT(DiscordGPT):def__init__(self):super().__init__(token="bot token",api_key="openai api key",channel_id="a channel id",<moreoptionsifyouneed>)asyncdefnew_question(self,message):response=super().get_response(message)# gets ChatGPT's responseawaitmessage.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 arehere.Documentationdiscordgpt.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.Parameterstoken- 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 ifchannel_nameis passed. Default isNone.channel_name- The channel name for the bot to check for messages. Can be empty ifchannel_idis passed. Default isNone.model- The model to use. Default istext-davinci-003which 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 isTrue, can be turned off (byFalse).RaisesGPTError, which is just a class for various exceptions, such as{message}or{response}missing, orchannel_idandchannel_namemissing.Methodsnew_question- runs when a message is sent in the right channel. Acceptsmessage(which is adiscord.Messageinstance) from which it gets the content and author.get_response- runs when the code needs ChatGPT's response to the message. It also acceptsmessageas an argument, it's also adiscord.Messageinstance, and it uses it for the content.All of those are customizable. There's also_logwhich 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 ithere.If you don't know how to make a bot and get its token, you can do thathere. 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 onhttps://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 ishere(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 ithere.Happy GPTing!