Skip to content

Instantly share code, notes, and snippets.

@xarical
Created December 25, 2024 18:33
Show Gist options
  • Select an option

  • Save xarical/d4ae63b0db9e3dce09bff7e9d97cf593 to your computer and use it in GitHub Desktop.

Select an option

Save xarical/d4ae63b0db9e3dce09bff7e9d97cf593 to your computer and use it in GitHub Desktop.
import discord
from discord.ext import commands, tasks
import requests
bot = commands.Bot(command_prefix='/', intents=discord.Intents.default())
user_id = "" # Discord user ID
bot_token = "" # Discord bot token
api_url = "" # API url
api_key = "" # API key
newest_notification_id = -1 # ID of the newest notification; -1 is just a default value since we assume the IDs are not negative
def read_api() -> list[dict]:
headers = {"Authorization:": "Bearer: " + api_key}
r = requests.get(api_url, headers=headers)
return r.json()
@tasks.loop(seconds=30)
async def check_notifs():
print("checking notifs...")
notifications = read_api()
for notification in notifications:
if notification["id"] == newest_notification_id:
break
else:
await user.send(str(notification))
newest_notification_id = notifications[0]["id"]
@bot.event
async def on_ready():
print("ready! starting...")
global user
user = await bot.fetch_user(user_id)
global newest_notification_id
notifications = read_api()
newest_notification_id = notifications[0]["id"]
check_notifs.start()
bot.run(bot_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment