0% found this document useful (0 votes)
46 views4 pages

Discord Bot: Ephemeral Message Handling

This document is a Python script for a Discord bot using the Nextcord library. It includes functionality for managing starred messages in a starboard channel, sending messages with attachments, creating customizable embed messages, and impersonating other members. The bot is configured with specific user IDs allowed to use certain commands and handles dependencies installation for required libraries.

Uploaded by

SONIC BooM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Discord Bot: Ephemeral Message Handling

This document is a Python script for a Discord bot using the Nextcord library. It includes functionality for managing starred messages in a starboard channel, sending messages with attachments, creating customizable embed messages, and impersonating other members. The bot is configured with specific user IDs allowed to use certain commands and handles dependencies installation for required libraries.

Uploaded by

SONIC BooM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import subprocess

import sys
import os
import random
import string
import json
from io import BytesIO
import requests
import nextcord
from nextcord import Interaction, File
from nextcord.ext import commands
from nextcord import SlashOption

# Install required dependencies


try:
import requests
except ImportError:
print("Installing 'requests'...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])

try:
import aiohttp
except ImportError:
print("Installing 'aiohttp'...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "aiohttp"])

# Bot settings
ALLOWED_IDS = [1271345768665710667, 708605521065934900, 1251217809703305340,
1272007178441588779,
1226633929289760871, 1272261775659634721] # Replace with the IDs of
users allowed to use the command
STARBOARD_CHANNEL_ID = 1280296978655215616
STAR_EMOJI = "⭐"
STAR_THRESHOLD = 3
SERVER_ID = 1271929049761185832
MEME_CHANNEL_ID = 1280830092398039050

# Create the bot instance


intents = nextcord.Intents.default()
bot = commands.Bot(command_prefix="/", intents=intents)

# Maintain starred messages


starred_messages = {}

@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
await
bot.change_presence(activity=nextcord.Activity(type=nextcord.ActivityType.watching,
name="Humans"))

def create_embed(message, star_count):


embed = nextcord.Embed(description=message.content or "No content",
color=0xFFD700)
embed.set_author(name=message.author.display_name,
icon_url=message.author.avatar.url)
embed.add_field(name="Jump to message:", value=f"[Click
here](https://discord.com/channels/{message.guild.id}/{message.channel.id}/
{message.id})", inline=False)
embed.set_footer(text=f"⭐ {star_count}")

# Add the first attachment if it exists


if message.attachments:
embed.set_image(url=message.attachments[0].url)
return embed

@bot.event
async def on_raw_reaction_add(payload):
if payload.guild_id != SERVER_ID:
return

if payload.emoji.name == STAR_EMOJI:
channel = bot.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
starboard_channel = bot.get_channel(STARBOARD_CHANNEL_ID)

if message.id in starred_messages:
embed_message = await
starboard_channel.fetch_message(starred_messages[message.id])
star_count = next((r.count for r in message.reactions if r.emoji ==
STAR_EMOJI), 0)
if star_count >= STAR_THRESHOLD:
embed = embed_message.embeds[0]
embed.set_footer(text=f"⭐ {star_count}")
await embed_message.edit(embed=embed)
else:
await embed_message.delete()
del starred_messages[message.id]
else:
star_count = next((r.count for r in message.reactions if r.emoji ==
STAR_EMOJI), 0)
if star_count >= STAR_THRESHOLD:
embed = create_embed(message, star_count)
embed_message = await starboard_channel.send(embed=embed)
starred_messages[message.id] = embed_message.id

@bot.event
async def on_raw_reaction_remove(payload):
if payload.emoji.name == STAR_EMOJI:
channel = bot.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
starboard_channel = bot.get_channel(STARBOARD_CHANNEL_ID)

if message.id in starred_messages:
embed_message = await
starboard_channel.fetch_message(starred_messages[message.id])
star_count = next((r.count for r in message.reactions if r.emoji ==
STAR_EMOJI), 0)
if star_count >= STAR_THRESHOLD:
embed = embed_message.embeds[0]
embed.set_footer(text=f"⭐ {star_count}")
await embed_message.edit(embed=embed)
else:
await embed_message.delete()
del starred_messages[message.id]

@bot.slash_command(name="send", description="Send a message as the bot, with


multiple attachments.")
async def send_message(
interaction: Interaction,
message: str = SlashOption(description="The message to send."),
attachments: list[nextcord.Attachment] = SlashOption(description="File
attachments.", required=False, default=[]),
urls: str = SlashOption(description="Comma-separated image URLs.",
required=False, default=None)
):
if interaction.user.id not in ALLOWED_IDS:
await interaction.response.send_message("You are not allowed to use this
command.", ephemeral=True)
return

formatted_message = message.replace("\\n", "\n") # Handle newlines


files = []

# Process attachments uploaded as files


for attachment in attachments:
file = await attachment.to_file()
files.append(file)

# Process URLs provided


if urls:
url_list = urls.split(",")
for url in url_list:
try:
response = requests.get(url.strip())
if response.status_code == 200:
file_data = BytesIO(response.content)
filename = url.strip().split("/")[-1]
files.append(File(file_data, filename=filename))
else:
await interaction.response.send_message(f"Failed to download
image from URL: {url.strip()}", ephemeral=True)
except Exception as e:
await interaction.response.send_message(f"Error downloading image
from URL {url.strip()}: {e}", ephemeral=True)

# Send the message with all files attached


if files:
await interaction.channel.send(content=formatted_message, files=files)
await interaction.response.send_message("Message sent with attachments!",
ephemeral=True)
else:
await interaction.channel.send(formatted_message)
await interaction.response.send_message("Message sent!", ephemeral=True)

# Additional commands

@bot.slash_command(name="send_embed", description="Send a customizable embed


message as the bot.")
async def send_embed(
interaction: Interaction,
title: str,
description: str,
colour: str = "#773303",
footer: str = ""
):
if interaction.user.id not in ALLOWED_IDS:
await interaction.response.send_message("You are not allowed to use this
command.", ephemeral=True)
return

if not (colour.startswith("#") and len(colour) == 7 and all(c in


"0123456789abcdefABCDEF" for c in colour[1:])):
colour = "#dfeef5"

try:
embed_color = nextcord.Color(int(colour.lstrip("#"), 16))
except ValueError:
embed_color = nextcord.Color.from_rgb(223, 238, 245)
formatted_description = description.replace("\\n", "\n")

embed = nextcord.Embed(
title=title,
description=formatted_description,
color=embed_color
)
if footer:
embed.set_footer(text=footer)

await interaction.response.send_message("Embed sent!", ephemeral=True)


await interaction.channel.send(embed=embed)

@bot.slash_command(description="Impersonate a member")
async def impersonate(
interaction: Interaction,
person: nextcord.Member = SlashOption(description="Select a member to
impersonate", required=True),
message: str = SlashOption(description="Message to send as the member",
required=True)
):
if interaction.user.id not in ALLOWED_IDS:
await interaction.response.send_message("You are not allowed to use this
command.", ephemeral=True)
return

display_name = person.global_name if isinstance(person, nextcord.Member) and


person.global_name else person.name
display_name = display_name.split('#')[0]

channel = interaction.channel
await interaction.response.send_message(f"Sending message as
{display_name}...", ephemeral=True)
avatar = await person.avatar.read() if person.avatar else None
webhook = await channel.create_webhook(name=display_name, avatar=avatar)
await webhook.send(content=message)
await webhook.delete()

# More commands like dox, insult, compliment, etc. remain unchanged here

bot.run("YOUR_BOT_TOKEN")

You might also like