Creating a C# Discord Bot to alert server members about new YouTube uploads

Hey everyone! I’ve got my Discord bot running in C#, and now I’m working on making it alert my server whenever I drop a new video on YouTube. I’ve managed to get the basic functions up, but I’m stuck on linking it with YouTube for notifications. Any advice or sample code would be really appreciated!

Below is a revised code snippet I created to get things started:

using Discord;
using Discord.WebSocket;

public class BotController
{
    private DiscordSocketClient client;

    public async Task InitializeBot()
    {
        client = new DiscordSocketClient();
        client.MessageReceived += ProcessMessage;

        await client.LoginAsync(TokenType.Bot, "insert-your-token-here");
        await client.StartAsync();

        // Runs indefinitely
        await Task.Delay(-1);
    }

    private async Task ProcessMessage(SocketMessage msg)
    {
        if (msg.Content == "!ping")
        {
            await msg.Channel.SendMessageAsync("Pong!");
        }
    }
}

Thanks a lot for your help!

For your YouTube notification system, I’d suggest implementing a webhook approach. Instead of periodically checking for new uploads, you can set up a webhook with YouTube’s PubSubHubbub protocol. This way, YouTube will send a notification to your server whenever a new video is uploaded.

To implement this, you’ll need to create an endpoint in your application that YouTube can send POST requests to. When a notification is received, your bot can then send a message to the designated Discord channel.

This method is more efficient and real-time compared to polling. It also helps you avoid hitting API rate limits. Remember to secure your webhook endpoint and validate incoming requests to ensure they’re genuinely from YouTube.

Regarding your Discord bot code, consider using a command handler for better organization as your bot grows in complexity.

hey aroberts, cool project! for youtube notifs, check out the YouTube Data API. you’ll need to set up OAuth 2.0 and use the API to fetch new vids periodically. then u can send alerts to discord when theres new content. good luck with ur bot!

I’ve tackled a similar project before, and I can tell you it’s quite rewarding once you get it working. For YouTube integration, I’d recommend looking into Google.Apis.YouTube.v3 NuGet package. It simplifies working with the YouTube API significantly.

One approach I found effective was setting up a background task that periodically checks for new uploads. You can use a PeriodicTimer for this in .NET 6+. Store the last checked video ID somewhere (maybe in a local file) and compare it with the latest upload each time.

For the Discord part, consider creating an embed with video details - it looks much nicer than plain text. Don’t forget to handle API quota limits and potential network issues robustly. OAuth setup can be tricky, so take your time with that part.

Hope this helps point you in the right direction!