Esempio n. 1
0
    async def remove_youtube_channel(self, ctx: commands.Context,
                                     youtube_channel_id: str):
        """
        Remove a Youtube channel that was being notified
        :param ctx: The current context. (discord.ext.commands.Context)
        :param youtube_channel_id: The Youtube channel to be notified of (str)
        """

        guild_data = await get_guild_data(ctx.message.guild.id)
        # Error if not admin
        if not guild_data.user_is_admin(ctx.author):
            gif = translate("not_admin_gif", await culture(ctx))
            return await ctx.send(gif)

        remove_response = await guild_data.remove_youtube_channel(
            youtube_channel_id)
        msg = ""
        if remove_response:
            msg = translate("youtube_removed", await
                            culture(ctx)).format(youtube_channel_id)
        else:
            msg = translate("youtube_no_exists", await
                            culture(ctx)).format(youtube_channel_id)
        info(msg)
        await ctx.send(msg)
Esempio n. 2
0
async def on_ready():
    if YOUTUBE_TOKEN:
        info("Starting YouTube scheduler")
        check_and_post_latest_videos.start(bot)
    else:
        fatal(
            "Not starting YouTube scheduler. Please provide a YOUTUBE_TOKEN in your .env file"
        )

    bot.is_purging = {}
    purge_messages.start(bot)
Esempio n. 3
0
    async def add_purger(self, ctx: commands.Context, text_channel: str,
                         max_age: int):
        """
        Add a channel to be regularly purged.
        :param ctx: The current context. (discord.ext.commands.Context)
        :param text_channel: The text channel that will be purged (str)
        :param max_age: The max age of messages in days (int)
        """
        guild_data = await get_guild_data(ctx.message.guild.id)
        # Error if not admin
        if not guild_data.user_is_admin(ctx.author):
            gif = translate("not_admin_gif", await culture(ctx))
            return await ctx.send(gif)

        text_channel = text_channel.lower()

        # Get channel
        channel = get_channel(ctx, text_channel)

        # TODO: Give information to the user when the text channel does not exist
        if not channel:
            await ctx.channel.send(
                translate("membercount_channel_nonexistant", await
                          culture(ctx)))
            raise Exception("Invalid text channel provided")

        #Give error if the channel is a voice channel
        if isinstance(channel, discord.VoiceChannel):
            await ctx.channel.send(
                translate("channel_is_voice", await culture(ctx)))
            return

        # member = ctx.get_member(ctx.user.id)
        channel_permissions = channel.permissions_for(ctx.me)
        if not (channel_permissions.manage_messages
                and channel_permissions.read_message_history):
            return await ctx.send(
                translate("purger_permissions", await culture(ctx)))

        add_response = await guild_data.add_purger(channel, max_age)

        msg = ""
        if add_response:
            msg = translate("purger_added", await
                            culture(ctx)).format(str(channel.id), max_age)
        else:
            msg = translate("purger_exists", await
                            culture(ctx)).format(str(channel.id))
        info(msg)
        await ctx.send(msg)
Esempio n. 4
0
    async def add_youtube_channel(self, ctx: commands.Context,
                                  youtube_channel_id: str, text_channel: str):
        """
        Add a Youtube channel to be notified
        :param ctx: The current context. (discord.ext.commands.Context)
        :param youtube_channel_id: The Youtube channel to be notified of (str)
        :param text_channel: The text channel that will receive the notification (str)
        """
        guild_data = await get_guild_data(ctx.message.guild.id)
        # Error if not admin
        if not guild_data.user_is_admin(ctx.author):
            gif = translate("not_admin_gif", await culture(ctx))
            return await ctx.send(gif)

        # TODO: throw specific error with message when channel ID is wrong
        latest_video = await get_latest_video(youtube_channel_id)

        # Get the channel
        channel = get_channel(ctx, text_channel)

        # TODO: Give information to the user when the text channel does not exist
        if not channel:
            await ctx.channel.send(
                translate("membercount_channel_nonexistant", await
                          culture(ctx)))
            raise Exception("Invalid text channel provided")

        if isinstance(channel, discord.VoiceChannel):
            await ctx.channel.send(
                translate("channel_is_voice", await culture(ctx)))
            return

        add_response = await guild_data.add_youtube_channel(
            youtube_channel_id, channel, latest_video["video_id"])

        msg = ""
        if add_response:
            msg = translate("youtube_added", await
                            culture(ctx)).format(youtube_channel_id, channel)
        else:
            msg = translate("youtube_exists", await
                            culture(ctx)).format(youtube_channel_id)
        info(msg)
        await ctx.send(msg)
Esempio n. 5
0
    async def remove_purger(self, ctx: commands.Context, text_channel: str):
        """
        Remove a purger channel that was being notified
        :param ctx: The current context. (discord.ext.commands.Context)
        :param text_channel: The channel where the purger is attached to (str)
        """

        guild_data = await get_guild_data(ctx.message.guild.id)
        # Error if not admin
        if not guild_data.user_is_admin(ctx.author):
            gif = translate("not_admin_gif", await culture(ctx))
            return await ctx.send(gif)

        # Get channel
        channel = get_channel(ctx, text_channel)

        # TODO: Give information to the user when the text channel does not exist
        if not channel:
            await ctx.channel.send(
                translate("membercount_channel_nonexistant", await
                          culture(ctx)))
            raise Exception("Invalid text channel provided")

        #Give error if the channel is a voice channel
        if isinstance(channel, discord.VoiceChannel):
            await ctx.channel.send(
                translate("channel_is_voice", await culture(ctx)))
            return

        remove_response = await guild_data.remove_purger(channel)
        msg = ""
        if remove_response:
            msg = translate("purger_removed", await
                            culture(ctx)).format(str(channel.id))
        else:
            msg = translate("purger_no_exists", await
                            culture(ctx)).format(str(channel.id))
        info(msg)
        await ctx.send(msg)
Esempio n. 6
0
async def purge_messages(bot):
    info("Purging messages")
    guilds_data = await get_all_guilds_data()

    for guild_data in guilds_data:
        if not bot.is_purging.get(str(guild_data.guild_id), False):
            bot.is_purging[str(guild_data.guild_id)] = True
            for text_channel, max_age in guild_data.purgers.items():
                before = datetime.today() - timedelta(days=max_age)
                channel = bot.get_channel(int(text_channel))
                try:
                    if channel:
                        while True:
                            deleted_messages = await channel.purge(
                                check=check, before=before)
                            if len(deleted_messages) == 0:
                                break
                except:
                    fatal(
                        f"Failed to purge messages for channel {channel.name} in guild {guild_data.guild_id}. Does the bot have appropriate permissions on that channel?"
                    )
            bot.is_purging[str(guild_data.guild_id)] = False
Esempio n. 7
0
from dotenv import load_dotenv

from nerdlandbot.bot import NerdlandBot
from nerdlandbot.helpers.log import info, fatal
from nerdlandbot.translations.Translations import get_text as _
from nerdlandbot.scheduler.YoutubeScheduler import check_and_post_latest_videos
from nerdlandbot.scheduler.PurgeScheduler import purge_messages
from nerdlandbot.commands.GuildData import get_all_guilds_data, GuildData

load_dotenv()

PREFIX = os.getenv("PREFIX")
TOKEN = os.getenv("DISCORD_TOKEN")

if PREFIX:
    info("Start bot with prefix '" + PREFIX + "'")
else:
    fatal("Please provide a PREFIX in your .env file")
    sys.exit()

# load up intents
intents = discord.Intents.all()

bot = NerdlandBot(PREFIX, intents)

# remove default help command
bot.remove_command("help")

# load event handlers
bot.load_extension("nerdlandbot.eventhandlers.onmemberjoin")
bot.load_extension("nerdlandbot.eventhandlers.onready")
Esempio n. 8
0
 async def on_ready(self):
     """
     This gets executed when the bot connects to discord.
     """
     info(f'{self.bot.user.name} has connected to Discord!')