1
0
mirror of https://gitlab.com/ddorn/tfjm-discord-bot.git synced 2025-07-03 20:02:48 +02:00

async console

This commit is contained in:
ddorn
2020-05-01 17:08:05 +02:00
parent d16aeee4c0
commit 5af3342182
6 changed files with 152 additions and 17 deletions

View File

@ -1,3 +1,4 @@
import asyncio
import code
from pprint import pprint
@ -5,6 +6,7 @@ import discord
from discord import TextChannel, PermissionOverwrite
from discord.ext.commands import command, has_role, Bot, Cog
from discord.utils import get
from ptpython.repl import embed
from src.constants import *
from src.core import CustomBot
@ -18,6 +20,8 @@ COGS_SHORTCUTS = {
"v": "dev",
}
KeyboardInterrupt
class DevCog(Cog, name="Dev tools"):
def __init__(self, bot: CustomBot):
@ -42,18 +46,17 @@ class DevCog(Cog, name="Dev tools"):
# Utility functions
local = {
**globals(),
**locals(),
"pprint": pprint,
"_show": lambda o: print(*dir(o), sep="\n"),
"__name__": "__console__",
"__doc__": None,
}
def send(msg, channel=None):
channel = channel or ctx.channel
asyncio.create_task(channel.send(msg))
try:
await embed(
globals(), locals(), vi_mode=True, return_asyncio_coroutine=True
)
except EOFError:
pass
code.interact(
banner="Ne SURTOUT PAS FAIRE Ctrl+C !\n(TFJM² debugger)", local=local
)
await ctx.send("Tout va mieux !")
def full_cog_name(self, name):
@ -121,17 +124,24 @@ class DevCog(Cog, name="Dev tools"):
"""
return
guild: discord.Guild = ctx.guild
nothing = PermissionOverwrite(read_messages=False)
see = PermissionOverwrite(read_messages=True)
# orga = get(guild.roles, name=f"Orga {t}")
for t in TOURNOIS[3:]:
jury = get(guild.roles, name=f"Jury {t}")
for p in "AB":
await guild.create_voice_channel(
f"blabla-jury-poule-{p}",
overwrites={guild.default_role: nothing, jury: see},
category=get(guild.categories, name=t),
)
return
aide: TextChannel = get(guild.text_channels, name="aide")
for t in TOURNOIS:
orga = get(guild.roles, name=f"Orga {t}")
jury = get(guild.roles, name=f"Jury {t}")
await aide.set_permissions(orga, overwrite=see)
await aide.set_permissions(jury, overwrite=see)
@ -173,5 +183,5 @@ class DevCog(Cog, name="Dev tools"):
await ctx.send(" ".join(msg))
def setup(bot: Bot):
def setup(bot: CustomBot):
bot.add_cog(DevCog(bot))

View File

@ -1,9 +1,11 @@
import datetime
import io
import itertools
import random
from operator import attrgetter
from time import time
import aiohttp
import discord
from discord import Guild
from discord.ext import commands
@ -85,6 +87,18 @@ class MiscCog(Cog, name="Divers"):
await ctx.send(embed=embed)
@command(hidden=True)
async def fractal(self, ctx: Context):
await ctx.message.add_reaction(Emoji.CHECK)
seed = random.randint(0, 1_000_000_000)
async with aiohttp.ClientSession() as session:
async with session.get(FRACTAL_URL.format(seed=seed)) as resp:
if resp.status != 200:
return await ctx.send("Could not download file...")
data = io.BytesIO(await resp.read())
await ctx.send(file=discord.File(data, "cool_image.png"))
# ----------------- Help ---------------- #
@command(name="help", aliases=["h"])

View File

@ -12,6 +12,7 @@ __all__ = [
"DIEGO",
"TOURNOIS",
"EMBED_COLOR",
"FRACTAL_URL",
"File",
"Emoji",
]
@ -32,6 +33,8 @@ GUILD = "690934836696973404"
DIEGO = "Diego" # Mon display name
TEAMS_CHANNEL_CATEGORY = "Channels d'équipes"
EMBED_COLOR = 0xFFA500
FRACTAL_URL = "https://thefractal.space/img/{seed}.png?size=1500"
ROUND_NAMES = ["premier tour", "deuxième tour"]
TOURNOIS = [
"Lille",
@ -61,6 +64,7 @@ class Emoji:
SOB = "😭"
BIN = "🗑️"
DICE = "🎲"
CHECK = ""
class File:

View File

@ -27,7 +27,7 @@ def send_and_bin(f):
return wrapped
def start_time(self):
def start_time():
return psutil.Process().create_time()