More work on the slash transition

This commit is contained in:
2021-08-03 12:48:22 +02:00
parent 025294c633
commit 9c0adf1cae
2 changed files with 128 additions and 55 deletions

181
bot.py
View File

@@ -17,6 +17,8 @@ from lib.config import config, config_load, config_save, config_get, config_set,
config_set_raw, config_meta config_set_raw, config_meta
from lib.utils import async_filter, find_category, find_role_case_insensitive, link_channel from lib.utils import async_filter, find_category, find_role_case_insensitive, link_channel
VERSION = "1.0.0"
load_dotenv() load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN') TOKEN = os.getenv('DISCORD_TOKEN')
@@ -219,52 +221,6 @@ async def on_voice_state_update(member: discord.Member, before: discord.VoiceSta
await channel.delete(reason="Delete temporary group channel when last person left") await channel.delete(reason="Delete temporary group channel when last person left")
@bot.command(name='random_message', brief='Select a random message from a channel')
async def random_message_command(ctx: commands.Context, channel: Optional[discord.TextChannel] = None,
max_cnt: Optional[int] = 100, reaction_filter: Optional[str] = None):
typing = ctx.channel.trigger_typing()
if channel is None:
channel = ctx.channel
messages = async_filter(lambda m: _is_message_valid_for_selection(m, reaction_filter),
channel.history(limit=max_cnt))
messages = [item async for item in messages]
if not messages:
await typing
await ctx.channel.send("No valid messages found!")
return
msg: discord.Message = random.choice(messages)
author: discord.abc.User = msg.author
embed = discord.Embed(
description=msg.content + "\n\n[Go to message](" + msg.jump_url + ")"
)
embed.set_author(name=author.display_name, icon_url=author.avatar_url)
embed.set_footer(text="random message from #" + channel.name + " out of " + str(len(messages)) + " messages")
await typing
await ctx.channel.send(embed=embed)
@bot.command(name='collect_messages', brief='Lists all messages with the given filters')
async def collect_messages_command(ctx: commands.Context, channel: Optional[discord.TextChannel] = None,
max_cnt: Optional[int] = 100, reaction_filter: Optional[str] = None):
typing = ctx.channel.trigger_typing()
if channel is None:
channel = ctx.channel
messages = async_filter(lambda m: _is_message_valid_for_selection(m, reaction_filter),
channel.history(limit=max_cnt))
messages = [item async for item in messages]
embed = discord.Embed(
title="List of matching messages",
description='\n'.join([m.content for m in messages])
)
await typing
await ctx.channel.send(embed=embed)
@bot.command(name='flip_pipelin', brief='Flip a pipelin')
async def flip_pipelin_command(ctx: commands.Context):
await ctx.channel.send("Pipelin " + random.choice(['schnel', 'langsam']))
def _is_message_valid_for_selection(message: discord.Message, reaction_filter: Optional[str] = None) -> bool: def _is_message_valid_for_selection(message: discord.Message, reaction_filter: Optional[str] = None) -> bool:
if message.clean_content.strip() == '': if message.clean_content.strip() == '':
return False return False
@@ -357,7 +313,122 @@ async def check_text_channel(ctx: SlashContext, channel) -> bool:
return True return True
# ____ ____ ___ _ _ ____ ____ # __ __ ___ ____ ____
# | \/ |_ _/ ___| / ___|
# | |\/| || |\___ \| |
# | | | || | ___) | |___
# |_| |_|___|____/ \____|
@slash.slash(
name="about",
description="Shows information about this bot",
guild_ids=slash_guild_ids
)
async def about_slash(ctx: SlashContext):
embed = discord.Embed(
color=discord.Colour.from_rgb(200, 0, 0),
title="About Och bot",
description="The Och bot is a special bot created by Siphalor filled with features and inside jokes of his "
"university group.",
)
embed.add_field(name="version", value=VERSION)
await ctx.send(embed=embed, hidden=True)
@slash.slash(
name="random-message",
description="Select a random message from a channel",
options=[
create_option(
name="channel",
description="The channel to pull the messages from",
option_type=SlashCommandOptionType.CHANNEL,
required=False
),
create_option(
name="max_messages",
description="The maximum amount of messages to index (max 1024, default 100)",
option_type=SlashCommandOptionType.INTEGER,
required=False
),
create_option(
name="reaction",
description="Only pull from messages that have this reaction",
option_type=str,
required=False
)
],
guild_ids=slash_guild_ids
)
async def random_message_slash(ctx: SlashContext, channel: Optional[discord.abc.GuildChannel] = None,
max_messages: int = 100, reaction: Optional[str] = None):
await ctx.defer()
if channel is None:
channel = ctx.channel
messages = async_filter(lambda m: _is_message_valid_for_selection(m, reaction), channel.history(limit=max_messages))
messages = [item async for item in messages]
if not messages:
await ctx.send("No valid messages found!")
msg: discord.Message = random.choice(messages)
author: discord.abc.User = msg.author
embed = discord.Embed(
description=msg.content + "\n\n[Jump to message](" + msg.jump_url + ")"
)
embed.set_author(name=author.display_name, icon_url=author.avatar_url)
embed.set_footer(text="random message from #" + channel.name + " out of " + str(len(messages)) + "messages")
await ctx.send(embed=embed)
@slash.slash(
name="list-messages",
description="Lists all messages with the given filtes",
options=[
create_option(
name="channel",
description="The channel to pull the messages from",
option_type=SlashCommandOptionType.CHANNEL,
required=False
),
create_option(
name="max_messages",
description="The maximum amount of messages to index (max 1024, default 100)",
option_type=SlashCommandOptionType.INTEGER,
required=False
),
create_option(
name="reaction",
description="Only pull from messages that have this reaction",
option_type=str,
required=False
)
],
guild_ids=slash_guild_ids
)
async def list_messages_slash(ctx: SlashContext, channel: Optional[discord.abc.GuildChannel] = None,
max_messages: int = 100, reaction: Optional[str] = None):
await ctx.defer()
if channel is None:
channel = ctx.channel
messages = async_filter(lambda m: _is_message_valid_for_selection(m, reaction), channel.history(limit=max_messages))
messages = [item async for item in messages]
embed = discord.Embed(
title="List of matching messages",
description="\n".join([m.content for m in messages])
)
await ctx.send(embed=embed)
@slash.slash(
name="flip-pipelin",
description="Toss a pipelin to your favourite teacher and see whether it's schnel or slo"
)
async def flip_pipelin_slash(ctx: SlashContext):
await ctx.channel.send("Pipelin " + random.choice(['schnel', 'slo']))
# ____ ____ ___ _ _ ____ ____
# / ___| _ \ / _ \| | | | _ \/ ___| # / ___| _ \ / _ \| | | | _ \/ ___|
# | | _| |_) | | | | | | | |_) \___ \ # | | _| |_) | | | | | | | |_) \___ \
# | |_| | _ <| |_| | |_| | __/ ___) | # | |_| | _ <| |_| | |_| | __/ ___) |
@@ -444,7 +515,7 @@ async def groups_list_slash(ctx: SlashContext):
create_option( create_option(
name="group_channel", name="group_channel",
description="The group identified by it's channel", description="The group identified by it's channel",
option_type=discord.abc.GuildChannel, option_type=SlashCommandOptionType.CHANNEL,
required=True required=True
) )
], ],
@@ -483,7 +554,7 @@ async def groups_archive_slash(ctx: SlashContext, group_channel: discord.abc.Gui
create_option( create_option(
name="group_channel", name="group_channel",
description="The group identified by it's channel", description="The group identified by it's channel",
option_type=discord.abc.GuildChannel, option_type=SlashCommandOptionType.CHANNEL,
required=True required=True
) )
], ],
@@ -519,7 +590,7 @@ async def groups_archive_slash(ctx: SlashContext, group_channel: discord.abc.Gui
create_option( create_option(
name="group_channel", name="group_channel",
description="The group identified by it's channel", description="The group identified by it's channel",
option_type=discord.abc.GuildChannel, option_type=SlashCommandOptionType.CHANNEL,
required=True required=True
) )
], ],
@@ -587,7 +658,7 @@ async def groups_create_slash(ctx: SlashContext, name: str, members: Optional[st
cor = channel.edit(sync_permissions=True) cor = channel.edit(sync_permissions=True)
role: discord.Role = await cor_role role: discord.Role = await cor_role
await cor await cor
await channel.set_permissions(role, reason="Create group " + name, read_messages=True) await channel.set_permissions(role, reason="Create group " + name, read_messages=True, manage_threads=True)
extra = "" extra = ""
if members is not None: if members is not None:
@@ -843,7 +914,8 @@ async def quote_add_slash(ctx: SlashContext, author: str, quote: str):
], ],
guild_ids=slash_guild_ids guild_ids=slash_guild_ids
) )
async def quote_remove_slash(ctx: SlashContext, author: str, quote: Optional[str] = None, quote_position: Optional[int] = None): async def quote_remove_slash(ctx: SlashContext, author: str, quote: Optional[str] = None,
quote_position: Optional[int] = None):
if not await check_slash_context(ctx, True): if not await check_slash_context(ctx, True):
return return
@@ -883,7 +955,9 @@ async def quote_remove_slash(ctx: SlashContext, author: str, quote: Optional[str
config_save() config_save()
return return
await ctx.send("Invalid quote position - must be within the range of 0 and " + str(len(author_quotes)) + "!", hidden=True) await ctx.send(
"Invalid quote position - must be within the range of 0 and " + str(len(author_quotes)) + "!",
hidden=True)
else: else:
await ctx.send("Either quote or quote_position must be used!", hidden=True) await ctx.send("Either quote or quote_position must be used!", hidden=True)
@@ -894,7 +968,6 @@ async def quote_remove_slash(ctx: SlashContext, author: str, quote: Optional[str
# | | | | | | \| | |_ | | | _ # | | | | | | \| | |_ | | | _
# | |__| |_| | |\ | _| | | |_| | # | |__| |_| | |\ | _| | | |_| |
# \____\___/|_| \_|_| |___\____| # \____\___/|_| \_|_| |___\____|
@slash.slash( @slash.slash(
name="config", name="config",

View File

@@ -1,4 +1,4 @@
discord git+git://github.com/Rapptz/discord.py@58ca9e9
discord-py-slash-command>=2 discord-py-slash-command>=2
python-dotenv python-dotenv
PyNaCl PyNaCl