-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (111 loc) · 4.78 KB
/
main.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -- imports
## discord
import discord
from discord.ext import commands
from discord import app_commands
## stuff
from random import randint
from typing import Optional
## other
from consts import SONGS
from func import which_function
from logger import Logger
## os & env
import sys
from os import getenv
from dotenv import load_dotenv
## neural network
from neuralintents import BasicAssistant
# -- config
LOGGER = Logger()
load_dotenv()
try:
chatbot = BasicAssistant('./src/json/intents.json')
except FileNotFoundError:
chatbot = BasicAssistant('../src/json/intents.json')
LOGGER.makeLog("Training model...", "INFO")
chatbot.fit_model(epochs=400)
LOGGER.makeLog("Saving model...", "INFO")
chatbot.save_model()
## const
TOKEN = getenv('TOKEN') # bot token
OWNID = getenv('OWNID') # own id
GUILD = discord.Object(id=int(getenv('GUILD'))) # type: ignore
if not TOKEN:
LOGGER.makeLog("Undefined token", "CRITICAL")
sys.exit("Undefined token")
if not OWNID: # NOTE: Remove if changing prefix
LOGGER.makeLog("Undefined ID", "CRITICAL")
sys.exit("Undefined ID")
PRFX: str = f"<@{OWNID}>" # NOTE: Edit if changing prefix
## discord client setup
intents = discord.Intents.default()
intents.message_content = True
client = commands.Bot(PRFX, intents=intents, activity=discord.Activity(type=discord.ActivityType.listening, name=SONGS[randint(0, len(SONGS)-1)]))
LOGGER.makeLog("Bot is starting...", "INFO")
client.activity = discord.Activity(type=discord.ActivityType.listening, name=SONGS[randint(0, len(SONGS)-1)])
# -- bot
@client.event
async def on_ready():
LOGGER.makeLog(f"Successfully loggged in as {client.user}", "INFO")
LOGGER.makeLog(f"ID: {client.user.id}", "DEBUG") #type: ignore --- no error here
sys.stdout.flush()
await client.tree.sync(guild=GUILD)
@client.event
async def on_message(message):
# Ignore self
if message.author == client.user:
return
if message.content.startswith(PRFX):
LOGGER.makeLog(f"Mentioned bot", "DEBUG")
cb_response = chatbot.process_input(message.content[len(PRFX):])
try:
await message.channel.send(which_function(cb_response, message.content[len(PRFX):])) # type: ignore --- no error here
except ModuleNotFoundError:
await message.channel.send(cb_response)
## commands
@client.tree.command(
name="ping",
description="Replies with pong"
)
async def ping(interaction: discord.Interaction):
"""Get bot's latency"""
try:
LOGGER.makeLog(f"Invoked ping command ({round(client.latency * 1000)})", "DEBUG")
await interaction.response.send_message(f"Pong! ||*with {round(client.latency * 1000)}ms*||")
except discord.errors.NotFound or discord.app_commands.errors.CommandInvokeError:
LOGGER.makeLog(f"Error occurred when calling *ping* command", "ERROR")
await interaction.response.send_message(f"An error occurred...", ephemeral=True)
@client.tree.command(
name="joined",
description="Says when a member joined",
)
@app_commands.describe(member='The member you want to get the joined date from; defaults to the user who uses the command')
async def joined(interaction: discord.Interaction, member: Optional[discord.Member] = None):
"""Says when a member joined."""
# If no member is explicitly provided then we use the command user here
member = member or interaction.user # type: ignore --- no error here
# The format_dt function formats the date time into a human readable representation in the official client
try:
LOGGER.makeLog(f"Invoked joined command for {member}", "DEBUG")
await interaction.response.send_message(f'{member} joined {discord.utils.format_dt(member.joined_at)}') # type: ignore --- no error here either
except discord.errors.NotFound or discord.app_commands.errors.CommandInvokeError:
LOGGER.makeLog(f"Error occurred when calling *joined command", "ERROR")
await interaction.response.send_message(f"An error occurred...", ephemeral=True)
@client.tree.command(
name="song",
description="Replies with a random song and changes the bot's activity to it"
)
async def song(interaction: discord.Interaction):
"""Get a random song"""
try:
LOGGER.makeLog(f"Invoked song command", "DEBUG")
r_song = SONGS[randint(0, len(SONGS)-1)]
await interaction.response.send_message(f"You should listen to {r_song}!")
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=r_song))
except discord.errors.NotFound or discord.app_commands.errors.CommandInvokeError:
LOGGER.makeLog(f"Error occurred when calling *song* command", "ERROR")
await interaction.response.send_message(f"An error occurred...", ephemeral=True)
# -- run
client.tree.copy_global_to(guild=GUILD)
client.run(TOKEN)