forked from devksingh4/fogelbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
240 lines (202 loc) · 7.22 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import discord
import os
from discord.ext import commands
from discord import Intents
from latex import render_latex, extract_inline_tex
import random
import requests
import json
import praw
from discord.ext.tasks import loop
client = commands.AutoShardedBot(command_prefix=os.environ['PREFIX'],intents=Intents.all())
token = os.environ['DISCORD_TOKEN']
reddit_token = os.environ['REDDIT_TOKEN']
reddit = praw.Reddit(client_id='ZOkK-ZCFJpcWCQ', client_secret=reddit_token,
user_agent='FogelBot by AsyncSGD', username='androstudios')
def createRandomSortedList(num, start=1, end=50):
arr = []
tmp = random.randint(start, end)
for _ in range(num):
while tmp in arr:
tmp = random.randint(start, end)
arr.append(tmp)
arr.sort()
return arr
def checkPost(i):
return (i.stickied or i.over_18)
@loop(seconds=150)
async def refreshCache():
global cache
global cache_funny
cache = [i for i in reddit.subreddit('memes').new() if not checkPost(i) ]
cache_funny = [i for i in reddit.subreddit('funny').new() if not checkPost(i)]
@client.event
async def on_ready():
global cache
global cache_funny
cache = [i for i in reddit.subreddit('memes').new() if not i.stickied]
cache_funny = [i for i in reddit.subreddit('funny').new() if not i.stickied]
print('Logged in as: ' + str(client.user.name) + ' ' + str(client.user.id))
activity = discord.Game(name=os.environ['ACTIVITY'])
await client.change_presence(activity=activity)
@client.event
async def on_message(message):
try:
start_marker = end_marker = '$$'
string = message.content
start = string.index(start_marker) + len(start_marker)
end = string.index(end_marker, start + 1)
lc = string[start:end]
if lc:
async with message.channel.typing():
et = extract_inline_tex(message.content)
id = render_latex(et)
if id != None:
await message.reply(file=discord.File('{}.png'.format(id)))
else:
await message.reply('Your LaTeX could not be rendered. Please, try again.')
try:
os.remove('{}.png'.format(id))
except:
pass
except ValueError: # no latex command found
await client.process_commands(message)
@client.event
async def on_message_edit(before, message):
try:
start_marker = end_marker = '$$'
string = message.content
start = string.index(start_marker) + len(start_marker)
end = string.index(end_marker, start + 1)
lc = string[start:end]
if lc:
async with message.channel.typing():
et = extract_inline_tex(message.content)
id = render_latex(et)
if id != None:
await message.reply(file=discord.File('{}.png'.format(id)))
else:
await message.reply('Your LaTeX could not be rendered. Please, try again.')
try:
os.remove('{}.png'.format(id))
except:
pass
except ValueError: # no latex command found
await client.process_commands(message)
class Main_Commands():
def __init__(self, client):
self.client = client
@client.command()
async def ping(ctx):
await ctx.send('Pong!')
@client.command()
async def tex(ctx, latex):
"""Render LaTeX code and reply with an image"""
id = ''
async with ctx.typing():
id = render_latex(latex)
if id != None:
await ctx.reply(file=discord.File('{}.png'.format(id)))
else:
await ctx.reply('Your LaTeX could not be rendered. Please, try again.')
if id != None:
os.remove('{}.png'.format(id))
else:
try:
os.remove('{}.png'.format(id))
except:
pass
@client.command()
async def clear(ctx, amount=0):
if (amount == 0):
await ctx.send("Please specify how many messages are to be deleted.")
else:
try:
realNum = amount + 1
await ctx.channel.purge(limit=realNum)
except discord.errors.Forbidden:
await ctx.send("Bot does not have neccessary permissions to delete messages.")
@client.command()
async def quote(ctx, num=1):
"""Send a Fogel quote"""
quote_template = """
> {}
~ Dr. Micah E. Fogel
"""
r = requests.get("https://raw.githubusercontent.com/devksingh4/fogelbot/master/quotes.txt")
quotes = r.text.split("\n")[0:-1] # remove last split line
quotes = list(map(lambda x: x.strip().replace("\'", "'"), quotes))
if num > len(quotes):
await ctx.send("There are not enough unique quotes to fufill this request.")
return
while num > 0:
random_index = random.randrange(len(quotes))
await ctx.send(quote_template.format(str(quotes[random_index])))
del quotes[random_index]
num -= 1
@client.command(aliases=['bio'])
async def mathbio(ctx):
"""Send a biography about a famous mathematician"""
bio_template = """
***{}***
**Born:** {} ({})
**Died:** {} ({})
**Summary:** {}
**Read more:** {}
"""
files = os.listdir('bios')
random_index = random.randrange(len(files))
with open('bios/{}'.format(files[random_index])) as f:
data = json.load(f)
await ctx.send(bio_template.format(data["name"], data["born"], data["born_place"], data["died"], data["died_place"], data["summary"], data["link"]))
@client.command()
async def meme(ctx, numMemes=1):
"""Sends a number of memes to a channel."""
try:
if (int(numMemes) > 5 or int(numMemes) < 1):
await ctx.send("Please provide a reasonable number of memes.")
return
except:
await ctx.send("Please provide a reasonable number of memes.")
return
x = int(numMemes)
if len(cache) < x:
await refreshCache()
randomlist = createRandomSortedList(x)
for i in randomlist:
selectedpost = cache[i]
if "i.redd.it" in selectedpost.url or 'imgur' in selectedpost.url:
await ctx.send("Here is a meme from r/memes: https://reddit.com{}".format(selectedpost.permalink), embed=discord.Embed(title=selectedpost.title).set_image(url=selectedpost.url))
else:
await ctx.send("Here is a meme from r/memes: {} \n\n*This post is a video. Please click on the link to see the full video*".format(selectedpost.url))
del cache[i]
@client.command()
async def clap(ctx, *, message):
"""Create messages with spaces replaced with claps"""
try:
await ctx.send(" 👏 " + message.replace(" ", " 👏 ") + " 👏 ")
except:
await ctx.send("Please provide a valid message to clap-ify!")
@client.command()
async def funny(ctx, numMemes=1):
"""Sends a number of memes to a channel."""
try:
if (int(numMemes) > 5 or int(numMemes) < 1):
await ctx.send("Please provide a reasonable number of posts to retrieve from r/funny.")
return
except:
await ctx.send("Please provide a reasonable number of posts to retrieve from r/funny.")
return
x = int(numMemes)
if len(cache_funny) < x:
await refreshCache()
randomlist = createRandomSortedList(x)
for i in randomlist:
selectedpost = cache_funny[i]
if "i.redd.it" in selectedpost.url or 'imgur' in selectedpost.url:
await ctx.send("Here is a post from r/funny: https://reddit.com{}".format(selectedpost.permalink), embed=discord.Embed(title=selectedpost.title).set_image(url=selectedpost.url))
else:
await ctx.send("Here is a post from r/funny: https://reddit.com{} \n\n *This post is a video. Please click on the link to see the full video*".format(selectedpost.permalink))
del cache_funny[i]
# refreshCache.start()
client.run(token)