The Grizzly Gazette

The Grizzly Gazette discord server now runs the worst microblogging service of all time

Article written by kami
Heya!
Ava recently had the idea to make a channel in the gazette discord where every message automatically gets posted on a website.
So, i went and implemented it. Introducing sg.kamiscorner.xyz, also known as chat surgery!

Everytime someone posts a message in the #auto-blogging chat in the gazette discord, it automatically gets added to the site. Without any input validation whatsoever. We already have an alert(1) about two minutes into the sites existence, and suliman changed the background to be piss-colored.

Anyways, how'd i do it?
Fairly simple, we've got a discord bot that listens for new messages and writes them to a file. I've got this bot running in the background at all times on a server i own. I've also got a webserver running php, which reads that file and puts the contents on the website. That's it. Chat surgery.

Here's the source code!

The python bot:

import discord
import csv
import os

# Define your bot's token and the channel ID you're interested in
TOKEN = ''  # Replace with your bot's token
CHANNEL_ID = 0  # Replace with the channel ID you want to monitor

# CSV file path
CSV_FILE = 'messages.csv'

intents = discord.Intents.default()
intents.message_content = True  # Enable content intent to read messages

# Set up the bot client
client = discord.Client(intents=intents)

# Create the CSV file if it doesn't exist
if not os.path.exists(CSV_FILE):
    with open(CSV_FILE, 'w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(['Author', 'Message', 'Timestamp'])  # Header row

# Function to write messages to the CSV file
def write_to_csv(author, message, timestamp):
    with open(CSV_FILE, 'a', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow([author, message, timestamp])

# Event when the bot is ready
@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

# Event when a new message is received
@client.event
async def on_message(message):
    # Ignore messages from the bot itself
    if message.author == client.user:
        return

    # Check if the message is from the specific channel
    if message.channel.id == CHANNEL_ID:
        # Write the message details to the CSV
        write_to_csv(message.author.name, message.content, message.created_at)

        print(f"Message from {message.author.name}: {message.content} saved to CSV.")

# Start the bot
client.run(TOKEN)  

The singular index.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>chat.surgery</title>
</head>
<body>
    <h1>chat.surgery</h1>
    <?php
        $lines = [];
        $file = fopen('messages.csv', 'r');
        while (($line = fgetcsv($file)) !== FALSE) {
            if($line[1] != "Message") {
                $lines[] = $line[1]; // store messages in array
            }
        }
        fclose($file);

        // loop through array in reverse order
        for ($i = count($lines) - 1; $i >= 0; $i--) {
            echo "<p>{$lines[$i]}</p>";
        }
    ?>
</body>
</html>

I might make a more-indepth technical explanation on how to set up the kvm for all of this at some point, but i feel like this is clear enough that anyone who for some reason wants to subject themselves to hosting this can do it.

#guide #indieweb #kami