36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
import os
|
|
import requests
|
|
from discord.ext import commands
|
|
|
|
# Load environment variables
|
|
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
|
|
OLLAMA_API_URL = os.getenv('OLLAMA_API_URL')
|
|
|
|
# Create a bot instance with a command prefix (we'll use an empty string for natural interaction)
|
|
bot = commands.Bot(command_prefix='', intents=commands.Intents.all())
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
print(f'Logged in as {bot.user.name}!')
|
|
|
|
@bot.event
|
|
async def on_message(message):
|
|
# Ignore the message if it was sent by a bot
|
|
if message.author.bot:
|
|
return
|
|
|
|
# Forward the message to Ollama and get a response
|
|
try:
|
|
response = requests.post(OLLAMA_API_URL, json={'prompt': message.content, 'max_tokens': 100})
|
|
response.raise_for_status() # Raise an error for bad responses (4xx and 5xx)
|
|
ollama_response = response.json().get('choices', [{}])[0].get('text', '')
|
|
|
|
# Send the response from Ollama to the Discord channel
|
|
await message.channel.send(ollama_response)
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
print(f'Error communicating with Ollama: {e}')
|
|
await message.channel.send('I encountered an error while trying to get a response from Ollama. Please try again later.')
|
|
|
|
# Run the bot with your token
|
|
bot.run(DISCORD_TOKEN)
|