Skip to content

Commit bdece9f

Browse files
committed
added optin/out for users (global) and changed delete mechanism
1 parent 6b63f45 commit bdece9f

File tree

1 file changed

+136
-19
lines changed

1 file changed

+136
-19
lines changed

src/Bot/discord.py

+136-19
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
# Set up logging
2323
logger = logging.getLogger('nextcord')
2424
logger.setLevel(logging.DEBUG)
25-
handler = logging.FileHandler(filename='nextcord.log', encoding='utf-8', mode='w')
25+
date = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
26+
handler = logging.FileHandler(filename=f'nextcord-{date}.log', encoding='utf-8', mode='w')
2627
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
2728
logger.addHandler(handler)
2829

2930
# Set up intents and create the bot client
30-
intents = nextcord.Intents.all()
31+
intents = nextcord.Intents.default()
32+
intents.message_content = True
3133
client = commands.Bot(command_prefix="a!", intents=intents)
3234

3335
# Create an aiohttp session
@@ -103,6 +105,11 @@ async def on_message(message: nextcord.Message):
103105
async with session.get(f"https://api.astroid.cc/{message.guild.id}?token={config.MASTER_TOKEN}") as discord_channel_request:
104106
discord_channel = await discord_channel_request.json()
105107
if str(message.channel.id) in discord_channel.get("config").get("channels").get("discord"):
108+
async with session.get(f"https://api.astroid.cc/optout/{message.author.id}?token={config.MASTER_TOKEN}") as optout_request:
109+
optout = await optout_request.json()
110+
if optout.get("optedOut"):
111+
return
112+
106113
global blacklist
107114
# Get blacklist information from the API
108115
async with session.get(f"https://api.astroid.cc/{message.guild.id}?token={config.MASTER_TOKEN}") as blacklist_request:
@@ -188,9 +195,85 @@ async def on_message_delete(message: nextcord.Message):
188195
await client.get_channel(int(log_json["config"]["logs"]["discord"])).send(embed=embed)
189196

190197

198+
@client.slash_command(name="opt", description="Set your opt-status", force_global=True)
199+
async def opt(interaction: nextcord.Interaction, status: str):
200+
pass
201+
202+
@opt.subcommand(name="in", description="Opt into Astroid to let your messages be bridged")
203+
async def opt_in(o_interaction: nextcord.Interaction):
204+
embed = nextcord.Embed(title="Opting in?", description="If you opt into Astroid __your messages will be bridged to bridged__ servers.\nThis is global for **all servers** using Astroid.", color=0x3D60FF)
205+
class ExternalLinkBtn(nextcord.ui.Button):
206+
def __init__(self):
207+
super().__init__(style=nextcord.ButtonStyle.link, label="Privacy", url="https://astroid.cc/privacy")
208+
209+
class ConfirmBtn(nextcord.ui.Button):
210+
def __init__(self):
211+
super().__init__(style=nextcord.ButtonStyle.success, label="Confirm", custom_id="opt_in", emoji="✅")
212+
213+
async def callback(self, interaction: nextcord.Interaction):
214+
async with session.post(f"https://api.astroid.cc/optin/{interaction.user.id}?token={config.MASTER_TOKEN}") as r:
215+
r_json = await r.json()
216+
if r_json["optedOut"] == False:
217+
await interaction.response.send_message("Opted in successfully.", ephemeral=True)
218+
else:
219+
await interaction.response.send_message(f"Could not opt in. ({r.status, r.reason})", ephemeral=True)
220+
221+
class CancelBtn(nextcord.ui.Button):
222+
def __init__(self):
223+
super().__init__(style=nextcord.ButtonStyle.danger, label="Cancel", custom_id="opt_cancel", emoji="❌")
224+
225+
async def callback(self, interaction: nextcord.Interaction):
226+
await o_interaction.delete_original_message()
227+
await interaction.response.send_message("Cancelled.", ephemeral=True)
228+
229+
class Buttons(nextcord.ui.View):
230+
def __init__(self):
231+
super().__init__()
232+
self.add_item(ExternalLinkBtn())
233+
self.add_item(ConfirmBtn())
234+
self.add_item(CancelBtn())
235+
236+
await o_interaction.response.send_message(embed=embed, view=Buttons(), ephemeral=True)
237+
238+
239+
@opt.subcommand(name="out", description="Opt out of Astroid to stop your messages from being bridged")
240+
async def opt_out(o_interaction: nextcord.Interaction):
241+
embed = nextcord.Embed(title="Opting out?", description="If you opt out of Astroid __your messages will not be bridged__ to bridged servers.\nThis is global for **all servers** using Astroid.", color=0x3D60FF)
242+
class ExternalLinkBtn(nextcord.ui.Button):
243+
def __init__(self):
244+
super().__init__(style=nextcord.ButtonStyle.link, label="Privacy", url="https://astroid.cc/privacy")
245+
246+
class ConfirmBtn(nextcord.ui.Button):
247+
def __init__(self):
248+
super().__init__(style=nextcord.ButtonStyle.success, label="Confirm", custom_id="opt_out", emoji="✅")
249+
250+
async def callback(self, interaction: nextcord.Interaction):
251+
async with session.post(f"https://api.astroid.cc/optout/{interaction.user.id}?token={config.MASTER_TOKEN}") as r:
252+
r_json = await r.json()
253+
if r_json["optedOut"] == True:
254+
await interaction.response.send_message("Opted out successfully.", ephemeral=True)
255+
else:
256+
await interaction.response.send_message(f"Could not opt out. ({r.status, r.reason})", ephemeral=True)
257+
258+
class CancelBtn(nextcord.ui.Button):
259+
def __init__(self):
260+
super().__init__(style=nextcord.ButtonStyle.danger, label="Cancel", custom_id="opt_cancel", emoji="❌")
261+
262+
async def callback(self, interaction: nextcord.Interaction):
263+
await o_interaction.delete_original_message()
264+
await interaction.response.send_message("Cancelled.", ephemeral=True)
265+
266+
class Buttons(nextcord.ui.View):
267+
def __init__(self):
268+
super().__init__()
269+
self.add_item(ExternalLinkBtn())
270+
self.add_item(ConfirmBtn())
271+
self.add_item(CancelBtn())
272+
273+
await o_interaction.response.send_message(embed=embed, view=Buttons(), ephemeral=True)
191274

192275
# Slash command to register the server
193-
@client.slash_command(name="register", default_member_permissions=8, description="Welcome! You are one step away from bridging your server")
276+
@client.slash_command(name="register", default_member_permissions=8, description="Welcome! You are one step away from bridging your server", force_global=True)
194277
async def register(interaction: nextcord.Interaction):
195278
await interaction.response.send_message("Starting registering process.. (This may take a while.)", ephemeral=True)
196279
try:
@@ -220,7 +303,7 @@ async def register(interaction: nextcord.Interaction):
220303
traceback.print_exc()
221304

222305
# Slash command to add another channel for bridging
223-
@client.slash_command(name="add-bridge", description="More than one channel huh? Add more with this command", default_member_permissions=8)
306+
@client.slash_command(name="add-bridge", description="More than one channel huh? Add more with this command", default_member_permissions=8, force_global=True)
224307
async def add_bridge(interaction: nextcord.Interaction):
225308
await interaction.response.send_message("Adding bridge..", ephemeral=True)
226309
# Create a webhook for astroid
@@ -233,17 +316,28 @@ async def add_bridge(interaction: nextcord.Interaction):
233316
await interaction.edit_original_message(content=r.json().get("message"))
234317

235318
# Slash command to display help information
236-
@client.slash_command(name="help", description="Help needed? This command will show you all the commands available")
319+
@client.slash_command(name="help", description="Help needed? This command will show you all the commands available", force_global=True)
237320
async def help(interaction: nextcord.Interaction):
238321
embed = nextcord.Embed(title="Astroid", description="First time here? To get started we recommend you our **User Guide: https://docs.astroid.cc ** \n\nTo use any of the command below you need **Administrator permissions**.", color=0x3D60FF)
239-
class ExternalLinkBtn(nextcord.ui.Button):
322+
class UserGuideExternalLinkBtn(nextcord.ui.Button):
240323
def __init__(self):
241324
super().__init__(style=nextcord.ButtonStyle.link, label="User Guide", url="https://docs.astroid.cc")
325+
326+
class TermsExternalLinkBtn(nextcord.ui.Button):
327+
def __init__(self):
328+
super().__init__(style=nextcord.ButtonStyle.link, label="Terms of Service", url="https://astroid.cc/terms")
329+
330+
class PrivacyExternalLinkBtn(nextcord.ui.Button):
331+
def __init__(self):
332+
super().__init__(style=nextcord.ButtonStyle.link, label="Privacy Policy", url="https://astroid.cc/privacy")
242333

243334
class ExternalLink(nextcord.ui.View):
244335
def __init__(self):
245336
super().__init__()
246-
self.add_item(ExternalLinkBtn())
337+
self.add_item(UserGuideExternalLinkBtn())
338+
self.add_item(TermsExternalLinkBtn())
339+
self.add_item(PrivacyExternalLinkBtn())
340+
247341

248342
view = ExternalLink()
249343
embed.add_field(name="register", value="Register you server.", inline=False)
@@ -252,21 +346,44 @@ def __init__(self):
252346
embed.add_field(name="allow", value="Allow a bot or webhook to be forwarded.", inline=False)
253347
embed.add_field(name="set-log", value="Setup your logchannel.", inline=False)
254348
embed.add_field(name="add-bridge", value="Add another channel to bridge over.\nNote: Works like `register`", inline=False)
255-
await interaction.response.send_message(embed=embed, ephemeral=True, view=view)
349+
embed.add_field(name="opt", value="Set your opt-status. This is globally set for **all servers** using Astroid. Changable anytime.\n" \
350+
"**in**: Opt into Astroid to allow your messages being bridged.\n" \
351+
"**out**: opt out of Astroid. None of you messages will be picked up.", inline=False)
352+
await interaction.response.send_message(embed=embed, view=view)
256353

257354
# Slash command to delete the endpoint
258-
@client.slash_command(name="delete", default_member_permissions=8, description="You want to delete your endpoint? This command will do it for you.")
355+
@client.slash_command(name="delete", default_member_permissions=8, description="You want to delete your endpoint? This command will do it for you.", force_global=True)
259356
async def delete(interaction: nextcord.Interaction):
260-
await interaction.response.send_message("Deleting your endpoint..", ephemeral=True)
261-
# Delete the endpoint in the API
262-
async with session.delete(f"https://api.astroid.cc/delete/{interaction.guild.id}?token={config.MASTER_TOKEN}") as r:
263-
if r.ok:
264-
await interaction.edit_original_message(content="Deleted endpoint.")
265-
else:
266-
await interaction.edit_original_message(content=await r.json().get("message"))
357+
embed = nextcord.Embed(title="Are you sure?", description="This action is irreversible. Are you sure you want to delete your endpoint?", color=0x3D60FF)
358+
class ConfirmBtn(nextcord.ui.Button):
359+
def __init__(self):
360+
super().__init__(style=nextcord.ButtonStyle.success, label="Confirm", custom_id="delete_confirm", emoji="🗑️")
361+
362+
async def callback(self, interaction: nextcord.Interaction):
363+
await interaction.response.send_message("Deleting your endpoint..", ephemeral=True)
364+
# Delete the endpoint in the API
365+
async with session.delete(f"https://api.astroid.cc/delete/{interaction.guild.id}?token={config.MASTER_TOKEN}") as r:
366+
if r.ok:
367+
await interaction.edit_original_message(content="Deleted endpoint.")
368+
else:
369+
await interaction.edit_original_message(content=await r.json().get("message"))
370+
class CancelBtn(nextcord.ui.Button):
371+
def __init__(self):
372+
super().__init__(style=nextcord.ButtonStyle.danger, label="Cancel", custom_id="delete_cancel", emoji="❌")
373+
374+
async def callback(self, interaction: nextcord.Interaction):
375+
await interaction.response.send_message("Cancelled.", ephemeral=True)
376+
377+
class Buttons(nextcord.ui.View):
378+
def __init__(self):
379+
super().__init__()
380+
self.add_item(ConfirmBtn())
381+
self.add_item(CancelBtn())
382+
383+
await interaction.response.send_message(embed=embed, view=Buttons(), ephemeral=True)
267384

268385
# Slash command to set the log channel
269-
@client.slash_command(name="set-log", default_member_permissions=8, description="Messages will be logged in this channel")
386+
@client.slash_command(name="set-log", default_member_permissions=8, description="Messages will be logged in this channel", force_global=True)
270387
async def setlog(interaction: nextcord.Interaction, channel: nextcord.TextChannel):
271388
await interaction.response.send_message("Setting logchannel..", ephemeral=True)
272389
# Update the log channel in the API
@@ -277,7 +394,7 @@ async def setlog(interaction: nextcord.Interaction, channel: nextcord.TextChanne
277394
await interaction.edit_original_message(content=r.json().get("message"))
278395

279396
# Slash command to allow a webhook or bot
280-
@client.slash_command(name="allow", default_member_permissions=8, description="Allow a webhook or bot to be forwarded")
397+
@client.slash_command(name="allow", default_member_permissions=8, description="Allow a webhook or bot to be forwarded", force_global=True)
281398
async def allow(interaction: nextcord.Interaction, id):
282399
await interaction.response.send_message(f"Adding {id} to allowlist..", ephemeral=True)
283400
# Update the allowed IDs in the API
@@ -288,7 +405,7 @@ async def allow(interaction: nextcord.Interaction, id):
288405
await interaction.edit_original_message(content=r.json().get("message"))
289406

290407
# Slash command to generate a new API token
291-
@client.slash_command(name="generate-token", default_member_permissions=8, description="Lost, forgot or leaked your token? Generate a new one")
408+
@client.slash_command(name="generate-token", default_member_permissions=8, description="Lost, forgot or leaked your token? Generate a new one", force_global=True)
292409
async def gen(interaction: nextcord.Interaction):
293410
await interaction.response.send_message("Generating token..", ephemeral=True)
294411
# Request a new token from the API

0 commit comments

Comments
 (0)