Created
December 25, 2024 18:33
-
-
Save xarical/d4ae63b0db9e3dce09bff7e9d97cf593 to your computer and use it in GitHub Desktop.
Notifications forwarding Discord bot using Python and Discord.py - https://xarical.medium.com/making-a-notifications-forwarding-discord-bot-using-python-and-discord-py-16b4ca54702e
This file contains hidden or 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
| 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