-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
283 lines (223 loc) · 10.4 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
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
"""
The main file for the bot.
"""
import os
import sys
import discord
from discord import app_commands
from dotenv import dotenv_values
import ifunnybot as funny
from ifunnybot.core import Logger, FunnyBot
from ifunnybot.utils import sanitize_discord, create_filename
# loading in config values
config = {
**os.environ,
**dotenv_values(".env")
}
# vars
DEV_MODE = False
DEV_SERVER = 0
# checking the .env values
Logger.debug("Checking for DEV to be set, if so, start in development mode.")
try:
DEV_MODE = bool(config["DEV"])
Logger.debug("DEV is set, starting in development mode.")
except:
Logger.debug("DEV not set, starting in production.")
# development mode checks
if DEV_MODE:
# checking for the development server id
if not config["GUILDID"]:
Logger.warn(f"The bot is currently running in development mode but the ID of the development server is not set, development mode won't work correctly.")
else:
# checking if the development server is an integer (it should be)
try:
DEV_SERVER = int(config["GUILDID"])
except Exception:
Logger.fatal("Could not cast GUILDID to an integer, development mode won't work correctly.")
# checking for the token
Logger.debug("Checking for TOKEN...")
if not config["TOKEN"]:
Logger.fatal("Couldn't start bot, missing 'TOKEN' from .env values.")
sys.exit(1)
Logger.debug("TOKEN is set.")
# intents
intents = discord.Intents.default()
intents.message_content = True
# creating the client
client = FunnyBot(intents=intents, logger=Logger, guildId=DEV_SERVER)
@client.event
async def on_ready():
Logger.info(f"Logged in as: '{client.user}'")
# presence message
_status, _tag = None, None
if DEV_MODE:
# development mode
_status = discord.Status.do_not_disturb
_tag = "Down for maintenance."
else:
# production
_status = discord.Status.online
_tag = "iFunny Bot v2.0"
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=_tag), status=_status)
@client.event
async def on_message(message: discord.message.Message):
# guard clauses
if message.author == client.user: # not replying to myself
return
if message.author.bot: # not replying to any messages from other bots
return
if not message.guild: # the message didn't come from a server
return
# debug mode shit
if DEV_MODE:
if message.guild.id != int(config["GUILDID"]):
# ignoring commands when in development mode
return
# ignoring messages that do not have an ifunny link
if not funny.has_url(message.content):
return
# testing if the interaction contains an iFunny link
if not (urls := funny.get_url(message.content)):
return
# there might be multiple urls
for url in urls:
# what type of url was it? post or user?
t = funny.get_datatype(url)
print(url, t, type(t))
match t:
case funny.PostType.USER:
# embed a user post
if not (profile := funny.get_profile_by_url(url)):
Logger.info(f"Could not find user '{user}' (although they may exist)")
return
else:
# creating an embed for the profile
embed = discord.Embed(description=profile.description)
# adding info
embed.set_author(name=sanitize_discord(profile.username),
url=profile.profile_url)
embed.set_thumbnail(url=profile.icon_url)
embed.set_footer(text=f"{profile.subscribers} subscribers, {profile.subscriptions} subscriptions, {profile.features} features")
# logging
Logger.info(f"Replying to interaction with embed about: {profile}")
# replying to interaction
return await message.reply(embed=embed)
# apparently, Python won't work properly if the case is a list of enums or comma-separated
case funny.PostType.VIDEO | funny.PostType.GIF | funny.PostType.PICTURE:
# the post was a link to a non user
if not (post := funny.get_post(url)):
Logger.error(f"There was an error extracting information from {message.content}")
await message.reply(content=f"There was an internal error embedding the post from {message.content}", silent=True)
# looping
continue
# creating an embed
embed = discord.Embed(title=f"Post by {sanitize_discord(post.username)}",
url=post.url,
description=f"{post.likes} likes.\t{post.comments} comments.")
embed.set_author(name="", icon_url=post.icon_url)
# create the filename
filename = create_filename(post)
# forming the file extension
extension = ""
match post.post_type:
case funny.PostType.PICTURE:
extension = "png"
case funny.PostType.VIDEO:
extension = "mp4"
case funny.PostType.GIF:
extension = "gif"
case _:
# this should never happen
Logger.error(f"Tried to make extension of invalid post type: {post.post_type}")
# creating the file object
file = discord.File(post.content, filename=f"{filename}.{extension}")
# logging
Logger.info(f"Replying to interaction with '{filename}.{extension}'")
await message.reply(embed=embed, file=file)
case _:
Logger.error(f"Could not discern the type of the post, silently aborting. Type was {funny.get_datatype(url)}")
return None
@client.tree.command(name="icon", description="Retrieves a user's profile picture. (case insensitive)")
@app_commands.describe(user="The user's name.")
async def icon(interaction: discord.Interaction, user: str):
# deferring the reply
await interaction.response.defer(thinking=True)
# getting the user's profile
if not (profile := funny.get_profile_by_name(user)):
Logger.info(f"Could not find user '{user}' (although they may exist)")
return await interaction.followup.send(content=f"Could not find user '{user}' (although they may exist)")
else:
# getting the icon of the user
if not (image := profile.retrieve_icon()):
Logger.info(f"An error occurred getting '{user}'s profile picture.")
return await interaction.followup.send(content=f"An error occurred getting '{user}'s profile picture.")
# user has icon, returning it
file = discord.File(image, filename=f"{profile.username}_pfp.png")
# logging
Logger.info(f"Replying to interaction with file: {profile.username}_pfp.png")
# returning the image
return await interaction.followup.send(file=file)
@client.tree.command(name="user", description="Embeds the link to a user's profile. (case insensitive)")
@app_commands.describe(user="The user's name.")
async def user(interaction: discord.Interaction, user: str):
# deferring the reply
await interaction.response.defer(thinking=True)
# getting the user's profile
if not (profile := funny.get_profile_by_name(user)):
Logger.info(f"Could not find user '{user}' (although they may exist)")
return await interaction.followup.send(content=f"Could not find user '{user}' (although they may exist)")
else:
# creating an embed for the profile
embed = discord.Embed(description=profile.description)
# adding info
embed.set_author(name=sanitize_discord(profile.username),
url=profile.profile_url)
embed.set_thumbnail(url=profile.icon_url)
embed.set_footer(text=f"{profile.subscribers} subscribers, {profile.subscriptions} subscriptions, {profile.features} features")
# logging
Logger.info(f"Replying to interaction with embed about: {profile}")
# replying to interaction
return await interaction.followup.send(embed=embed)
@client.tree.command(name="post", description="Posts a video/image from iFunny.")
@app_commands.describe(link="An iFunny.co link e.g., ifunny.co/video/...")
async def post(interaction: discord.Interaction, link: str):
# deferring the reply
await interaction.response.defer(thinking=True)
# testing if the interaction contains an iFunny link
if not (url := funny.get_url(link)):
# logging & returning
Logger.info(f"Received an improper link: {link}")
return await interaction.followup.send(content=f"The url {link}, isn't a proper iFunny url.")
# simple hack, my precious
url = url[0]
# got a valid link, getting the post information
if not (post := funny.get_post(url)):
Logger.error(f"There was an error extracting information from {link}")
return await interaction.followup.send(content=f"There was an internal error embedding the post from {link}")
# creating an embed
embed = discord.Embed(title=f"Post by {sanitize_discord(post.username)}",
url=post.url,
description=f"{post.likes} likes.\t{post.comments} comments.")
embed.set_author(name="", icon_url=post.icon_url)
# create the filename
filename = create_filename(post)
# forming the file extension
extension = ""
match post.post_type:
case funny.PostType.PICTURE:
extension = "png"
case funny.PostType.VIDEO:
extension = "mp4"
case funny.PostType.GIF:
extension = "gif"
case _:
# this should never happen
Logger.error(f"Tried to make extension of invalid post type: {post.post_type}")
# creating the file object
file = discord.File(post.content, filename=f"{filename}.{extension}")
# logging
Logger.info(f"Replying to interaction with '{filename}.{extension}'")
return await interaction.followup.send(embed=embed, file=file)
# main loop
client.run(config["TOKEN"])