def create_environment(cog: 'Exec', ctx: DogbotContext) -> Dict[Any, Any]:
async def upload(file_name: str) -> Message:
"""Shortcut to upload a file."""
with open(file_name, 'rb') as fp:
return await ctx.send(file=discord.File(fp))
async def send(*args, **kwargs) -> Message:
"""Shortcut to send()."""
return await ctx.send(*args, **kwargs)
def better_dir(*args, **kwargs) -> List[str]:
"""dir(), but without magic methods."""
return [n for n in dir(*args, **kwargs) if not n.endswith('__') and not n.startswith('__')]
T = TypeVar('T')
def grabber(lst: List[T]) -> Callable[[int], T]:
"""Returns a function that, when called, grabs an item by ID from a list of objects with an ID."""
def _grabber_function(thing_id: int) -> T:
return discord.utils.get(lst, id=thing_id)
return _grabber_function
env = {
'bot': ctx.bot,
'ctx': ctx,
'msg': ctx.message,
'guild': ctx.guild,
'channel': ctx.channel,
'me': ctx.message.author,
'cog': cog,
# modules
'discord': discord,
'commands': commands,
'command': commands.command,
'group': commands.group,
# utilities
'_get': discord.utils.get,
'_find': discord.utils.find,
'_upload': upload,
'_send': send,
# grabbers
'_g': grabber(ctx.bot.guilds),
'_u': grabber(ctx.bot.users),
'_c': grabber(list(ctx.bot.get_all_channels())),
# last result
'_': cog.last_result,
'_p': cog.previous_code,
'dir': better_dir,
}
# add globals to environment
env.update(globals())
return env
评论列表
文章目录