-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
353 lines (324 loc) · 11.3 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import logging
from discord.ext import commands, tasks
from config import config
from logDumper import LogDumper
from r34API import R34API
from messages import messages, logTemplates
from activityList import activities
import discord
import time
import datetime
import random
dailyDoseTime = [
datetime.time(12, 0, tzinfo=datetime.timezone(datetime.timedelta(hours=3)))
]
class TheRes3ar4er(commands.Bot):
def __init__(self, locale):
self.API = R34API()
self.__logDumper = LogDumper()
self.locale = locale
self.searchTags = set()
self.filterTags = set()
self.__logDumper.send('Bot successfully deployed!')
super().__init__(command_prefix=config['bot']['prefix'], intents=discord.Intents.all())
@tasks.loop(hours=8)
async def dumpLogs(self):
self.__logDumper.send("make a dumper, you lazy ass")
pass
@tasks.loop(time=dailyDoseTime)
async def dailyDose(self):
await self.wait_until_ready()
chan = self.get_channel(int(config['bot']['daily_dose_chan']))
await chan.send(messages[self.locale]['daily_dose'])
recentPosts = self.API.getPosts(10)
for recentPost in recentPosts:
await chan.send(recentPost)
time.sleep(config['bot']['sending_delay'])
await chan.send(messages[self.locale]['finish'])
@tasks.loop(hours=1)
async def changePresence(self):
await self.wait_until_ready()
await self.change_presence(
status=discord.Status.online,
activity=random.choice(activities)
)
async def setup_hook(self):
self.changePresence.start()
self.dailyDose.start()
self.dumpLogs.start()
async def sendPosts(ctx, messageBegin, messageFinish, posts):
await ctx.send(messageBegin)
for post in posts:
await ctx.send(post)
time.sleep(config['bot']['sending_delay'])
await ctx.send(messageFinish)
logging.basicConfig(level=logging.INFO, filename='botLog.log', format="[%(asctime)s] [%(levelname)s] %(message)s")
bot = TheRes3ar4er(config['bot']['locale'])
# regular commands, result = posts
@bot.command(name='test')
async def test(ctx):
if ctx.author != bot.user:
logging.info(
logTemplates['command'] % ('test', ctx.author)
)
posts = bot.API.getPosts(10, {'yuri'})
await sendPosts(
ctx,
messages[bot.locale]['test'],
messages[bot.locale]['finish'],
posts
)
logging.info(
logTemplates['success'] % (str(posts))
)
@bot.command(name='recent')
async def getRecent(ctx, amount):
if ctx.author != bot.user:
logging.info(
logTemplates['command'] % ('recent', ctx.author)
)
if amount.isdigit():
posts = bot.API.getPosts(
int(amount)
)
await sendPosts(
ctx,
messages[bot.locale]['recent'],
messages[bot.locale]['finish'],
posts
)
logging.info(
logTemplates['success'] % (str(posts))
)
else:
logging.error(
logTemplates['failed'] % ('Invalid amount value: %s' % amount)
)
await ctx.send(
messages[bot.locale]['error'] % (
'$r34er-get-recent',
messages[bot.locale]['posts_to_send']
)
)
@bot.command(name='tagged')
async def getTagged(ctx, amount):
logging.info(
logTemplates['command'] % ('tagged', ctx.author)
)
if amount.isdigit():
posts = bot.API.getPosts(
int(amount),
bot.searchTags
)
await sendPosts(
ctx,
messages[bot.locale]['tagged'] % " ".join(bot.searchTags),
messages[bot.locale]['finish'],
posts
)
logging.info(
logTemplates['success'] % (str(posts))
)
else:
logging.error(
logTemplates['failed'] % ('Invalid amount value: %s' % amount)
)
await ctx.send(
messages[bot.locale]['error'] % (
'$r34er-get-recent',
messages[bot.locale]['posts_to_send']
)
)
# these commands add tags to some lists
@bot.command(name='manage-tags')
async def manageTags(ctx, command, tagType, tag):
if ctx.author != bot.user:
logging.info(
logTemplates['command'] % ('manage-tags', ctx.author)
)
match command:
case 'add':
match tagType:
case 'search':
bot.searchTags.add(tag)
logging.info(
logTemplates['success'] % (
'tag %s added to %s tags' % (
tag, tagType
)
)
)
await ctx.send(
messages[bot.locale]['add_search'] % (
tag
)
)
case 'filter':
bot.filterTags.add(tag)
logging.info(
logTemplates['success'] % (
'tag %s added to %s tags' % (
tag, tagType
)
)
)
await ctx.send(
messages[bot.locale]['add_filter'] % (
tag
)
)
case _:
logging.error(
logTemplates['failed'] % ('Invalid tag type: %s' % str(tagType))
)
await ctx.send(
messages[bot.locale]['error_mt']
)
case 'delete':
match tagType:
case 'search':
if tag in bot.searchTags:
bot.searchTags.remove(tag)
logging.info(
logTemplates['success'] % (
'tag %s deleted from %s tags' % (
tag, tagType
)
)
)
await ctx.send(
messages[bot.locale]['delete_search'] % (
tag
)
)
case 'filter':
if tag in bot.filterTags:
bot.filterTags.remove(tag)
logging.info(
logTemplates['success'] % (
'tag %s deleted from %s tags' % (
tag, tagType
)
)
)
await ctx.send(
messages[bot.locale]['delete_filter'] % (
tag
)
)
case _:
logging.error(
logTemplates['failed'] % ('Invalid tag type: %s' % str(tagType))
)
await ctx.send(
messages[bot.locale]['error_mt']
)
case _:
logging.error(
logTemplates['failed'] % ('Invalid command type: %s' % str(command))
)
await ctx.send(
messages[bot.locale]['error_mt']
)
@bot.command('reset-tags')
async def resetTags(ctx, tagType):
if ctx.author != bot.user:
logging.info(
logTemplates['command'] % ('reset-tags', ctx.author)
)
match tagType:
case 'search':
bot.searchTags.clear()
logging.info(
logTemplates['success'] % (
'%s tags have been reset' % (
tagType
)
)
)
await ctx.send(
messages[bot.locale]['reset_search']
)
pass
case 'filter':
bot.searchTags.clear()
logging.info(
logTemplates['success'] % (
'%s tags have been reset' % (
tagType
)
)
)
await ctx.send(
messages[bot.locale]['reset_filter']
)
case 'all':
bot.searchTags.clear()
logging.info(
logTemplates['success'] % (
'all tags have been reset'
)
)
await ctx.send(
messages[bot.locale]['reset_all']
)
case _:
logging.error(
logTemplates['failed'] % ('Invalid tag type: %s' % str(tagType))
)
await ctx.send(
messages[bot.locale]['error_rvt'] % (
'$r34er-reset-tags'
)
)
@bot.command('view-tags')
async def resetTags(ctx, tagType):
if ctx.author != bot.user:
logging.info(
logTemplates['command'] % ('view-tags', ctx.author)
)
match tagType:
case 'search':
await ctx.send(
messages[bot.locale]['view_search'] % (
" ".join(bot.searchTags)
)
)
logging.info(
logTemplates['success'] % (
" ".join(bot.searchTags)
)
)
case 'filter':
await ctx.send(
messages[bot.locale]['view_filter'] % (
" ".join(bot.filterTags)
)
)
logging.info(
logTemplates['success'] % (
" ".join(bot.filterTags)
)
)
case 'all':
await ctx.send(
messages[bot.locale]['view_all'] % (
" ".join(bot.searchTags),
" ".join(bot.filterTags)
)
)
logging.info(
logTemplates['success'] % (
" ".join(bot.searchTags) + ';' + " ".join(bot.filterTags)
)
)
case _:
logging.error(
logTemplates['failed'] % ('Invalid tag type: %s' % str(tagType))
)
await ctx.send(
messages[bot.locale]['error_rvt'] % (
'$r34er-view-tags'
)
)
bot.run(config['bot']['token'])