-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloggerBot.py
124 lines (107 loc) · 4.6 KB
/
loggerBot.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
import asyncio, discord, json
from discord.ext.commands import Bot
from discord.ext import commands
from tinydb import TinyDB, Query
from tinydb.operations import delete, increment
'''
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SETUP
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
# Create a bot
bot = Bot(description="Channel Logger Bot by jskrist#3569", command_prefix="!", pm_help = True)
# Start or connect to a database to log the messages
db = TinyDB('data.json')
# This is a Query object to use when searching through the database
msg = Query()
usr = Query()
'''
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
HELPER FUNCTIONS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
# this function returns a list of all the users that have posted to the server
def getPostingUsers():
postingUsers = set();
for item in db:
postingUsers.add(item['authorName'])
return postingUsers
async def addMsgToDB(message):
# Confirm that the message did not come from this Bot to make sure we don't get
# into an infinite loop if this bot send out any messages in this function also
# check that the first character of the message is not a "!" or "]", which would
# indicate a command
if (message.author.id != bot.user.id) & \
(message.content[0] != '!') & (message.content[0] != ']'):
# if the mesage content is not in the database yet
if not db.search(msg.content == message.content.lower()):
# Insert the content into the database, along with the name of the user that posted it.
# You could add any other data to the database at this point.
db.insert({'content': message.content.lower(), 'authorName': message.author.name})
'''
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BOT EVENTS AND COMMANDS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
# This function prints a message to the terminal/command window to let you know the bot started correctly
@bot.event
async def on_ready():
print('Bot is up and running.')
# when a message comes into the server, this function is executed
@bot.listen()
async def on_message(message):
await addMsgToDB(message)
# when a message on the server is edited, this function is executed
@bot.listen()
async def on_message_edit(msgBefore, msgAfter):
'''
update the database to reflect only the edited message. This could create a state where a
duplicate message is on the server, but not represented in the database, e.g.
User1 sends "Hello"
User2 sends "Hello"
Database no has {'content':"hello", "authorName":"User1"}
User1 edits post to say "Hello World"
Database now has {'content':"hello world", "authorName":"User1"}
Should it also contain a copy of the message "hello"? since User2 also sent it?
'''
# db.update({'content': msgAfter.content.lower()}, msg.content == msgBefore.content.lower())
'''
Alternatively, you could just add the updated message to the database:
'''
await addMsgToDB(msgAfter)
@bot.command(pass_context=True)
async def printDB(context):
# this command prints out the contents of the database. It should not be used with a large database.
# the database will be save into a file called data.json (see line 12 of this file).
for item in db:
await bot.send_message(context.message.channel, item)
@bot.command(pass_context=True)
async def stats(context):
# this command returns the stats for each user, at the moment that is just the number of messages
# each user has posted, but could be expanded however you'd like
postingUsers = getPostingUsers()
for user in postingUsers:
userMsgs = db.search(msg.authorName == user)
await bot.send_message(context.message.channel, '{0} has {1} messages'.format(user, len(userMsgs)))
@bot.command(pass_context=True)
async def clearDB_all(context):
# this command removes all of messages from the Database
db.purge()
@bot.command(pass_context=True)
async def clearDB_usr(context, User=""):
# this command removes all of messages in the Database from the given user
db.remove(usr.authorName == User)
@bot.command(pass_context=True)
async def clearDB_msg(context, Msg=""):
# this command removes the given messages from the Database if it exists
db.remove(msg.content == Msg.lower())
'''
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
STARTING THE BOT
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
# this opens up a file named botToken.txt which should contain a single line of text; the bot's token
with open('botToken.txt', 'r') as myfile:
botToken = myfile.read().replace('\n', '')
# start the bot
bot.run(botToken)