-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_bot.py
48 lines (36 loc) · 1.51 KB
/
counter_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
import discord
import json # needs a Json File "users.json" to work
keywords = ["word1", "word2", "word3"]
class MyClient(discord.Client):
# bot login
async def on_ready(self):
print("Bot online")
def get_score(self, member: discord.Member):
with open("users.json") as users:
dictlist = json.load(users)
id = str(member.id)
for k in dictlist:
if k == id:
return dictlist[k] # return val corresponding to id
def add_score(self, member: discord.Member, amount: float):
with open("users.json") as jsonFile:
dictlist = json.load(jsonFile)
id = str(member.id)
if id not in dictlist:
dictlist[id] = amount # create new dict entry for user
else:
dictlist[id] += amount
new = dictlist
with open("users.json", "w+") as dictlist:
json.dump(new, dictlist, sort_keys=True, indent=4) # save the changes
# When a message is sent
async def on_message(self, message):
if message.author != client.user:
author = message.author
if message.content in keywords:
self.add_score(author, 0.01)
await message.channel.send(
message.content + " ??? " + author.mention + " Must pay 1ct." + author.name
+ "'s debts are now " + str(round(self.get_score(author), 3)) + "€!")
client = MyClient()
client.run("TOKEN")