-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbot.py
79 lines (56 loc) · 2.21 KB
/
bot.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
import os
import discord
import logging
from discord.ext import commands
from dotenv import load_dotenv
from agent import MistralAgent
PREFIX = "!"
# Setup logging
logger = logging.getLogger("discord")
# Load the environment variables
load_dotenv()
# Create the bot with all intents
# The message content and members intent must be enabled in the Discord Developer Portal for the bot to work.
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
# Import the Mistral agent from the agent.py file
agent = MistralAgent()
# Get the token from the environment variables
token = os.getenv("DISCORD_TOKEN")
@bot.event
async def on_ready():
"""
Called when the client is done preparing the data received from Discord.
Prints message on terminal when bot successfully connects to discord.
https://discordpy.readthedocs.io/en/latest/api.html#discord.on_ready
"""
logger.info(f"{bot.user} has connected to Discord!")
@bot.event
async def on_message(message: discord.Message):
"""
Called when a message is sent in any channel the bot can see.
https://discordpy.readthedocs.io/en/latest/api.html#discord.on_message
"""
# Don't delete this line! It's necessary for the bot to process commands.
await bot.process_commands(message)
# Ignore messages from self or other bots to prevent infinite loops.
if message.author.bot or message.content.startswith("!"):
return
# Process the message with the agent you wrote
# Open up the agent.py file to customize the agent
logger.info(f"Processing message from {message.author}: {message.content}")
response = await agent.run(message)
# Send the response back to the channel
await message.reply(response)
# Commands
# This example command is here to show you how to add commands to the bot.
# Run !ping with any number of arguments to see the command in action.
# Feel free to delete this if your project will not need commands.
@bot.command(name="ping", help="Pings the bot.")
async def ping(ctx, *, arg=None):
if arg is None:
await ctx.send("Pong!")
else:
await ctx.send(f"Pong! Your argument was {arg}")
# Start the bot, connecting it to the gateway
bot.run(token)