by Lazy Sloth
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)
Frequently Asked Questions
How can this Discord Bot for YouTube Search benefit my business?
The Discord Bot for YouTube Search can significantly enhance your business's social media strategy by automatically sharing relevant YouTube content with your community. It keeps your Discord channel active with fresh, targeted content, potentially increasing engagement and fostering discussions around topics important to your brand or industry.
Can I use this bot to monitor competitor content or industry trends?
Absolutely! The Discord Bot for YouTube Search is versatile and can be configured to track various keywords or hashtags. By setting the YOUTUBE_SEARCH_QUERY
and TITLE_KEYWORD
environment variables, you can monitor competitor content, industry trends, or specific topics relevant to your business. This can provide valuable insights and help you stay ahead in your market.
How can I customize the bot's search criteria for my specific needs?
The Discord Bot for YouTube Search allows for customization through environment variables. You can adjust the YOUTUBE_SEARCH_QUERY
to broaden or narrow your search, and use the TITLE_KEYWORD
to filter results further. For example:
python
YOUTUBE_SEARCH_QUERY = "digital marketing strategies 2024"
TITLE_KEYWORD = "SEO"
This configuration would search for videos about digital marketing strategies in 2024, but only post those that specifically mention SEO in the title.
Is it possible to change how often the bot checks for new videos?
Yes, you can easily modify the frequency of checks. In the Discord Bot for YouTube Search template, there's a line that sets the check interval:
python
@tasks.loop(minutes=60) # Check for new videos every hour
You can change the minutes=60
to any value you prefer. For example, to check every 30 minutes:
python
@tasks.loop(minutes=30) # Check for new videos every 30 minutes
Remember that more frequent checks might impact your YouTube API quota usage.
How can I expand this bot's functionality for more comprehensive social media management?
The Discord Bot for YouTube Search provides a solid foundation that can be expanded in several ways:
Created: | Last Updated:
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.
Template Benefits
-
Real-time Content Monitoring: Businesses can stay up-to-date with the latest YouTube content related to their industry, competitors, or brand by automatically tracking and sharing relevant videos as they're published.
-
Improved Team Communication: By posting new videos to a Discord channel, this bot ensures that all team members have immediate access to the latest video content, fostering better information sharing and collaboration.
-
Competitive Intelligence: Companies can use this bot to monitor competitors' YouTube activities, gaining insights into their marketing strategies, product launches, and industry trends.
-
Content Curation for Social Media: Social media managers can leverage this tool to quickly identify and share relevant content across other platforms, enhancing their social media presence and engagement.
-
Market Research Automation: Researchers and marketers can automate the process of gathering video content related to specific topics or keywords, saving time and ensuring comprehensive data collection for market analysis.