-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·166 lines (137 loc) · 5.83 KB
/
main.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
#!/usr/local/bin/python3.6
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
# This is a discord bot to count how many times people say lol
import discord
import traceback
from discord.ext import commands
import random
import sys
import asyncio
import json
description = "LoL, bot!"
# Path prefix for the lolbot directory containing the count files
file_loc = "/home/gector/lolbot/"
# Full path for the token file, the file containing the bot token
token_file="/home/gector/discord_bot_token.txt"
# This gets filled later
token = ""
# the "lol"s in commands aren't checked.
commands = [
"lolcheck",
"lolremove",
"lolrestart",
"loladd",
"scoreboard",
"lolscore",
"lolboard"]
# Populate the token file.
with open(token_file, 'r') as file:
token = file.readline(100).strip()
print("Bot token: " + token)
# Init the client
bot = discord.Client() # commands.Bot(command_prefix='?', description=description)
@bot.event
async def on_ready():
print("Connected!")
print("Username: " + bot.user.name)
print('------------------')
@bot.event
async def on_error(event, *args, **kwargs):
await bot.send_message(bot.get_channel("289957526882222080"), "ERROR: FROM: {}, message: {}".format(event, traceback.format_exc()))
print("error.")
#print("ERROR: FROM: {}, message: {}".format(event, sys.exc_info()))
#discord.on_error(event, *args, **kwargs)
@bot.event
async def on_message(message): # Basis for most of the bot functions
data = 0; # Filled later
if message.author.bot:
return
### ---- Commands {{{{ ---- ###
if message.content.lower().find("lolcheck") != -1:
with open(file_loc + "count.txt") as file:
num = file.readline(9999).strip()
await bot.send_message(message.channel, "We have a total of {} \'lol\'s so far".format(num))
if message.content.lower().find("lolrestart") != -1:
await bot.send_message(message.channel, "AHHHhh!")
await bot.send_message(message.channel, "Restarting: " + message.author.nick);
#sys.exit()
raise SystemExit(0)
if message.content.lower().find("lolscore") != -1:
with open(file_loc+'data.json') as json_data:
data = json.load(json_data)
await bot.send_message(message.channel, message.author.name + ": " +
str(data[message.author.name.lower()]))
if message.content.lower().find("lolboard") != -1:
with open(file_loc+'data.json') as json_data:
data = json.load(json_data)
fill_me = ""
for usr in message.server.members:
if usr.name.lower() in data and data[usr.name.lower()] != 0:
fill_me += usr.name + ": " + str(data[usr.name.lower()]) + "\n"
await bot.send_message(message.channel, fill_me)#bot.send_message(message.channel, usr.name + ": " + str(data[usr.name.lower()]))
if message.content.lower().find("lolremove") != -1 and message.author.name.lower().find("gector") != -1:
msg_args = message.content.split(" ");
arg1 = int(msg_args[1].strip())
num = 0
with open(file_loc + "count.txt") as file:
num = int(file.readline(9999).strip())
num = num - arg1
with open(file_loc + "count.txt", "w") as file:
file.write(str(num))
await bot.send_message(message.channel, "\'lol\'s now: " + str(num))
if message.content.lower().find("loladd") != -1 and message.author.nick.lower().find("gector") != -1:
msg_args = message.content.split(" ");
arg1 = int(msg_args[1].strip())
num = 0
with open(file_loc + "count.txt") as file:
num = int(file.readline(9999).strip())
num = num + arg1
with open(file_loc + "count.txt", "w") as file:
file.write(str(num))
await bot.send_message(message.channel, "\'lol\'s now: " + str(num))
### --- }}}} end commands --- ###
if message.content == "die123":
raise ValueError('err...')
if message.content.lower().find("lol") != -1 and message.author.name != bot.user.name:
print("Got lol")
# Check for commands the message, if any commands are found then cancel checking for "lol"s
for x in commands:
if message.content.lower().find(x) != -1:
return;
# JSON WIP
with open(file_loc+'data.json') as json_data:
data = json.load(json_data)
print(data)
for usr in message.server.members:
if usr.name.lower() not in data:
data[usr.name.lower()] = 0
data[message.author.name.lower()] += 1
with open(file_loc+'data.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
if random.randint(1, 30) == 15:
with open(file_loc + "count.txt") as file:
num = file.readline(99999).strip()
await bot.send_message(message.channel, "LOL!(x{})".format(num))
print(message.channel)
data = {0, 1}
with open(file_loc + "count.txt") as file:
data = file.readline(99999).strip()
print(data)
data = int(data) + 1
with open(file_loc + "count.txt", 'w') as file:
file.write(str(data))
if message.content.lower().find(x) == -1:
return;
if random.randint(1, 30) == 15:
with open(file_loc + "count.txt") as file:
num = file.readline(99999).strip()
await bot.send_message(message.channel, "LOL!(x{})".format(num))
print(message.channel)
data = {0, 1}
with open(file_loc + "count.txt") as file:
data = file.readline(99999).strip()
with open(file_loc + "count.txt", "w") as file:
print(data)
data = int(data) + 1
file.write(str(data))
bot.run(token)