-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
171 lines (138 loc) · 5.29 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
import discord
import os
import requests
import json
from dotenv import load_dotenv
from pymongo import MongoClient
import random
import spacy
import re
import hashlib
load_dotenv()
client = discord.Client()
mongoclient = MongoClient(os.getenv("MONGO_URL"))
db = mongoclient.boba_db
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q']
return [quote, json_data[0]['a']]
def bobafy_quote(message):
nlp = spacy.load("en_core_web_sm")
doc = nlp(message)
first = True
tog = True
replaced = []
for token in doc:
if token.pos_ == "NOUN" and tog:
if(first):
replaced.append([str(token), "Boba"])
replaced.append([str(token).lower(), "boba"])
first = False
else:
replaced.append([str(token), "boba"])
if(token.pos_ == "PUNCT" and str(token) != ','):
first = True
else:
first = False
if(token.pos_ == "NOUN"):
tog = not tog
replaced.sort()
for pair in replaced:
message = message.replace(pair[0], pair[1])
return(message + " - BobaBot")
#to fix a bug where substrings of longer words are replaced with "boba" (like act -> boba, character -> charbobaer ), add
#all of the nouns to a list and then sort by size before replacing
@client.event
async def on_ready():
print('Ready to count boba intake as {0.user}'.format(client))
def get_count(user):
collection = db.boba_count
user_query = {
"user": user,
}
query_user = collection.find_one(user_query)
if query_user is None:
return 0
else:
return query_user["boba_count"]
def update_count(user, increment=1):
collection = db.boba_count
user_query = {
"user": user,
}
query_user = collection.find_one(user_query)
if query_user is None:
count = increment
user_query["boba_count"] = count
collection.insert_one(user_query)
else:
filter = { 'user': user }
newvalues = { "$set": { 'boba_count': query_user["boba_count"] + increment } }
collection.update_one(filter, newvalues)
count = query_user["boba_count"] + increment
return count
def add_user_to_server_db(user, server: str):
collection = db[server]
user_query = {
"user": user,
}
query_user = collection.find_one(user_query)
if query_user is None:
collection.insert_one(user_query)
async def handle_boba(message):
username = str(message.author).split("#")[0]
channel = str(message.guild)
add_user_to_server_db(username, channel)
word_list = message.content.split(" ")
if len(word_list) > 2 or word_list[-1] == "help":
my_message = f"Use the boba count with 1 argument.\nType `count` to get your boba count, type `integer` to add to your boba count (default: 1).\nFor more information, try !boba info"
await message.channel.send(my_message)
elif word_list[-1] == "count":
# user calls !boba count to get their boba count
count = get_count(username)
my_message = f"{username}'s boba count is {count}"
await message.channel.send(my_message)
elif word_list[-1] == "info":
my_message = "https://github.com/milo-ucla/BobaBot/blob/4f25e9e1472c81d0dcc14efe334f653d9a738e41/README.md"
await message.channel.send(my_message)
elif len(word_list) == 2:
incr = 0
my_message = "Error: unhandled exception"
try:
incr = int(word_list[-1])
count = update_count(username, incr) # update_across_databases(username, incr)
my_message = f"{username}'s boba count is now {count}"
except:
my_message = "Error: second arg invalid. Try `!boba help` for more information."
# user just calls !boba to increment count
await message.channel.send(my_message)
elif len(word_list) == 1:
# user just calls !boba to increment count
count = update_count(username)
my_message = f"{username}'s boba count is now {count}"
await message.channel.send(my_message)
@client.event
async def on_message(message):
if message.author == client.user:
return
channel = message.guild
if str(channel) not in db.list_collection_names():
db.create_collection(str(channel))
if message.content.startswith('!boba quote'):
quote = get_quote()
my_message = bobafy_quote(quote[0])
fullquote = quote[0] + " - " + quote[1]
await message.channel.send(my_message + '\n||' + fullquote +'||')
elif message.content.startswith('!boba?'):
my_message = "I can't say, not until Milo codes this bot better"
yn = random.choice([True, False])
if(yn):
my_message = "Yes, go get some boba!"
else:
my_message = "Nah, not today"
await message.channel.send(my_message)
elif message.content.startswith('!boba'):
await handle_boba(message)
client.run(os.getenv('TOKEN'))