Esempio n. 1
0
async def log_task(guild_id, target):
    to_send = ""
    todo = None
    # keep pumping until we run out of messages
    while not LOG_QUEUE[target].empty():
        try:
            channel = BOT.get_channel(int(target))
            # channel no longer exists, abort and re-validate config to remove the invalid entry
            if channel is None:
                del LOG_QUEUE[target]
                Configuration.validate_config(guild_id)
                return
            # pull message from queue
            todo = LOG_QUEUE[target].get(block=False)
            if (len(to_send) + len(todo.message) if todo.message is not None else 0) < 2000:
                to_send = f'{to_send}\n{todo.message if todo.message is not None else ""}'
            else:
                # too large, send it out
                await channel.send(to_send, allowed_mentions=AllowedMentions(everyone=False, users=False, roles=False))
                to_send = todo.message
            if todo.embed is not None or todo.file is not None or LOG_QUEUE[target].empty():
                await channel.send(to_send, embed=todo.embed, file=todo.file, allowed_mentions=AllowedMentions(everyone=False, users=False, roles=False))
                to_send = ""
        except discord.Forbidden:
            # someone screwed up their permissions, not my problem, will show an error in the dashboard
            del LOG_QUEUE[target]
            return
        except CancelledError:
            return  # bot is terminating
        except Exception as e:
            del LOG_QUEUE[target]
            await TheRealGearBot.handle_exception("LOG PUMP", BOT, e,
                                                  cid=target, todo=todo, to_send=to_send)
            return
    del LOG_QUEUE[target]
Esempio n. 2
0
async def log_task(guild_id, target):
    to_send = ""
    todo = None
    # keep pumping until we run out of messages
    while not LOG_QUEUE[target].empty():
        try:
            channel = BOT.get_channel(int(target))
            # channel no longer exists, abort and re-validate config to remove the invalid entry
            if channel is None:
                del LOG_QUEUE[target]
                Configuration.validate_config(guild_id)
                return

            permissions = channel.permissions_for(channel.guild.me,
                                                  ignore_timeout=False)
            if not (permissions.send_messages and permissions.read_messages):
                # we can't send to this channel log and abort before we get ip banned again!
                del LOG_QUEUE[target]
                info(
                    f"Unable to log to logging target channel {target} due to missing permissions!"
                )
                return
            # pull message from queue
            todo = LOG_QUEUE[target].get(block=False)
            if (len(to_send) + len(todo.message)
                    if todo.message is not None else 0) < 2000:
                to_send = f'{to_send}\n{todo.message if todo.message is not None else ""}'
            else:
                # too large, send it out
                await channel.send(to_send,
                                   allowed_mentions=AllowedMentions(
                                       everyone=False,
                                       users=False,
                                       roles=False))
                to_send = todo.message
            if todo.embed is not None or todo.file is not None or LOG_QUEUE[
                    target].empty():
                await channel.send(to_send,
                                   embed=todo.embed,
                                   file=todo.file,
                                   allowed_mentions=AllowedMentions(
                                       everyone=False,
                                       users=False,
                                       roles=False))
                to_send = ""
        except CancelledError:
            return  # bot is terminating
        except Exception as e:
            del LOG_QUEUE[target]
            await TheRealGearBot.handle_exception("LOG PUMP",
                                                  BOT,
                                                  e,
                                                  cid=target,
                                                  todo=todo,
                                                  to_send=to_send)
            return
    del LOG_QUEUE[target]