Add slash commands

Use /remind instead of !remind. You need to reinvite your bot with the "applications.commands" scope. Discords Bot API can take up to 1 hour to register the commands so you will have to wait a bit before use.
This commit is contained in:
2021-04-22 02:04:24 +02:00
parent c06424184f
commit 23bbf5cbc2
2 changed files with 33 additions and 12 deletions

42
main.py
View File

@ -8,16 +8,16 @@ import pytz
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
from discord.ext import commands from discord.ext import commands
from discord_slash import SlashCommand
from discord_slash.utils.manage_commands import create_option
from dotenv import load_dotenv from dotenv import load_dotenv
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot( bot = commands.Bot(
command_prefix="!", command_prefix="!",
description="Reminder bot for Discord by TheLovinator#9276", description="Reminder bot for Discord by TheLovinator#9276",
intents=intents, intents=discord.Intents.all(),
) )
slash = SlashCommand(bot, sync_commands=True)
@bot.event @bot.event
@ -38,8 +38,11 @@ async def on_ready():
logging.info(f"Logged in as {bot.user.name} ({bot.user.id})") logging.info(f"Logged in as {bot.user.name} ({bot.user.id})")
@bot.command() @slash.slash(
async def reminders(ctx): name="reminders",
description="Show reminders.",
)
async def do_reminders(ctx):
embed = discord.Embed( embed = discord.Embed(
colour=discord.Colour.random(), colour=discord.Colour.random(),
title="discord-reminder-bot by TheLovinator#9276", title="discord-reminder-bot by TheLovinator#9276",
@ -87,8 +90,25 @@ async def reminders(ctx):
await ctx.send(embed=embed) await ctx.send(embed=embed)
@bot.command(aliases=["reminder", "remindme", "at"]) @slash.slash(
async def remind(ctx, message_date: str, message_reason: str): name="remind",
description="Set a reminder.",
options=[
create_option(
name="message_reason",
description="The message I should send when I notify you.",
option_type=3, # String
required=True,
),
create_option(
name="message_date",
description="The time or date I should write in this channel.",
option_type=3, # String
required=True,
),
],
)
async def do_remind(ctx, message_date: str, message_reason: str):
logging.info(f"New Discord message: {ctx.message}") logging.info(f"New Discord message: {ctx.message}")
parsed_date = dateparser.parse( parsed_date = dateparser.parse(
@ -111,14 +131,14 @@ async def remind(ctx, message_date: str, message_reason: str):
send_to_discord, send_to_discord,
run_date=remove_timezone_from_date, run_date=remove_timezone_from_date,
kwargs={ kwargs={
"channel_id": ctx.channel.id, "channel_id": ctx.channel_id,
"message": message_reason, "message": message_reason,
"author_id": ctx.message.author.id, "author_id": ctx.author_id,
}, },
) )
logging.debug(f"Job id: '{job.id}', name: '{job.name}' and kwargs: '{job.kwargs}'") logging.debug(f"Job id: '{job.id}', name: '{job.name}' and kwargs: '{job.kwargs}'")
message = ( message = (
f"Hello {ctx.message.author.name}, I will notify you at:\n" f"Hello {ctx.author.display_name}, I will notify you at:\n"
f"**{remove_timezone_from_date}**\n" f"**{remove_timezone_from_date}**\n"
f"With the message:\n**{message_reason}**. " f"With the message:\n**{message_reason}**. "
) )

View File

@ -4,6 +4,7 @@ async-timeout==3.0.1
attrs==20.3.0 attrs==20.3.0
chardet==4.0.0 chardet==4.0.0
dateparser==1.0.0 dateparser==1.0.0
discord-py-slash-command==1.1.2
discord.py==1.7.1 discord.py==1.7.1
greenlet==1.0.0 greenlet==1.0.0
idna==3.1 idna==3.1
@ -13,7 +14,7 @@ python-dotenv==0.17.0
pytz==2021.1 pytz==2021.1
regex==2021.4.4 regex==2021.4.4
six==1.15.0 six==1.15.0
SQLAlchemy==1.4.8 SQLAlchemy==1.4.10
typing-extensions==3.7.4.3 typing-extensions==3.7.4.3
tzlocal==2.1 tzlocal==2.1
yarl==1.6.3 yarl==1.6.3