-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbr.py
307 lines (267 loc) · 13 KB
/
dbr.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
# To Do: /Lightbar (will add at a later date)
import discord
import insta
import requests
import asyncio
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
from findcover import album_cover
from typing import Literal, Optional
import os
if not os.path.exists("TOKEN"):
print("Please enter the bot token in the file named 'TOKEN'")
exit()
with open("TOKEN", "r") as file:
TOKEN = file.read()
if TOKEN == "":
print("Please enter the bot token in the file named 'TOKEN'")
exit()
global client
intents = discord.Intents.all()
intents.message_content = True
class DiscordClient(discord.Client):
def __init__(self) -> None:
super().__init__(intents=intents)
self.synced = False
self.added = False
self.tree = discord.app_commands.CommandTree(self)
self.activity = discord.Activity(type=discord.ActivityType.playing, name="Muziek bij Radio Rijks")
async def on_ready(self):
await self.wait_until_ready()
print("Syncing")
if not self.synced:
await self.tree.sync()
self.synced = True
if not self.added:
self.added = True
print(f"Synced, {self.user} is running!")
client = DiscordClient()
def cover_to_image(image_url: str, overlay: str, output_file: str = "AlbumCover.png"):
response = requests.get(image_url)
if not response.ok:
return None, "Download failed"
image_data = BytesIO(response.content)
image = Image.open(image_data)
overlay = Image.open(overlay)
x, y = image.size
if x != y:
return None, "Size"
image = image.resize(overlay.size)
overlayed_image = Image.alpha_composite(image.convert("RGBA"), overlay.convert("RGBA"))
image_bytes = BytesIO()
overlayed_image.save(image_bytes, format="PNG")
image_bytes.seek(0)
return image_bytes, output_file
def text_overlay(name: dict, artist: dict, image_bytes: BytesIO, output_file: str = "AlbumCoverTO.png"):
image_data = image_bytes
image = Image.open(image_data)
draw = ImageDraw.Draw(image)
font_size = name["Size"]
font = ImageFont.truetype(name["Font"], font_size)
text = name["Title"].title()
position = name["pos"]
text_color = name["Color"]
draw.text(position, text, font=font, fill=text_color)
font_size = artist["Size"]
font = ImageFont.truetype(artist["Font"], font_size)
text = artist["Artist"].title()
position = artist["pos"]
text_color = artist["Color"]
draw.text(position, text, font=font, fill=text_color)
image = image.convert("RGB")
image = image.resize((1080, 1080))
edited_image = image
image_bytes = BytesIO()
edited_image.save(image_bytes, format="JPEG")
image_bytes.seek(0)
return image_bytes, output_file
def defer(func):
async def wrapper(interaction: discord.Interaction, *args, **kwargs):
await interaction.response.defer()
await func(interaction, *args, **kwargs)
return wrapper
class Checks:
"""Checking decorators for the commands. Class based for easy use."""
def admin_only(func):
"""Check if user has admin permissions"""
async def wrapper(interaction: discord.Interaction, *args, **kwargs):
if not interaction.user.guild_permissions.administrator:
await interaction.followup.send(f"You don't have permission to use this command, {interaction.user.mention}")
return
await func(interaction, *args, **kwargs)
return wrapper
def commands_channel_only(func):
"""Check if the command is used in the main channel"""
async def wrapper(interaction: discord.Interaction, *args, **kwargs):
if not interaction.channel.name == "commands":
await interaction.followup.send(f"You can't use this command here, {interaction.user.mention}")
return
await func(interaction, *args, **kwargs)
return wrapper
def confirm(func):
"""Roughly based uppon the reaction from "Just a random coder" on:
https://stackoverflow.com/questions/76299397/how-to-add-accept-deny-button-to-a-submission-bot-discord-py/76302606#76302606"
This decorator adds a confirm and cancel button to the command, and only runs the command if the confirm button is pressed by the same user who used the command.
Made for safety."""
async def wrapper(interaction: discord.Interaction, *args, **kwargs):
iu = interaction.user
b1 = discord.ui.Button(label='Ja', style=discord.ButtonStyle.success)
b2 = discord.ui.Button(label='Nee', style=discord.ButtonStyle.danger)
view = discord.ui.View()
view.add_item(b1)
view.add_item(b2)
async def b1_callback(interaction:discord.Interaction):
if interaction.user != iu:
await interaction.response.send_message(f"{interaction.user.mention}You can't confirm this command, as it was run by {iu.mention}.", ephemeral=True)
return
await prevmessage.edit(content="Bevestigd", view=None)
await func(interaction, *args, **kwargs)
async def b2_callback(interaction:discord.Interaction):
if interaction.user != iu:
await interaction.response.send_message(f"{interaction.user.mention}You can't cancel this command, as it was run by {iu.mention}.", ephemeral=True)
return
await prevmessage.edit(content="Geannuleerd", view=None)
b1.callback = b1_callback
b2.callback = b2_callback
# sadly, this can NOT be set to only visible for the user who used the command (due to deferring), so we have to check if the user is the same as the user who used the command.
prevmessage = await interaction.followup.send('Weet je het zeker?', ephemeral=True, view=view)
return wrapper
def confirm_upload_instagram(func):
async def wrapper(interaction: discord.Interaction, *args, **kwargs):
iu = interaction.user
b1 = discord.ui.Button(label='Jazeker', style=discord.ButtonStyle.success)
b2 = discord.ui.Button(label='Nee', style=discord.ButtonStyle.danger)
view = discord.ui.View()
view.add_item(b1)
view.add_item(b2)
async def b1_callback(interaction:discord.Interaction):
if interaction.user != iu:
await interaction.response.send_message(f"{interaction.user.mention}You can't confirm this command, as it was run by {iu.mention}.", ephemeral=True)
return
await prevmessage.edit(content="Bevestigd - Dit kan even duren", view=None)
await func(interaction, *args, **kwargs)
async def b2_callback(interaction:discord.Interaction):
if interaction.user != iu:
await interaction.response.send_message(f"{interaction.user.mention}You can't cancel this command, as it was run by {iu.mention}.", ephemeral=True)
return
await prevmessage.edit(content="Geannuleerd", view=None)
b1.callback = b1_callback
b2.callback = b2_callback
# set them to delete after 2 minutes
prevmessage = await interaction.followup.send('Wil je deze foto uploaden?', ephemeral=True, view=view)
return wrapper
@client.tree.command(
name="help",
description="Get help with the commands"
)
async def help(interaction: discord.Interaction):
@defer
@Checks.commands_channel_only
async def run(interaction: discord.Interaction):
await interaction.followup.send(\
"""Het help menu:
/Lightbar: Past de text van de lightbar aan (nog niet geïmplementeerd)
/InstaLogin: Login bij instagram, voor de bot
/InstaPostImage: Post een afbeelding (met tekst) naar Instagram
/InstaPostText: Post een tekst (met afbeelding van spotify albumcover) naar Instagram
/Clear: Maak de chat leeg
/Help: Laat dit menu zien"""
)
await run(interaction)
@client.tree.command(
name="instalogin",
description="Login to Instagram, multiple ways",
)
async def insta_login(interaction: discord.Interaction, name: str, password: str, tfa: str, sessionid: str):
@defer
@Checks.admin_only
@Checks.commands_channel_only
@confirm
@defer
async def run(interaction: discord.Interaction, name: str, password: str, tfa: str, sessionid: str):
with open("Credentials.txt", 'w') as file:
file.write(name + '\n')
file.write(password + '\n')
file.write(tfa + '\n')
file.write(sessionid + '\n')
await run(interaction, name, password, tfa, sessionid)
@client.tree.command(
name="clear",
description="Clear the chat",
)
async def clear(interaction: discord.Interaction):
@defer
@Checks.admin_only
@confirm
@defer
async def run(interaction: discord.Interaction):
await interaction.channel.purge()
await run(interaction)
@client.tree.command(
name="instapostimage",
description="Post to Instagram",
)
async def insta_post_image(interaction: discord.Interaction, image: discord.Attachment, type: Literal["Nummer van de maand", "New Music Friday"], nummer: str, artiest: str, caption: str, tag: Optional[str] = None):
@defer
#@Checks.admin_only
@Checks.commands_channel_only
async def run(interaction: discord.Interaction, image: discord.Attachment, type: Literal["Nummer van de maand", "New Music Friday"], nummer: str, artiest: str, caption: str):
# check if the attachment is an image
if not image.content_type.startswith("image"):
await interaction.followup.send("Dit is geen afbeelding")
return
tag = tag.lstrip("@")
if type == "Nummer van de maand":
image_bytes, output_file = cover_to_image(image.url, "NVDM.png")
elif type == "New Music Friday":
image_bytes, output_file = cover_to_image(image.url, "NMF.png")
else:
await interaction.followup.send("Er ging iets fout")
return
if image_bytes == None:
await interaction.followup.send(f"Er ging iets fout. Dit betreft {output_file}")
return
image_bytes, output_file = text_overlay({"Title": nummer, "Font": "Helvetica.ttf", "Size": 144, "Color": (245,247,196), "pos": (62,1530)}, {"Artist": artiest, "Font": "Helvetica.ttf", "Size": 96, "Color": (249,250,226), "pos": (62,1690)}, image_bytes, output_file)
interaction.followup.send("Dit is de foto", file=discord.File(image_bytes, filename=output_file))
@confirm_upload_instagram
async def confirm_upload(interaction: discord.Interaction):
commands_channel = discord.utils.get(interaction.guild.text_channels, name="commands")
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(None, insta.post, image_bytes, caption, tag)
await commands_channel.send(f"Foto geupload met link: {result}")
await confirm_upload(interaction)
await run(interaction, image, type, nummer, artiest, caption)
@client.tree.command(
name="instaposttext",
description="Post to Instagram",
)
async def insta_post_text(interaction: discord.Interaction, type: Literal["Nummer van de maand", "New Music Friday"], nummer: str, artiest: str, caption: str, tag: Optional[str] = None):
@defer
#@Checks.admin_only
@Checks.commands_channel_only
async def run(interaction: discord.Interaction, type: Literal["Nummer van de maand", "New Music Friday"], nummer: str, artiest: str, caption: str):
image_link = album_cover(nummer, artiest)
if not image_link:
await interaction.followup.send("Invalide nummer of artiest, mogelijk typefout")
return
if type == "Nummer van de maand":
image_bytes, output_file = cover_to_image(image_link, "NVDM.png")
elif type == "New Music Friday":
image_bytes, output_file = cover_to_image(image_link, "NMF.png")
else:
await interaction.followup.send("Er ging iets fout")
return
if image_bytes == None:
await interaction.followup.send(f"Er ging iets fout. Dit betreft {output_file}")
return
image_bytes, output_file = text_overlay({"Title": nummer, "Font": "Helvetica.ttf", "Size": 144, "Color": (245,247,196), "pos": (62,1530)}, {"Artist": artiest, "Font": "Helvetica.ttf", "Size": 96, "Color": (249,250,226), "pos": (62,1690)}, image_bytes, output_file)
await interaction.followup.send("Dit is de foto", file=discord.File(image_bytes, filename=output_file))
@confirm_upload_instagram
async def confirm_upload(interaction: discord.Interaction):
commands_channel = discord.utils.get(interaction.guild.text_channels, name="commands")
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(None, insta.post, image_bytes, caption, tag)
await commands_channel.send(f"Foto geupload met link: {result}")
await confirm_upload(interaction)
await run(interaction, type, nummer, artiest, caption)
client.run(TOKEN)