Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reaction command / logger #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions commands/reaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import nextcord
from DiscordBot.logger import log
from DiscordBot.commands.reactions import emoji # yuck

commands = {
"emoji": emoji.emoji
}


async def reaction(message: nextcord.Message, client):
log("command", f"(ur prefix) {message.guild.name}")
reaction_type = message.content.split()[3] # modify based on your prefix

if reaction_type in commands:
await commands[reaction_type](message, client)
else:
return
52 changes: 52 additions & 0 deletions commands/reactions/emoji.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import nextcord
from DiscordBot.logger import log


# TODO: make it actually log the channels to a .json or a db

async def emoji(message: nextcord.Message, client):
await message.channel.send("Which channel do you want me to use for the emoji reaction system? (Enter the "
"channel ID or 'new' to create a new channel)")
channel_response = await client.wait_for("message")

if channel_response.content.lower() == "new":
channel = await message.guild.create_text_channel('emoji')
log("command", f"New emoji channel {channel.id} in {message.guild.name}")

else:
channel = client.get_channel(int(channel_response.content))

await channel.send(f"{message.author.mention} Which emojis do you want me to associate with which roles? ("
f"Enter each association in the format 'emoji: role', or 'done' when finished)")

associations = {}

while True:
association_response = await client.wait_for("message")
if association_response.content.lower() == "done":
break

emoji, role = association_response.content.split(":")

emoji = emoji.strip()
role = role.strip()
associations[emoji] = role

await channel.send('Please send the message you want me to react to with the corresponding emojis:')

user_message = await client.wait_for("message")
log("command", f"Logged message {user_message.id} in {message.guild.name}")

for emoji in associations:
await user_message.add_reaction(emoji)

@client.event
async def on_raw_reaction_add(payload):
if payload.member == client.user:
return

if payload.emoji.name in associations:
user = payload.member
role = nextcord.utils.get(message.guild.roles, name=associations[payload.emoji.name])

await user.add_roles(role)
13 changes: 13 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import time

ansi_codes = {
"warning": "\33[41;30m",
"start": "\33[105;30m",
"command": "\33[44;30m",
"reset": "\33[0m"
}


def log(type, message, *args):
current_time = time.gmtime()
print(f"{ansi_codes[type]}[{time.strftime('%H:%M:%S', current_time)}]{ansi_codes['reset']} {message}", *args)
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import logging
# instead of that module, you can use a custom logger.py file I added in the main folder
# from logger import log

import coloredlogs
from hereby import Here
Expand All @@ -24,5 +26,6 @@
else:
client = BachelorOverTelecom.get_instance()
client.add_cog(CommandCog(ENVIRONMENT=ENVIRONMENT))
# log("start", f"{client.user.name} is ready!") # example usage of the logger.py :)
logging.info(f"Starting application with env vars: {', '.join(ENVIRONMENT.keys())}")
client.run(API_KEY)