ScreamingOpossum/discord_bot.py

51 lines
1.6 KiB
Python
Raw Normal View History

import os
import discord
from discord.ext import commands
from openai_interaction.image_recognition_test import image_recognition
# Загружаем токен из переменной среды
TOKEN = os.getenv('SCREAMING_OPOSSUM')
# Создаем клиент с префиксом (не обязателен для слэш-команд)
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
# region Bot Stuff
@bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')
try:
synced = await bot.tree.sync()
print(f'Synced {len(synced)} command(s).')
except Exception as e:
print(f'Failed to sync commands: {e}')
@bot.tree.command(name='hello', description='Replies with Hello World!')
async def hello_command(interaction: discord.Interaction):
await interaction.response.send_message('AAAAAAAAAAAAAAAAAAAAA!')
@bot.event
async def on_message(message: discord.Message):
if message.author == bot.user:
return
if message.attachments and bot.user in message.mentions:
message_attachments = [
{'filename': i_attachment.filename, 'url': i_attachment.url} for i_attachment in message.attachments
]
await message.channel.send(await image_recognition(message.content, message_attachments))
await bot.process_commands(message)
# endregion
if __name__ == '__main__':
if TOKEN is None:
raise RuntimeError('Environment variable SCREAMING_OPOSSUM is not set')
bot.run(TOKEN)