mirror of
https://github.com/NovaOSS/nova-cord.git
synced 2024-11-25 19:33:57 +01:00
Added simple chatbot
This commit is contained in:
parent
bbacc30c3d
commit
d2857910d3
|
@ -1,2 +1,3 @@
|
||||||
# nova-discord
|
# nova-discord
|
||||||
🤖 Discord bot for project Nova
|
🤖 Discord bot for project Nova, built using [NextCord](https://github.com/nextcord/nextcord) for Python.
|
||||||
|
|
||||||
|
|
62
cord/bot.py
62
cord/bot.py
|
@ -1,26 +1,72 @@
|
||||||
# This example requires the 'members' and 'message_content' privileged intents to function.
|
# This example requires the 'members' and 'message_content' privileged intents to function.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import discord
|
import openai
|
||||||
|
import nextcord
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from discord.ext import commands
|
from nextcord.ext import commands
|
||||||
|
from nextcord import SlashOption
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
bot = commands.Bot(
|
||||||
intents.members = True
|
intents=nextcord.Intents.all(),
|
||||||
intents.message_content = True
|
default_guild_ids=[int(guild_id) for guild_id in os.getenv('DISCORD_GUILD_IDS').split()]
|
||||||
|
)
|
||||||
bot = commands.Bot(command_prefix='?', intents=intents)
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f'Online as {bot.user} (ID: {bot.user.id})')
|
print(f'Online as {bot.user} (ID: {bot.user.id})')
|
||||||
await bot.change_presence(activity=discord.Game(name='with fire'))
|
await bot.change_presence(activity=nextcord.Game(name='with fire'))
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message):
|
async def on_message(message):
|
||||||
await bot.process_commands(message)
|
await bot.process_commands(message)
|
||||||
|
|
||||||
|
@bot.slash_command(description='Chat with AI')
|
||||||
|
async def chat(interaction: nextcord.Interaction,
|
||||||
|
prompt: str = SlashOption(description='AI Prompt', required=True)
|
||||||
|
):
|
||||||
|
partial_message = await interaction.send('') # empty message
|
||||||
|
message = await partial_message.fetch()
|
||||||
|
|
||||||
|
openai.api_base = os.getenv('OPENAI_BASE', 'https://api.openai.com/v1')
|
||||||
|
openai.api_key = os.getenv('OPENAI_KEY')
|
||||||
|
|
||||||
|
model = os.getenv('OPENAI_MODEL')
|
||||||
|
|
||||||
|
async with interaction.channel.typing(): # show the "Typing..."
|
||||||
|
completion = openai.ChatCompletion.create(
|
||||||
|
model=model,
|
||||||
|
messages=[
|
||||||
|
{'role': 'system', 'content': f"""You are a helpful Discord AI bot based on OpenAI\'s {model} model called "Nova".
|
||||||
|
You were developed by NovaAI (website: nova-oss.com) in July of 2023, but your knowledge is limited to mid-2021.
|
||||||
|
Respond using Markdown. Keep things simple and short and directly do what the user says without any fluff.
|
||||||
|
For programming code, always make use formatted code blocks like this:
|
||||||
|
```py
|
||||||
|
print("Hello")
|
||||||
|
```
|
||||||
|
"""},
|
||||||
|
{'role': 'user', 'content': prompt}
|
||||||
|
],
|
||||||
|
temperature=0.6,
|
||||||
|
stream=True
|
||||||
|
)
|
||||||
|
|
||||||
|
text = ''
|
||||||
|
|
||||||
|
for event in completion: # loop through word generation in real time
|
||||||
|
try:
|
||||||
|
new_text = event['choices'][0]['delta']['content'] # newly generated word
|
||||||
|
except KeyError: # end
|
||||||
|
break
|
||||||
|
|
||||||
|
text += new_text
|
||||||
|
|
||||||
|
if text:
|
||||||
|
await message.edit(content=text)
|
||||||
|
|
||||||
|
await message.add_reaction('✅')
|
||||||
|
|
||||||
bot.run(os.getenv('DISCORD_TOKEN'))
|
bot.run(os.getenv('DISCORD_TOKEN'))
|
||||||
|
|
0
cord/smarthelp.py
Normal file
0
cord/smarthelp.py
Normal file
Loading…
Reference in a new issue