Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

Create a simple C# Discord bot, that writes all received messages to a text file.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o4-mini-2025-04-16’s answer.
  1. User

    Create a simple C# Discord bot, that writes all received messages to a text file.

  2. Claude Assistant

    I'll help you create a simple C# Discord bot that logs all messages to a text file. Here's a complete implementation using Discord.Net:

    First, you'll need to install the Discord.Net NuGet package. You can do this via Package Manager Console:

    Install-Package Discord.Net
    

    Here's the bot code:

    Program.cs:

    using Discord;
    using Discord.WebSocket;
    using System;
    using System.IO;
    using System.Threading.Tasks;
    
    namespace DiscordMessageLogger
    {
        class Program
        {
            private DiscordSocketClient _client;
            private readonly string _logFilePath = "discord_messages.txt";
            private readonly object _fileLock = new object();
    
            static void Main(string[] args)
                => new Program().MainAsync().GetAwaiter().GetResult();
    
            public async Task MainAsync()
            {
                _client = new DiscordSocketClient(new DiscordSocketConfig
                {
                    GatewayIntents = GatewayIntents.Guilds | 
                                     GatewayIntents.GuildMessages | 
                                     GatewayIntents.MessageContent
                });
    
                _client.Log += LogAsync;
                _client.MessageReceived += MessageReceivedAsync;
    
                // Replace with your bot token
                string token = "YOUR_BOT_TOKEN_HERE";
    
                await _client.LoginAsync(TokenType.Bot, token);
                await _client.StartAsync();
    
                // Block this task until the program is closed
                await Task.Delay(-1);
            }
    
            private Task LogAsync(LogMessage log)
            {
                Console.WriteLine(log.ToString());
                return Task.CompletedTask;
            }
    
            private async Task MessageReceivedAsync(SocketMessage message)
            {
                // Ignore messages from bots (including itself)
                if (message.Author.IsBot) return;
    
                try
                {
                    string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] " +
                                      $"Server: {((SocketGuildChannel)message.Channel).Guild.Name} | " +
                                      $"Channel: #{message.Channel.Name} | " +
                                      $"User: {message.Author.Username}#{message.Author.Discriminator} | " +
                                      $"Message: {message.Content}\n";
    
                    // Thread-safe file writing
                    lock (_fileLock)
                    {
                        File.AppendAllText(_logFilePath, logEntry);
                    }
    
                    Console.WriteLine($"Logged message from {message.Author.Username}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error logging message: {ex.Message}");
                }
    
                await Task.CompletedTask;
            }
        }
    }
    

    To set up and use this bot:

    1. Create a Discord Application and Bot:

    2. Configure Bot Permissions:

      • In the Discord Developer Portal, go to OAuth2 > URL Generator
      • Select "bot" under Scopes
      • Select these permissions: "Read Messages/View Channels" and "Read Message History"
      • Use the generated URL to invite your bot to your server
    3. Update the Code:

      • Replace "YOUR_BOT_TOKEN_HERE" with your actual bot token
      • Optionally change the _logFilePath to specify where to save the log file
    4. Important Notes:

      • The bot needs the "Message Content Intent" enabled in the Discord Developer Portal (Bot section)
      • Messages are appended to the text file, so it will grow over time
      • The file writing is thread-safe to prevent corruption
      • Bot messages are ignored to prevent potential loops

    The log file format will look like this:

    [2025-07-11 14:30:45] Server: My Discord Server | Channel: #general | User: JohnDoe#1234 | Message: Hello everyone!
    [2025-07-11 14:31:02] Server: My Discord Server | Channel: #general | User: JaneSmith#5678 | Message: Hey there!
    

    This bot will run continuously and log all messages from all servers it's invited to. Make sure to handle the bot token securely and never commit it to version control.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.