-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
147 lines (128 loc) · 5.2 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
#!/usr/bin/env python3
import requests
import os
from dotenv import load_dotenv
import discord
import random
import re
import json
import os.path
troll = "https://topps.diku.dk/torbenm/troll.msp"
data = { 'what' : 'Make random rolls.',
'trollExp' : '1d20',
'noOfDice' : '1'
}
def make_roll():
test = requests.post(troll, data=data)
for line in test.text.split('\n'):
if '<P>Rolls' in line:
return re.sub("[^0-9]", "", line.split()[-1])
load_dotenv()
TOKEN = os.getenv('TOKEN')
GUILD = os.getenv('GUILD')
def save_rules(rule_set, rules_id):
rules_file = "rules" + str(rules_id) + ".json"
with open(rules_file, "w+") as f:
json.dump(rule_set, f)
def load_rules(rules_id, new_game=False):
rules_file = "rules" + str(rules_id) + ".json"
if os.path.isfile(rules_file) and (not new_game):
with open(rules_file, "r") as f:
return json.load(f)
else:
rule_set = [""]*20
rule_set[0] = "Tag 1 bunder!"
rule_set[1] = "Tag 1/2 bunder!"
rule_set[9] = "Lav en ny regel!"
rule_set[18] = "Giv 1/2 bunder!"
rule_set[19] = "Giv 1 bunder!"
return rule_set
def pretty_rules(rule_set, rules_id):
title = "Terning Rules - Rules-ID=" + str(rules_id) + " - \n"
sep_line = ("#"*len(title)) + "\n"
str_rules = ""
for i in range(len(rule_set)):
str_rules += "Rule " + str(i+1) + ": " + rule_set[i] + "\n"
return title + sep_line + str_rules
current_rules_id = 0
rules = load_rules(current_rules_id)
# rules[0] = "Tag 1 bunder!"
# rules[1] = "Tag 1/2 bunder!"
# rules[9] = "Lav en ny regel"
# rules[18] = "Giv 1/2 bunder!"
# rules[19] = "Giv 1 bunder!"
client = discord.Client()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!r':
response = make_roll()
idx = int(response)-1
rule = rules[idx]
def check(reaction):
return reaction.author == message.author or reaction == "skip"
await message.channel.send(response)
if rule == "":
await message.channel.send("Lav en ny regel!")
await message.channel.send(message.author)
msg = await client.wait_for('message', timeout=200.0, check=check)
rules[idx] = msg.content
else:
await message.channel.send(rule)
if idx == 9:
await message.channel.send(pretty_rules(rules, current_rules_id))
await message.channel.send("vælg et tal")
# test = await client.wait_for('message', timeout=200.0, check=check)
while True:
test = await client.wait_for('message', timeout=200.0, check=check)
if test.content == "skip":
break
if (test.content.isnumeric()):
idx = int(test.content)-1
else:
await message.channel.send("idiot! der blev sagt et tal")
continue
if (idx >= 0 or idx < 20) and idx != 9:
break
else:
await message.channel.send("vælg et tal mellem 0 og 20, du kan ikke vælge 10")
if test.content == "skip":
await message.channel.send("næstes tur")
else:
await message.channel.send("skriv din regel")
msg = await client.wait_for('message', timeout=200.0, check=check)
rules[idx] = msg.content
if message.content == '!rules':
response = pretty_rules(rules, current_rules_id)
await message.channel.send(response)
if message.content == '!save':
terning.save_rules(rules, current_rules_id)
response = "Saved rules-ID " + str(current_rules_id)
await message.channel.send(response)
cont = message.content.split(" ", 2)
if len(cont) == 3:
if cont[0] == "!c" and cont[1].isnumeric():
rule = int(cont[1])-1
if 0 <= rule and rule <= 20:
prev_rule = rules[rule]
rules[rule] = cont[2]
response = "Change rule " + str(rule) + " from " + prev_rule + " to " + rules[rule]
await message.channel.send(response)
# if message.content[:5] == '!load' and message.content[6:].replace(" ", "").isnumeric():
# terning.save_rules(rules, current_rules_id)
# current_rules_id = int(message.content[6:].replace(" ", ""))
# rules = terning.load_rules(current_rules_id)
# response = "loaded rules-ID " + str(current_rules_id)
# await message.channel.send(response)
# with message.content.split(" ", 2) as cont:
# response = "Wrong input"
# print(cont)
# if cont[0] == "!c" and cont[1].isnumeric:
# rule = int(cont[1])-1
# if 0 <= rule and rule <= 20:
# prev_rule = rules[rule]
# rules[rule] = cont[2]
# response = "Change rule " + str(rule) + " from " + prev_rule + " to " + rules[rule]
# await message.channel.send(response)
client.run(TOKEN)