import discord
from discord.ext import commands
class ImageLoggerBot(commands.Bot):
"""
Discord bot that logs images sent in a server.
Attributes:
- token: str
The bot token required to authenticate and connect to the Discord API.
- log_channel_id: int
The ID of the channel where the bot will log the images.
"""
def __init__(self, token: str, log_channel_id: int):
"""
Constructor to instantiate the ImageLoggerBot class.
Parameters:
- token: str
The bot token required to authenticate and connect to the Discord API.
- log_channel_id: int
The ID of the channel where the bot will log the images.
"""
super().__init__(command_prefix="!")
self.token = token
self.log_channel_id = log_channel_id
async def on_ready(self):
"""
Event handler that is triggered when the bot is ready and connected to the
Discord API.
"""
print(f"Logged in as {self.user.name} ({self.user.id})")
print("Bot is ready to log images!")
async def on_message(self, message):
"""
Event handler that is triggered when a message is sent in a server.
Parameters:
- message: discord.Message
The message object that contains the information about the sent
message.
"""
# Check if the message has any attachments (images)
if message.attachments:
# Get the log channel where the images will be logged
log_channel = self.get_channel(self.log_channel_id)
# Log the images in the log channel
for attachment in message.attachments:
await log_channel.send(f"Image logged from {message.author.name}:
{attachment.url}")
await self.process_commands(message)
def run_bot(self):
"""
Method to start the bot and connect to the Discord API.
"""
self.run(self.token)
# Example usage of the ImageLoggerBot class:
# Create an instance of the bot with your bot token and the log channel ID
bot = ImageLoggerBot("YOUR_BOT_TOKEN", 1234567890)
# Run the bot
bot.run_bot()