✨ send_and_bin utility
This commit is contained in:
parent
3b9f4aae95
commit
b36a582a06
|
@ -168,6 +168,12 @@ class DevCog(Cog, name="Dev tools"):
|
|||
|
||||
await ctx.send(str(jury_channel))
|
||||
|
||||
@command(name="send", hidden=True)
|
||||
@has_role(Role.DEV)
|
||||
async def send_cmd(self, ctx, *msg):
|
||||
await ctx.message.delete()
|
||||
await ctx.send(" ".join(msg))
|
||||
|
||||
|
||||
def setup(bot: Bot):
|
||||
bot.add_cog(DevCog(bot))
|
||||
|
|
|
@ -44,7 +44,8 @@ class MiscCog(Cog, name="Divers"):
|
|||
"""
|
||||
|
||||
choice = random.choice(args)
|
||||
await ctx.send(f"J'ai choisi... **{choice}**")
|
||||
msg = await ctx.send(f"J'ai choisi... **{choice}**")
|
||||
await self.bot.wait_for_bin(ctx.author, msg),
|
||||
|
||||
@command(name="joke", aliases=["blague"], hidden=True)
|
||||
async def joke_cmd(self, ctx):
|
||||
|
@ -93,9 +94,11 @@ class MiscCog(Cog, name="Divers"):
|
|||
"""Affiche des détails à propos d'une commande."""
|
||||
|
||||
if not args:
|
||||
await self.send_bot_help(ctx)
|
||||
msg = await self.send_bot_help(ctx)
|
||||
else:
|
||||
await self.send_command_help(ctx, args)
|
||||
msg = await self.send_command_help(ctx, args)
|
||||
|
||||
await self.bot.wait_for_bin(ctx.author, msg)
|
||||
|
||||
async def send_bot_help(self, ctx: Context):
|
||||
embed = discord.Embed(
|
||||
|
@ -134,7 +137,7 @@ class MiscCog(Cog, name="Divers"):
|
|||
|
||||
embed.set_footer(text="Suggestion ? Problème ? Envoie un message à @Diego")
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
return await ctx.send(embed=embed)
|
||||
|
||||
async def send_command_help(self, ctx, args):
|
||||
name = " ".join(args)
|
||||
|
@ -162,7 +165,7 @@ class MiscCog(Cog, name="Divers"):
|
|||
)
|
||||
embed.set_footer(text="Suggestion ? Problème ? Envoie un message à @Diego")
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
return await ctx.send(embed=embed)
|
||||
|
||||
async def send_group_help(self, ctx, group: Group):
|
||||
embed = discord.Embed(
|
||||
|
@ -205,7 +208,7 @@ class MiscCog(Cog, name="Divers"):
|
|||
)
|
||||
embed.set_footer(text="Suggestion ? Problème ? Envoie un message à @Diego")
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
return await ctx.send(embed=embed)
|
||||
|
||||
def _name(self, command: Command):
|
||||
return f"`!{command.qualified_name}`"
|
||||
|
@ -265,5 +268,5 @@ class MiscCog(Cog, name="Divers"):
|
|||
return ret
|
||||
|
||||
|
||||
def setup(bot: Bot):
|
||||
def setup(bot: CustomBot):
|
||||
bot.add_cog(MiscCog(bot))
|
||||
|
|
|
@ -8,7 +8,7 @@ from discord.utils import get, find
|
|||
|
||||
from src.constants import *
|
||||
from src.core import CustomBot
|
||||
from src.utils import has_role
|
||||
from src.utils import has_role, send_and_bin
|
||||
|
||||
Team = namedtuple("Team", ["name", "trigram", "tournoi", "secret", "status"])
|
||||
|
||||
|
@ -46,7 +46,7 @@ class TeamsCog(Cog, name="Teams"):
|
|||
@team.command(name="create")
|
||||
async def create_team(self, ctx: Context, trigram, team_secret):
|
||||
"""
|
||||
Permet aux capitaines de créer leur equipes.
|
||||
Permet aux capitaines de créer leur equipe.
|
||||
|
||||
Pour utiliser cette commande, il faut ton trigram et ton code
|
||||
d'équipe. Tu peux ensuite écrire `!team create TRIGRAM SECRET`
|
||||
|
@ -66,21 +66,21 @@ class TeamsCog(Cog, name="Teams"):
|
|||
captain_role = get(ctx.guild.roles, name=Role.CAPTAIN)
|
||||
|
||||
if team is None:
|
||||
await ctx.send(
|
||||
msg = (
|
||||
f"{ctx.author.mention}: le trigram `{trigram}` "
|
||||
f"n'est pas valide. Es-tu sûr d'avoir le bon ?"
|
||||
)
|
||||
elif role is not None:
|
||||
# Team exists
|
||||
captain = find(lambda m: captain_role in m.roles, role.members)
|
||||
await ctx.send(
|
||||
msg = (
|
||||
f"{ctx.author.mention}: l'équipe {trigram} "
|
||||
f"existe déjà. Tu peux demander a ton capitaine "
|
||||
f"{captain.mention} de t'ajouter à l'équipe avec "
|
||||
f"`!team add {ctx.author.name}`"
|
||||
)
|
||||
elif team_secret != team.secret:
|
||||
await ctx.send(
|
||||
msg = (
|
||||
f"{ctx.author.mention}: ton secret n'est pas valide, "
|
||||
f"Tu peux le trouver sur https://inscription.tfjm.org/mon-equipe."
|
||||
)
|
||||
|
@ -103,7 +103,7 @@ class TeamsCog(Cog, name="Teams"):
|
|||
reason="Creation of team " + trigram,
|
||||
)
|
||||
|
||||
await ctx.send(
|
||||
msg = (
|
||||
f"L'équipe {team_role.mention} a été créée et son capitaine "
|
||||
f"est {ctx.author.mention}"
|
||||
)
|
||||
|
@ -122,8 +122,12 @@ class TeamsCog(Cog, name="Teams"):
|
|||
f"une meilleure expérience ici, envoie un petit message à {diego.mention} ;)"
|
||||
)
|
||||
|
||||
msg = await ctx.send(msg)
|
||||
await self.bot.wait_for_bin(ctx.author, msg)
|
||||
|
||||
@team.command(name="add")
|
||||
@commands.has_role(Role.CAPTAIN)
|
||||
@send_and_bin
|
||||
async def team_add(self, ctx, member: discord.Member):
|
||||
"""
|
||||
(cap) Ajoute un membre a ton équipe.
|
||||
|
@ -141,12 +145,12 @@ class TeamsCog(Cog, name="Teams"):
|
|||
member_teams = self.teams_for(member)
|
||||
|
||||
if member_teams:
|
||||
await ctx.send(
|
||||
return (
|
||||
f"{member.mention} est déjà dans une équipe "
|
||||
f"et ne peut pas être dans deux à la fois."
|
||||
)
|
||||
elif len(author_teams) > 1:
|
||||
await ctx.send(
|
||||
return (
|
||||
f"Tu est dans plusieurs équipes, je ne sais "
|
||||
f"pas où l'ajouter. Il faut demander à un organisateur "
|
||||
f"de le faire."
|
||||
|
@ -162,12 +166,11 @@ class TeamsCog(Cog, name="Teams"):
|
|||
participant,
|
||||
reason=f"{ctx.author.name} l'a ajouté à son équipe",
|
||||
)
|
||||
await ctx.send(
|
||||
f"{member.mention} a été ajouté dans l'équipe {the_team[1].mention}"
|
||||
)
|
||||
return f"{member.mention} a été ajouté dans l'équipe {the_team[1].mention}"
|
||||
|
||||
@team.command(name="channel")
|
||||
@commands.has_role(Role.CAPTAIN)
|
||||
@send_and_bin
|
||||
async def team_channel(self, ctx, *channel_name):
|
||||
"""
|
||||
(cap) Crée une channel privée pour l'équipe
|
||||
|
@ -180,18 +183,17 @@ class TeamsCog(Cog, name="Teams"):
|
|||
"""
|
||||
|
||||
if not channel_name:
|
||||
await ctx.send(
|
||||
return (
|
||||
"Tu dois mettre un nom d'équipe, par exemple "
|
||||
"`!team channel un-super-nom`"
|
||||
)
|
||||
return
|
||||
|
||||
channel_name = " ".join(channel_name)
|
||||
|
||||
guild: discord.Guild = ctx.guild
|
||||
team_role = self.teams_for(ctx.author)[0][1]
|
||||
team_channel_category = get(guild.categories, name=TEAMS_CHANNEL_CATEGORY)
|
||||
await guild.create_text_channel(
|
||||
channel = await guild.create_text_channel(
|
||||
channel_name,
|
||||
overwrites={
|
||||
guild.default_role: discord.PermissionOverwrite(read_messages=False),
|
||||
|
@ -201,8 +203,11 @@ class TeamsCog(Cog, name="Teams"):
|
|||
reason=f"{ctx.author.name} à demandé une channel pour son équipe.",
|
||||
)
|
||||
|
||||
return f"{ctx.author.mention}: Le salon d'équipe {channel.mention} à été créé."
|
||||
|
||||
@team.command(name="voice", usage="Nom du salon")
|
||||
@commands.has_role(Role.CAPTAIN)
|
||||
@send_and_bin
|
||||
async def team_voice(self, ctx, *channel_name):
|
||||
"""
|
||||
(cap) Crée une channel vocale privée pour l'équipe
|
||||
|
@ -215,18 +220,17 @@ class TeamsCog(Cog, name="Teams"):
|
|||
"""
|
||||
|
||||
if not channel_name:
|
||||
await ctx.send(
|
||||
return (
|
||||
"Tu dois mettre un nom d'équipe, par exemple "
|
||||
"`!team voice un-super-nom`"
|
||||
)
|
||||
return
|
||||
|
||||
channel_name = " ".join(channel_name)
|
||||
|
||||
guild: discord.Guild = ctx.guild
|
||||
team_role = self.teams_for(ctx.author)[0][1]
|
||||
team_channel_category = get(guild.categories, name=TEAMS_CHANNEL_CATEGORY)
|
||||
await guild.create_voice_channel(
|
||||
channel = await guild.create_voice_channel(
|
||||
channel_name,
|
||||
overwrites={
|
||||
guild.default_role: discord.PermissionOverwrite(read_messages=False),
|
||||
|
@ -236,6 +240,8 @@ class TeamsCog(Cog, name="Teams"):
|
|||
reason=f"{ctx.author.name} à demandé un salon vocale pour son équipe.",
|
||||
)
|
||||
|
||||
return f"{ctx.author.mention}: La salon vocal '{channel.mention}' à été créé."
|
||||
|
||||
@team.command(name="list")
|
||||
@commands.has_role(Role.CNO)
|
||||
async def list_cmd(self, ctx):
|
||||
|
@ -258,5 +264,5 @@ class TeamsCog(Cog, name="Teams"):
|
|||
await ctx.send(embed=embed)
|
||||
|
||||
|
||||
def setup(bot: Bot):
|
||||
def setup(bot: CustomBot):
|
||||
bot.add_cog(TeamsCog(bot))
|
||||
|
|
26
src/utils.py
26
src/utils.py
|
@ -1,12 +1,7 @@
|
|||
import asyncio
|
||||
from typing import Sequence
|
||||
from functools import wraps
|
||||
|
||||
import psutil
|
||||
from discord import Message, Member, User, Reaction
|
||||
from discord.ext.commands import Context, Bot
|
||||
from discord.utils import get
|
||||
|
||||
from src.constants import Emoji
|
||||
from discord.ext.commands import Bot
|
||||
|
||||
|
||||
def has_role(member, role: str):
|
||||
|
@ -15,6 +10,23 @@ def has_role(member, role: str):
|
|||
return any(r.name == role for r in member.roles)
|
||||
|
||||
|
||||
def send_and_bin(f):
|
||||
"""
|
||||
Decorator that allows a command in a cog to just return
|
||||
the messages that needs to be sent, and allow the author that
|
||||
trigger the message de delete it.
|
||||
"""
|
||||
|
||||
@wraps(f)
|
||||
async def wrapped(cog, ctx, *args, **kwargs):
|
||||
msg = await f(cog, ctx, *args, **kwargs)
|
||||
if msg:
|
||||
msg = await ctx.send(msg)
|
||||
await cog.bot.wait_for_bin(ctx.author, msg)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
def start_time(self):
|
||||
return psutil.Process().create_time()
|
||||
|
||||
|
|
Loading…
Reference in New Issue