-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
170 lines (148 loc) · 5.85 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
# bot.py
import os
import discord
import fact_sphere, asyncio
import animalapi as animal_facts
import random, requests
from chuck_facts_list import chuck_facts
from sun_tzu import sun_tzu_quotes
import randfacts
TOKEN = os.environ["DISCORD_TOKEN"]
client = discord.Client()
# define fact_channels list global
fact_channels = []
types = ["fact_sphere", "animal_fact", "random_fact", "chuck_norris", "sun_tzu", "cat_fact"]
word_blacklist = [" die ", " killed ", " dying ", " kill ", " killing "]
every_x_seconds = 25*60
delete_after_x_seconds = every_x_seconds * 12 - 5
class Fact:
def __init__(self, text, file, type, truth_type, has_file):
self.text = text
self.file = file
self.type = type
self.truth_type = truth_type
self.has_file = has_file
# print all the attributes
print("Fact: ", self.text)
print("Data: ", self.file)
print("Type: ", self.type)
print("True: ", self.truth_type)
print("File: ", self.has_file)
print("\n")
def get_cat_fact():
url = "https://some-random-api.ml/facts/cat"
response = requests.get(url)
fact = response.json()["fact"]
url = "https://some-random-api.ml/img/cat"
response = requests.get(url)
image = response.json()["link"]
return fact, image
# define get_random_fact function
def get_random_fact():
data = "none"
truth_type = "unspecified"
# get random type
type = random.choices(types, weights=(5, 40, 35, 1, 10, 25), k=1)[0]
# get random fact
if type == "fact_sphere":
fact_data = fact_sphere.fact()
fact = fact_data.text
data = fact_data.audio.filepath
truth_type = fact_data.type
# if any of the blacklisted words are in the fact as a whole word, and not as part of a word, get another fact
for word in word_blacklist:
if word in fact:
return get_random_fact()
elif type == "animal_fact":
fact_data = animal_facts.rand_animals()
fact = fact_data["fact"]
data = fact_data["image"]
elif type == "random_fact":
fact = randfacts.get_fact()
elif type == "chuck_norris":
fact = random.choice(chuck_facts)
elif type == "sun_tzu":
fact = random.choice(sun_tzu_quotes)
elif type == "cat_fact":
fact, data = get_cat_fact()
if data == "none":
has_file = False
else:
has_file = True
return Fact(fact, data, type, truth_type, has_file)
class FactData:
def __init__(self, title, color):
self.title = title
self.color = color
def get_data_from_fact(fact):
data = FactData("", 0)
if fact.type == "fact_sphere":
data.title = "Fact Sphere says:"
# depending on truth level, set color, uses the fact_sphere.FactType enum
if fact.truth_type == fact_sphere.FactType.FALSE:
data.color = 0xe02a1d
if fact.truth_type == fact_sphere.FactType.NEARLY_TRUE:
data.color = 0xa2b535
if fact.truth_type == fact_sphere.FactType.NOT_FACTS:
data.color = 0xf2d46f
if fact.truth_type == fact_sphere.FactType.PARTIALLY_TRUE:
data.color = 0x84ad51
if fact.truth_type == fact_sphere.FactType.PROBABLY_FALSE:
data.color = 0xc97d42
if fact.truth_type == fact_sphere.FactType.SUBJECTIVE_UNVERIFIABLE:
data.color = 0x42c9c3
if fact.truth_type == fact_sphere.FactType.TRUE:
data.color = 0x1fed41
elif fact.type == "animal_fact":
data.title = "Fact about this Animal!"
data.color = 0xd94867
elif fact.type == "random_fact":
data.title = "Fact for you!"
data.color = 0xe844eb
elif fact.type == "chuck_norris":
data.title = "True Fact about Chuck Norris"
data.color = 0x6dedc9
elif fact.type == "sun_tzu":
data.title = "Sun Tzu says..."
data.color = 0xffd700
elif fact.type == "cat_fact":
data.title = "Fact about Cats!"
data.color = 0x1f052f
return data
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
for server in client.guilds:
for channel in server.channels:
if "fact" in channel.name.lower():
fact_channels.append(channel)
print(f"Found channel {channel.name} in {server.name}")
# Send a fact_sphere fact every x seconds in fact_channel
while True:
try:
for fact_channel in fact_channels:
# Print where the fact is being sent, server and channel
print(f"\nSending fact to {fact_channel.guild.name}/{fact_channel.name}")
fact = get_random_fact()
# send embed with fact and image if there is one and delete after x seconds
fact_data = get_data_from_fact(fact)
embed = discord.Embed(title = fact_data.title, description = fact.text, color = fact_data.color)
if fact.type != "fact_sphere":
if fact.has_file:
embed.set_image(url = fact.file)
await fact_channel.send(embed = embed, delete_after = delete_after_x_seconds)
elif fact.has_file:
await fact_channel.send(embed = embed, delete_after = delete_after_x_seconds, file=discord.File(fact.file, filename="fact_sphere.mp3"))
await asyncio.sleep(every_x_seconds)
except AttributeError:
print("AttributeError")
# upon bot joining a new server, add the fact channels of it to the list of fact channels
@client.event
async def on_guild_join(guild):
fact_channels = []
for server in client.guilds:
for channel in server.channels:
if "fact" in channel.name.lower():
fact_channels.append(channel)
print(f"Found channel {channel.name} in {server.name}")
client.run(TOKEN)