Discord Bot for Youtube Search

 
import os
import discord
from datetime import datetime
from discord.ext import commands, tasks
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

# Environment variables
DISCORD_BOT_TOKEN = os.environ['DISCORD_BOT_TOKEN']
DISCORD_CHANNEL_ID = os.environ['DISCORD_CHANNEL_ID']
YOUTUBE_API_KEY = os.environ['YOUTUBE_API_KEY']
YOUTUBE_SEARCH_QUERY = os.environ['YOUTUBE_SEARCH_QUERY']
TITLE_KEYWORD = os.environ['TITLE_KEYWORD'].lower()  # Environment variable for title keyword, converted to lowercase

# Global variable
PUBLISHED_AFTER = "2024-01-01T00:00:00Z"

# Initialize the Discord bot
bot = commands.Bot(command_prefix='!')

# Function to create YouTube API client
def youtube_api_client():
    return build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)

About this template

A Discord bot that searches and posts new YouTube videos to a discord channel hourly based on a specific tag or keyword.

Introduction to the Discord Bot for YouTube Search Template

Welcome to the Discord Bot for YouTube Search template! This template allows you to create a Discord bot that automatically searches for new YouTube videos based on a specific keyword or tag and posts them to a Discord channel on an hourly basis. This is perfect for communities that want to stay updated on the latest content related to their interests.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will initialize the template within the Lazy platform, pre-populating the code in the Lazy Builder interface.

Initial Setup: Adding Environment Secrets

Before you can use the bot, you'll need to set up some environment secrets within the Lazy Builder. These secrets include:

  • DISCORD_BOT_TOKEN: Your Discord bot token, which you can obtain from the Discord Developer Portal.
  • DISCORD_CHANNEL_ID: The ID of the Discord channel where the bot will post messages.
  • YOUTUBE_API_KEY: Your YouTube API key, which you can get from the Google Cloud Console.
  • YOUTUBE_SEARCH_QUERY: The search query the bot will use to find new videos on YouTube.
  • TITLE_KEYWORD: A keyword that must be present in the video title for it to be posted to your Discord channel.

To set these up, navigate to the Environment Secrets tab within the Lazy Builder and enter the corresponding values for each secret.

Test: Pressing the Test Button

Once you have configured the environment secrets, press the "Test" button to deploy the app. The Lazy CLI will handle the deployment, and you won't need to worry about installing libraries or setting up your environment.

Entering Input: Filling in User Input

There is no user input required through the CLI for this template, as all necessary configurations are handled through environment secrets.

Using the App

After pressing the "Test" button and deploying the app, the bot will start running on the Lazy platform. It will automatically check for new YouTube videos every hour and post them to the specified Discord channel if they match the search query and title keyword criteria.

Integrating the App

To integrate the bot into your Discord server, you will need to invite the bot using the Discord Developer Portal. Here are the steps to do so:

  • Go to the Discord Developer Portal and navigate to your bot's page.
  • Under the "OAuth2" tab, select the "bot" scope to generate an invite link.
  • Choose the permissions your bot will need, such as "Send Messages" and "Read Message History."
  • Use the generated link to invite the bot to your Discord server.

Once the bot is part of your server and the app is running on Lazy, it will begin its hourly search for new videos and share them in the designated channel.

If you need further assistance or have any questions, the Lazy platform provides support to help you get the most out of your new Discord bot for YouTube searches.

Category
Technology
Last published
July 27, 2024

More templates like this

SecureUserAuthenticator

Develop a secure User Authentication system for users to register, log in, and manage their profiles, laying the foundation for user-specific data management and permissions in the CMS.

Laravel
Python
Flask
Javascript

Simple Multiplayer Telegram game

This app is a simple frontend for a game where users can upvote and downvote the most popular word in their country, learn about the flags of other countries, and view what other people voted for on a leaderboard.

Telegram
Python
Javascript

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask
Home
/
Discord Bot for Youtube Search