-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
310 lines (255 loc) · 12.6 KB
/
main.py
File metadata and controls
310 lines (255 loc) · 12.6 KB
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
# Discord for utilizing discord's api, time for waiting related functions, asyncpraw to access reddit posts
# os to access secret variables, random to create a sense of suspense, and importing commands from discord.ext to create
# a bot.
import asyncprawcore
import discord
import datetime
import time
import asyncpraw
import random
import json
import dev.secret_variables as secret_variables
from discord.ext import commands, tasks
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
fileWithIDs = 'dev/user_ids.txt'
# Accessing Reddit's API, this stuff is supposed to be secret.
reddit = asyncpraw.Reddit(
client_id=(secret_variables.client_ID),
client_secret=(secret_variables.client_secret),
user_agent=(secret_variables.user_agent)
)
# This is for the list of individuals in the list to receive kitty images.
listWithUserIDs = []
# List of subreddits to choose from
listOfSubreddits = ['IllegallySmolCats', 'cats', 'Blep', 'kitten_gifs']
# Counter to prevent people from spamming amogus command.
amogus_counter = 0
# SomethingRandom's ID.
owner_id = 265596926857183252
def createEmbed(submission):
redditLink = "https://www.reddit.com/" + submission.permalink
messageEmbed = discord.Embed(colour=3447003, title="Here's a cat, just for you! ^-^")
messageEmbed.add_field(name='Post Information', value=f"This post was created by reddit user {submission.author} which can be found [here]({redditLink})")
messageEmbed.set_footer(text="If the image/gif/video fails, feel free to click the blue link to see what would've been posted :D")
messageEmbed.set_image(url=submission.url)
return messageEmbed
# BOT COMMANDS start here
@bot.command()
# This command is used to obtain a single kitten gif or image by utilizing PRAW to retrieve a url.
async def getsinglekitty(ctx):
# Chooses a random subreddit from listWithSubreddits, uses createEmbed and then sends it to the individual who asked for it.
randomSubreddit = random.choice(listOfSubreddits)
chosenSubreddit = await reddit.subreddit(randomSubreddit)
submission = await chosenSubreddit.random()
single_kitty_embed = createEmbed(submission)
await ctx.channel.send(embed=single_kitty_embed)
@bot.command()
# Command to ask for multiple cats, limit is 15
async def getmultiplekitties(ctx, number):
if int(number) > 15:
await ctx.send("That's too many! Please do fewer than 16 :( it makes me tired.")
else:
number = int(number)
for i in range(0, number):
subreddit = await reddit.subreddit(random.choice(listOfSubreddits))
submission = await subreddit.random()
multiple_kitty_embed = createEmbed(submission)
await ctx.channel.send(embed=multiple_kitty_embed)
@bot.command()
# Sends a giant kitty.
async def sendbigkitty(ctx):
await ctx.send('https://cdn.discordapp.com/attachments/837418361696288828/837433194055598110/image0.jpg')
@bot.command()
# Command to join the list for kittens
async def joinlist(ctx): # A guy named indra in the python discord is the reason this command works. Adds the command user to listWithUserIDs
if ctx.author.id in listWithUserIDs:
await ctx.channel.send("Wait a minute! You're already in the list :O")
else:
with open(fileWithIDs, 'a') as file_object: # Add to the txt file
file_object.write(f"{ ctx.author.id }\n")
listWithUserIDs.append(ctx.author.id)
await ctx.channel.send(f"{ctx.author.name}, you have been added to the list! :D")
print(f"{ctx.author.name} has been added to the list")
@bot.command()
# Command to add in multiple user IDs with a tuple
async def joinlistmultiple(ctx, *user_ids_to_enter):
for id in user_ids_to_enter: # This for loop just adds them.
user = await bot.fetch_user(int(id))
if int(id) in listWithUserIDs:
await user.send(f"{user.display_name} is already in the list! .-. ")
print(user_ids_to_enter)
else:
try:
await user.send(f"This is to let you know you've been added into the list! :D If you would like to leave, be sure to use !leavelist")
listWithUserIDs.append(int(id)) # Adds the ID to the list
with open(fileWithIDs,'a') as file_object: # Appends to the txt file
file_object.write(f"{ id }\n")
except discord.Forbidden:
await ctx.channel.send(f"Either they don't share a server with me, or I'm blocked by them :( I can't add {user.display_name}")
@bot.command()
async def leavelist(ctx):
# Removes the command user from both the list and user_ids.txt, works by removing the user from the list, empties the entire txt file, then appends whoever is left in the
if ctx.author.id in listWithUserIDs:
listWithUserIDs.remove(ctx.author.id)
await ctx.author.send(f"This is to let you know you've been removed from the list! :D Hope you enjoyed the kitties and I hope to see you again soon! ^_^ ")
with open(fileWithIDs, 'w') as file_object: # Resets the file
file_object.write('')
for id in listWithUserIDs:
with open(fileWithIDs, 'a') as file_object: # Adds everyone else who is still in the file.
file_object.write(f"{ id }\n")
else:
await ctx.send(f"{await bot.fetch_user(ctx.author.id)} isn't in the list!")
@bot.command()
# Posts the list of people who have subscribed to getting random kitten images/gifs at random times.
async def postlist(ctx):
posting_list = ' '
if len(listWithUserIDs) > 0:
for individual in listWithUserIDs:
user = await bot.fetch_user(individual)
posting_list += "+ " + user.display_name + "\n"
await ctx.channel.send(posting_list)
else:
await ctx.channel.send(f"I don't have a list of people to send kittens to :( I'm sad now.")
@bot.command()
# Posts the dictionary of commands instantiated at the beginning of this file.
async def postcommands(ctx):
with open('commands_list.json') as commands_file:
cmds = json.load(commands_file)
commands_embed = discord.Embed.from_dict(cmds)
await ctx.channel.send(embed=commands_embed)
@bot.command()
# If you want to pet the kitten bot
async def petbot(ctx):
await ctx.channel.send(f"Mew! {ctx.message.author}, thank you for petting me! ^-^")
@bot.command()
# joke command, not meant to be anything serious
async def sendkittenstochina(ctx):
await ctx.channel.send(f"Mew mew mew!!!! Sending my brothers and sisters to China! ^-^")
@bot.command()
async def say(ctx, *message):
# Allows users to speak as the kitten bot
messagetosend = ""
for word in message:
messagetosend += " " + word
if '@everyone' in message:
await ctx.channel.send(f"{ctx.author} has used an everyone ping!")
elif 'sus' in message:
await ctx.channel.send("You do not have those privileges. LOOOOOOL")
await ctx.message.delete()
else:
try:
await ctx.channel.send(messagetosend)
await ctx.message.delete()
except discord.errors.HTTPException:
await ctx.channel.send("Cannot send an empty message.")
@bot.command()
async def announcement(ctx, *message):
# Command usable by SomethingRandom (or you if you decide to change it) to send a message to everyone that is currently subscribed to the list
if ctx.message.author.id == 265596926857183252:
for user_id in listWithUserIDs:
user = await bot.fetch_user(user_id)
messagetosend = "BOT ANNOUNCEMENT:"
for word in message:
messagetosend += " " + word
await user.send(messagetosend)
print(f"Message sent to {user}")
else:
await ctx.channel.send("You do not have access to this command.")
@bot.command()
async def pokeyourself(ctx):
# Why not poke yourself? Sometimes, it might be all that you need.
await ctx.author.send("You have poked yourself! ^_^")
@bot.command()
async def leaveserver(ctx, server_id):
# Removes kitten bot from the server id given
if ctx.author.id == 265596926857183252:
server_to_leave = bot.get_guild(int(server_id))
await server_to_leave.leave()
owner = await bot.fetch_user(265596926857183252)
await owner.send(f"Kitten Bot has left from {server_to_leave.name} (ID: {server_to_leave.id} )")
else:
await ctx.send("You do not have access to this command.")
@bot.command()
async def resetlist(ctx):
# Completely erases the entire user_ids.txt
if ctx.author.id == 265596926857183252:
with open(fileWithIDs, 'w') as file_object:
file_object.write('')
listWithUserIDs.clear()
print("The list has been reset!")
await ctx.channel.send("The list has been reset!")
else:
await ctx.channel.send("You do not have access to this command.")
@bot.command()
async def listservers(ctx):
# Lists the names of servers that Kitten Bot is in.
if ctx.author.id == 265596926857183252:
server_list = ' '
for server in bot.guilds:
server_list += f"+ {server.name} (ID: {server.id}) \n"
await ctx.channel.send(server_list)
else:
await ctx.channel.send("You do not have access to this command.")
@tasks.loop(seconds=1)
async def checkTime():
current_time = datetime.datetime.now()
if (current_time.minute == 0 and current_time.second == 0) or (current_time.minute == 30 and current_time.second ==0):
for user_id in listWithUserIDs:
user = await bot.fetch_user(user_id)
subreddit = await reddit.subreddit(random.choice(listOfSubreddits))
try:
submission = await subreddit.random()
except asyncprawcore.exceptions.Forbidden as error:
with open('crash_log.txt', 'w') as file_object: # Write to the crash log if this is the case.
file_object.write(f"{error} + \nSubreddit crashing name: {subreddit.display_name}")
print("Encountered a Forbidden error!")
owner = await bot.fetch_user(owner_id)
await owner.send( f"{subreddit.display_name} has crashed!")
try:
kitty_embed = createEmbed(submission)
await user.send(embed=kitty_embed)
print(f"{user.display_name} has been sent a picture.")
except discord.Forbidden:
owner = await bot.fetch_user(265596926857183252)
await owner.send(f"{user.display_name} has errored. Removing from the list.")
if user.id in listWithUserIDs:
listWithUserIDs.remove(user.id)
with open(fileWithIDs,'w') as file_object: # Resets the file
file_object.write('')
for id in listWithUserIDs:
with open(fileWithIDs,'a') as file_object: # Add everyone left in the list to the txt file
file_object.write(f"{ id }\n")
# Bot events start here
@bot.event
# Saying kitten bot will doom you to a breakfast with cookies and milk.
async def on_message(message):
if "kitten bot" in message.content.lower():
game = discord.Game(f"I see you, {message.author}")
await bot.change_presence(status=discord.Status.do_not_disturb, activity=game)
with open('dev/victims.txt', 'a') as file_object:
file_object.write(f"{message.author} incurred the wrath of Kitten Bot at {datetime.datetime.now()}\n")
time.sleep(1)
game = discord.Game("with a ball of yarn, mew!")
await bot.change_presence(status=discord.Status.online, activity=game)
await bot.process_commands(message)
@bot.event
async def on_ready():
"""What the bot will do here is read over user_ids.txt and then append each id
into listWithUserIDs so as to keep the list filled with whatever was inside it before.
It'll then set playing status and start the loop of giving cats every 45 minutes."""
with open(fileWithIDs) as file_object:
lines = file_object.readlines()
joining_users = ''
for line in lines:
listWithUserIDs.append(int(line))
user = await bot.fetch_user( int(line) )
joining_users += f"{user.display_name}'s ID has been pulled from the txt file.\n"
print(joining_users)
print("Loaded all IDs")
print("Live is UP! :D")
game = discord.Game("with a ball of yarn, mew!")
await bot.change_presence(status=discord.Status.online, activity=game)
await checkTime.start()
bot.run(secret_variables.discord_token)