-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ravinder-chadha/main
basic setup
- Loading branch information
Showing
8 changed files
with
637 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,38 @@ | ||
# social-media-bot | ||
# Social Media Bots | ||
## Bhagavad Gita Discord Bot | ||
--- | ||
This is Discord bot that sends messages,verses on schedule to specific time, user can get specific verse, random verse etc. | ||
|
||
## Setup | ||
--- | ||
<img src="https://img.shields.io/badge/Before%20use%20one%20must%20read-LICENSE%20File-orange"><br> | ||
|
||
### For using our bot | ||
1. Yet to be hosted | ||
--- | ||
### For making similar bot you must | ||
1. Go through the instructions in [discord developer portal](https://discord.com/developers/docs/intro) and create own discord bot. | ||
|
||
> Don't forget to give the bot permission `to read and write messages`! | ||
2. Download and install the the dependencies and requirements mention in `pyproject.toml` or `requirements.txt` file | ||
|
||
> After installing you may clone or fork the code and change it according to your use, *comments in the code will definitely help you to change the code* | ||
3. Last step is to either host the `social-media-bot/tests/__init__.py` file to make bot live 24/7 or for trial you may just run it locally. | ||
--- | ||
|
||
## About the bot | ||
<img src="https://img.shields.io/badge/Name-Bhagad%20Gita%20Bot-red"><br> | ||
<img src="https://img.shields.io/badge/API%20Used%20From-BhagavadGita.io-yellow"><br> | ||
<img src="https://img.shields.io/badge/Availablity-24%2F7-green"><br> | ||
<img src="https://img.shields.io/badge/Hosted%20on-%3F-blue"><br> | ||
|
||
## List of Commands and their uses | ||
<img src="https://img.shields.io/badge/hello-Greetings%20from%20bot-d902ee"><br> | ||
<img src="https://img.shields.io/badge/bye-Greetings%20from%20bot-ffd79d"><br> | ||
<img src="https://img.shields.io/badge/random-Get%20random%20verse%20from%20bot-f162ff"><br> | ||
<img src="https://img.shields.io/badge/verse{chapterNumber}.{verseNumber-Get%20specific%20verse%20from%20bot-320d3e"><br> | ||
|
||
Visit for more help or information! | ||
## [BhagavadGita.io](https://bhagavadgita.io/) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from asyncio.tasks import sleep | ||
import discord | ||
import random | ||
import requests | ||
import json | ||
import os | ||
from datetime import datetime, time, timedelta | ||
import asyncio | ||
from discord.ext import tasks, commands | ||
from requests.api import get | ||
|
||
TOKEN = os.environ.get("TOKEN") # token saved in environment variable locally | ||
# Api-key saved in environment variable locally | ||
Rapidapi = os.environ.get("RAPIDAPI") | ||
client = discord.Client() | ||
|
||
|
||
def get_verse(x, y): | ||
url = f"https://bhagavad-gita3.p.rapidapi.com/v2/chapters/{x}/verses/{y}/" | ||
headers = { | ||
'x-rapidapi-key': Rapidapi, | ||
'x-rapidapi-host': "bhagavad-gita3.p.rapidapi.com" | ||
} | ||
response = requests.request("GET", url, headers=headers).text | ||
# print(response.text) | ||
json_data = json.loads(response) | ||
verse = json_data["translations"][1]['description'] | ||
# print(response) | ||
return(verse) | ||
|
||
|
||
@client.event | ||
async def on_ready(): | ||
print('We have logged in as {0.user}'.format(client)) | ||
channel = client.get_channel(channel_id) | ||
await background_task(channel) | ||
|
||
|
||
@client.event | ||
async def on_message(message): # basic setup done below to respond to messages | ||
username = str(message.author).split('#')[0] | ||
user_message = str(message.content) | ||
channel = str(message.channel.name) | ||
print(f'{username}:{user_message} ({channel})') | ||
|
||
if message.author == client.user: | ||
return | ||
if message.channel.name == 'bot-testing': # in specific channel only | ||
if user_message.lower() == 'hello': | ||
await message.channel.send(f'Jai Shree Krishna {username}!') | ||
elif user_message.lower() == 'bye': | ||
await message.channel.send(f'Radhe Radhe {username}!') | ||
return | ||
elif user_message.lower() == 'random': # checking random function | ||
x = random.randrange(18) | ||
l = [47, 72, 43, 42, 29, 47, 30, 28, 34, | ||
42, 55, 20, 35, 27, 20, 24, 28, 78] | ||
y = random.randrange(l[x-1]) | ||
verse = get_verse(x, y) | ||
await message.channel.send(f'Here is random verse for you \n{verse}') | ||
return | ||
if user_message.lower() == 'help': # anywhere | ||
embedVar = discord.Embed(title="Bhagavad Gita Bot",name="BhagavadGita.io",url="https://bhagavadgita.io/",description="Click to visit site",color=0x00ff00) | ||
embedVar.add_field(name="hello, bye", value="The bot responds to hello and bye in any case", inline=False) | ||
embedVar.add_field(name="random", value="Use this command to generate random verse", inline=False) | ||
embedVar.add_field(name="verse <x>.<y>", value="Use this command to get specific verse where `<x>` is chapter number and `<y>` verse number", inline=False) | ||
await message.channel.send(embed=embedVar) | ||
return | ||
|
||
if user_message.startswith('verse'): | ||
sliced = user_message[5:] | ||
x = sliced.split('.')[0] | ||
y = sliced.split('.')[1] | ||
verse = get_verse(x, y) | ||
if verse=="": | ||
await message.channel.send("Please check your chapter number or verse number") | ||
await message.channel.send(verse) | ||
return | ||
WHEN = time(3, 30, 0) # 3:30 AM UTC = 9:00 AM IST | ||
channel_id = 865245003885576232 | ||
|
||
|
||
async def called_once_a_day(channel): # Fired every day | ||
await client.wait_until_ready() | ||
# print(channel) | ||
await channel.send("scheduled") | ||
|
||
|
||
@tasks.loop(seconds=1) | ||
async def background_task(channel): | ||
now = datetime.utcnow() | ||
# Make sure loop doesn't start after {WHEN} as then it will send immediately the first time as negative seconds will make the sleep yield instantly | ||
if now.time() > WHEN: | ||
tomorrow = datetime.combine(now.date() + timedelta(days=1), time(0)) | ||
# Seconds until tomorrow (midnight) | ||
seconds = (tomorrow - now).total_seconds() | ||
# Sleep until tomorrow and then the loop will start | ||
await asyncio.sleep(seconds) | ||
while True: | ||
# You can do now() or a specific timezone if that matters, but I'll leave it with utcnow | ||
now = datetime.utcnow() | ||
target_time = datetime.combine( | ||
now.date(), WHEN) # 6:00 PM today (In UTC) | ||
seconds_until_target = (target_time - now).total_seconds() | ||
# Sleep until we hit the target time | ||
await asyncio.sleep(seconds_until_target) | ||
# Call the helper function that sends the message | ||
await called_once_a_day(channel) | ||
tomorrow = datetime.combine(now.date() + timedelta(days=1), time(0)) | ||
# Seconds until tomorrow (midnight) | ||
seconds = (tomorrow - now).total_seconds() | ||
# Sleep until tomorrow and then the loop will start a new iteration | ||
await asyncio.sleep(seconds) | ||
|
||
client.run(TOKEN) |
Oops, something went wrong.