-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbot.py
96 lines (73 loc) · 3.33 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
import json
import discord
from discord.ext import commands
from discord.ext import tasks
from datetime import datetime
from googleapiclient.discovery import build
client = commands.Bot(command_prefix="++")
Key = "#" # Replace with your API key.
BlogID = "#" # Replace with your BlogId here.
Roles = ["CSGO", "Garry's Mod", "GTA 5"] # Add your server roles here.
blog = build("blogger", "v3", developerKey=Key)
Token = '#' # Replace with Discord Bot Token.
@client.event
async def on_ready():
print("Bot has started running")
await client.change_presence(activity=discord.Game(name="cmd: ++search"))
@client.command()
async def search(ctx, arg):
search_term = str(arg).replace(" ", "%")
base_url = "https://www.googleapis.com/blogger/v3/blogs/" + BlogID + "/posts/search"
complete_url = base_url + "?q=" + search_term + \
"&key=" + Key
response = requests.get(complete_url)
result = response.json()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
embed = discord.Embed(title="List of Search results",
description="Checked on " + f"{current_time}\n", color=0x349bfc)
embed.set_author(name="Your Website Name")
embed.set_thumbnail(url="https://www.yourwebsite.com/favicon.ico")
try:
for count, value in enumerate(result["items"]):
title = result["items"][count]["title"]
url = result["items"][count]["url"]
embed.description = embed.description + \
f"{count + 1}. [{title}]({url})\n"
embed.set_footer(text='This message will be deleted in 1 Hour.')
await ctx.send(embed=embed, delete_after=3600.0)
except:
await ctx.send("There is something wrong with the response.")
client.recentPosts = None
client.recentPostsTime = None
client.recentPostsEdit = None
@tasks.loop(seconds=5.0)
async def fetchUpdates():
posts = blog.posts().list(blogId=BlogID).execute()
postsList = posts["items"]
postTime = postsList[0]["published"]
if not client.recentPosts:
client.recentPostsTime = postTime
client.recentPosts = postsList
elif client.recentPostsTime != postTime:
titleValue = str(posts["items"][0]["title"])
urlValue = str(posts["items"][0]["url"])
channel = client.get_channel(channel_id) # Add channel ID
embed = discord.Embed(title="New posts available on the blog!",
description=f"[{titleValue}]({urlValue})")
channel = client.get_channel(channel_id) # Add channel ID
embed = discord.Embed(title="New posts available on the blog!",
description=f"[{titleValue}]({urlValue})")
embed.set_author(name="Your Website")
embed.set_thumbnail(url="https://www.yourwebsite.com/favicon.ico")
for i in Roles:
if i.lower() in postsList[0]["title"].lower():
guild = client.guilds[0]
await channel.send(discord.utils.get(guild.roles, name=i).mention, delete_after=86400.0)
await channel.send(embed=embed, delete_after=86400.0)
break
client.recentPostsTime = postTime
client.recentPosts = postsList
fetchUpdates.start()
client.run(Token, bot=True)