Skip to content
Snippets Groups Projects
root.py 2.36 KiB
import os

import discord
from discord.ext import commands
from dislash import *
from dotenv import load_dotenv

from cogs import appointments, calmdown, help, links, polls, roles, support, text_commands, timer, welcome

# .env file is necessary in the same directory, that contains several strings.
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = int(os.getenv('DISCORD_GUILD'))
ACTIVITY = os.getenv('DISCORD_ACTIVITY')
PIN_EMOJI = "📌"

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(ACTIVITY), intents=intents)
bot.add_cog(appointments.Appointments(bot))
bot.add_cog(calmdown.Calmdown(bot))
bot.add_cog(help.Help(bot))
bot.add_cog(links.Links(bot))
bot.add_cog(polls.Polls(bot))
bot.add_cog(roles.Roles(bot))
bot.add_cog(support.Support(bot))
bot.add_cog(text_commands.TextCommands(bot))
bot.add_cog(timer.Timer(bot))
bot.add_cog(welcome.Welcome(bot))

SlashClient(bot, show_warnings=True)  # Stellt den Zugriff auf die Buttons bereit


def get_reaction(reactions):
    """ Returns the reaction, that is equal to the specified PIN_EMOJI,
    or if that reaction does not exist in list of reactions, None will be returned"""

    for reaction in reactions:
        if reaction.emoji == PIN_EMOJI:
            return reaction
    return None


async def pin_message(message):
    """ Pin the given message, if it is not already pinned """

    if not message.pinned:
        await message.pin()


async def unpin_message(message):
    """ Unpin the given message, if it is pinned, and it has no pin reaction remaining. """

    if message.pinned:
        reaction = get_reaction(message.reactions)
        if reaction is None:
            await message.unpin()


@bot.event
async def on_ready():
    print("Client started!")


@bot.event
async def on_raw_reaction_add(payload):
    if payload.user_id == bot.user.id:
        return

    if payload.emoji.name == PIN_EMOJI:
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        await pin_message(message)


@bot.event
async def on_raw_reaction_remove(payload):
    if payload.emoji.name == PIN_EMOJI:
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        await unpin_message(message)


bot.run(TOKEN)