This repository has been archived on 2023-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
discord-notifier/discordnotifier/__init__.py

83 lines
2.9 KiB
Python
Raw Permalink Normal View History

2020-09-13 07:26:44 +02:00
import discord, boto3, configparser, os, sys
2020-09-13 08:34:13 +02:00
version = "0.0.3"
2020-09-13 07:26:44 +02:00
# Get token at: https://discord.com/developers/applications/
config = configparser.ConfigParser()
config_path = "discord_config.ini"
if not os.path.exists(config_path):
with open(config_path, "w") as file_writer:
file_writer.write("""[discord]
token = {PUT TOKEN HERE}
ignore_user = {PUT USERNAME HERE}
[aws]
topic = {PUT TOPIC HERE}
profile = default""".replace(" ",""))
print(f"Please update '{config_path}'!")
sys.exit(0)
config.read(config_path)
session = boto3.session.Session(profile_name=config["aws"]["profile"])
bot_token = config["discord"]["token"]
sns = session.resource('sns').Topic(config["aws"]["topic"])
if sns == None:
print("SNS was configured poorly!")
sys.exit(0)
class Notifier(discord.Client):
2020-09-13 08:33:52 +02:00
def __init__(self):
super().__init__()
self.enabled = True
2020-09-13 07:26:44 +02:00
async def on_ready(self):
print(f"Logged in as {self.user}")
guilds = ""
for guild in self.guilds:
guilds = guilds + ", " + str(guild)
print(f"Watching servers: {guilds[2:]}")
async def on_message(self, message):
if "do you see me?" in message.content.lower():
print(f"I see {message.author}")
await message.channel.send(":eye: You have been seen! :eye:")
2020-09-13 08:06:16 +02:00
if str(message.author) == config["discord"]["ignore_user"]:
2020-09-13 08:33:52 +02:00
if str(message.channel).lower() == f"direct message with {config['discord']['ignore_user']}".lower():
2020-09-13 08:06:16 +02:00
if message.content.lower() == "start":
2020-09-13 08:33:52 +02:00
self.enabled = True
2020-09-13 08:06:16 +02:00
print("Notifications were enabled")
2020-09-13 08:33:52 +02:00
await message.channel.send("Notifications were enabled")
2020-09-13 08:06:16 +02:00
elif message.content.lower() == "stop":
2020-09-13 08:33:52 +02:00
self.enabled = False
2020-09-13 08:06:16 +02:00
print("Notifications were disabled")
2020-09-13 08:33:52 +02:00
await message.channel.send("Notifications were disabled")
2020-09-13 08:06:16 +02:00
elif message.content.lower() == "status":
2020-09-13 08:33:52 +02:00
if self.enabled:
await message.channel.send("Notifications are currently enabled!")
2020-09-13 08:06:16 +02:00
else:
2020-09-13 08:33:52 +02:00
await message.channel.send("Notifications are currently disabled!")
2020-09-13 08:06:16 +02:00
else:
2020-09-13 08:33:52 +02:00
await message.channel.send("Valid commands are 'START', 'STATUS', and 'STOP'")
return
2020-09-13 08:06:16 +02:00
if str(message.author) in [str(self.user), config["discord"]["ignore_user"]]:
2020-09-13 07:26:44 +02:00
return
2020-09-13 08:33:52 +02:00
if self.enabled:
2020-09-13 08:06:16 +02:00
# Format and print mesage
formatted_message = f"<{message.author}> \"{message.content}\" from #{message.channel} on {message.guild}"
print(formatted_message)
# Send notification to SNS
sns.publish(Message=formatted_message)
2020-09-13 07:26:44 +02:00
def main():
client = Notifier()
client.run(bot_token)
if __name__ == "__main__":
main()