echo: New converters for arguemnts

- Created a GlobalChannelConverter
- Accepts Union[GlobalChannelConverter, UserConverter]

Fixes #145
This commit is contained in:
0x5c 2020-01-08 02:19:35 -05:00
parent 60764cd733
commit 0608a74e6c
No known key found for this signature in database
GPG Key ID: 82039FC95E3FE970
2 changed files with 38 additions and 11 deletions

View File

@ -5,19 +5,15 @@ Copyright (C) 2019-2020 Abigail Gold, 0x5c
This file is part of discord-qrm2 and is released under the terms of the GNU This file is part of discord-qrm2 and is released under the terms of the GNU
General Public License, version 2. General Public License, version 2.
---
`colours`: Colours used by embeds.
`cat`: Category names for the HelpCommand.
""" """
import collections import collections
import json import json
import re
import traceback import traceback
from pathlib import Path
from datetime import datetime from datetime import datetime
from pathlib import Path
from types import SimpleNamespace from types import SimpleNamespace
import discord import discord
@ -26,7 +22,8 @@ import discord.ext.commands as commands
import data.options as opt import data.options as opt
__all__ = ["colours", "cat", "emojis", "embed_factory", "error_embed_factory", "add_react", "check_if_owner"] __all__ = ["colours", "cat", "emojis", "paths", "ImageMetadata", "ImagesGroup",
"embed_factory", "error_embed_factory", "add_react", "check_if_owner"]
# --- Common values --- # --- Common values ---
@ -94,6 +91,29 @@ class ImagesGroup(collections.abc.Mapping):
return str(self._images) return str(self._images)
# --- Converters ---
class GlobalChannelConverter(commands.IDConverter):
"""Converter to get any bot-acessible channel by ID/mention (global), or name (in current guild only)."""
async def convert(self, ctx: commands.Context, argument: str):
bot = ctx.bot
guild = ctx.guild
match = self._get_id_match(argument) or re.match(r'<#([0-9]+)>$', argument)
result = None
if match is None:
# not a mention/ID
if guild:
result = discord.utils.get(guild.text_channels, name=argument)
else:
raise commands.BadArgument(f"""Channel named "{argument}" not found in this guild.""")
else:
channel_id = int(match.group(1))
result = bot.get_channel(channel_id)
if not isinstance(result, (discord.TextChannel, discord.abc.PrivateChannel)):
raise commands.BadArgument(f"""Channel "{argument}" not found.""")
return result
# --- Helper functions --- # --- Helper functions ---
def embed_factory(ctx: commands.Context) -> discord.Embed: def embed_factory(ctx: commands.Context) -> discord.Embed:

View File

@ -7,17 +7,19 @@ This file is part of discord-qrm2 and is released under the terms of the GNU
General Public License, version 2. General Public License, version 2.
""" """
import random
import re import re
from collections import OrderedDict from collections import OrderedDict
import random from typing import Union
import discord import discord
import discord.ext.commands as commands import discord.ext.commands as commands
import info import info
import common as cmn
import data.options as opt import data.options as opt
import common as cmn
class QrmHelpCommand(commands.HelpCommand): class QrmHelpCommand(commands.HelpCommand):
@ -171,8 +173,13 @@ class BaseCog(commands.Cog):
@commands.command(name="echo", aliases=["e"], hidden=True) @commands.command(name="echo", aliases=["e"], hidden=True)
@commands.check(cmn.check_if_owner) @commands.check(cmn.check_if_owner)
async def _echo(self, ctx: commands.Context, channel: commands.TextChannelConverter, *, msg: str): async def _echo(self, ctx: commands.Context,
"""Send a message in a channel as qrm. Only works within a server or DM to server, not between servers.""" channel: Union[cmn.GlobalChannelConverter, commands.UserConverter], *, msg: str):
"""Send a message in a channel as qrm. Accepts channel/user IDs/mentions.
Channel names are current-guild only.
Does not work with the ID of the bot user."""
if isinstance(channel, discord.ClientUser):
raise commands.BadArgument("Can't send to the bot user!")
await channel.send(msg) await channel.send(msg)