-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
73 lines (53 loc) · 1.78 KB
/
bot.py
File metadata and controls
73 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import datetime
import os
import asyncio
import disnake
from disnake.ext import commands
from transformers import pipeline
from pymongo import MongoClient
from dotenv import load_dotenv
load_dotenv()
list_channel_id = [int(id) for id in os.environ.get("CHANNEL_ID").split(",")]
# Setting the AI
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
model = "SamLowe/roberta-base-go_emotions"
classifier = pipeline("sentiment-analysis", model=model)
# Setting the Database
client = MongoClient(os.environ["MONGO_DB_URL"])
db = client[os.environ["DATABASE_NAME"]]
collection = db[os.environ["COLLECTION_NAME"]]
# Making the Database insertion asynchronous
async def insert_data(data: dict) -> None:
print(data)
await asyncio.to_thread(collection.insert_one, data)
print("!!!")
# Setting the bot
intents = disnake.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_message(ctx):
if ctx.author == bot.user:
return # Ignore messages from the bot itself
if ctx.channel.id in list_channel_id:
# Getting the AI response
message_emotion = classifier(ctx.content)
# Sending the data to the database
await insert_data({
'message': {
'message_id': ctx.id,
'channel': {
'channel_name': str(ctx.channel),
'channel_id': int(ctx.channel.id)
}
},
'time': datetime.datetime.now(tz=datetime.timezone.utc),
'ai': {
'label': message_emotion[0]['label'],
'score': message_emotion[0]['score']
}
})
bot.run(os.environ["BOT_TOKEN"])