-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
66 lines (49 loc) · 1.81 KB
/
bot.py
File metadata and controls
66 lines (49 loc) · 1.81 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
import os
import discord
from dotenv import load_dotenv
from database import create_table, add_message
import datetime
# load virtual environment variables
load_dotenv()
# get token and guild name from .env file
APP_TOKEN = os.getenv('DISCORD_APP_TOKEN')
GUILD_ID = os.getenv('DISCORD_GUILD_ID')
# change this according to your need
keywords = ["?TSLA", "?AAPL", "?FCBK"]
# create a discord client for our bot
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
create_table()
@client.event
async def on_ready():
"""This method will be run when the client is ready"""
try:
guild = next(filter(lambda g: str(g.id) == str(GUILD_ID), client.guilds))
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name} (id: {guild.id})'
)
except:
raise ValueError("Couldn't connect to the server, please check `DISCORD_GUILD_ID` value in `.env`")
@client.event
async def on_message(message):
"""This method execute when a new message come"""
# check if the message is from the bot itself, don't change this
if message.author == client.user:
return
# print new incoming message
print(f'Message from {message.author}: {message.content}')
time = datetime.datetime.now()
time = time.strftime("%Y-%m-%d %I:%M %p")
add_message(message.author.name, message.content, message.author.guild.name, str(time))
await message.channel.send("Hi! I'm a bot, Thanks for messaging!")
@client.event
async def on_error(event, *args, **kwargs):
""" Handle error, if anything get error """
with open('logs/error.log', 'a+') as f:
if event == 'on_message':
f.write(f'Unhandled message: {args[0]}\n')
else:
raise
client.run(APP_TOKEN)