[英]How can I convert a Discord bot command to a Discord slash command in Python?
I have a Python Discord bot that uses the discord.ext.commands module to implement a /last_message command that accepts a discord.Member object as an argument.我有一个 Python Discord 机器人,它使用 discord.ext.commands 模块来实现 /last_message 命令,该命令接受 discord.Member object 作为参数。
I want to convert this command to a slash command, so that users can type /last_message @username and pass @username as an argument.我想将此命令转换为斜杠命令,以便用户可以键入 /last_message @username 并将@username 作为参数传递。 I've tried adding the @bot.slash_command() decorator instead of @bot.command(), but the command doesn't seem to work when I try to use it because I'am using Replit and it seems Replit doesn't support slash commands.
我已经尝试添加 @bot.slash_command() 装饰器而不是 @bot.command(),但是当我尝试使用它时该命令似乎不起作用,因为我正在使用 Replit 并且似乎 Replit 没有支持斜杠命令。
Can someone provide an example of how to properly convert a Discord bot command to a Discord slash command in Pythonfor this last_message command?有人可以提供一个示例,说明如何将 Discord bot 命令正确转换为 Python 中的 Discord 斜线命令以用于此 last_message 命令吗?
I tried this code:我试过这段代码:
import discord
from discord.ext import commands
from datetime import timedelta, datetime
import pytz
from webserver import keep_alive
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='/', intents=intents)
@bot.command(name='last_message')
async def last_message(ctx, user: discord.Member):
newest_message = None
newest_channel = None
for channel in bot.get_all_channels():
if isinstance(channel, discord.TextChannel):
async for message in channel.history(limit=300):
if message.author == user:
if newest_message is None or message.created_at > newest_message.created_at:
newest_message = message
newest_channel = channel
if newest_message is not None:
now = datetime.now(pytz.utc)
message_age = now - newest_message.created_at
days_old = message_age.days
response_time = newest_message.created_at + timedelta(hours=1)
response = f"Newest message by {user.mention} is:\n\n{newest_message.content}\n\nSent by {user.display_name} in {newest_channel.mention} on {response_time.strftime('%b %d, %Y %I:%M %p')}\n\nIt was sent {days_old} days ago."
else:
response = f"No messages found for {user.display_name} ({user.name})."
await ctx.send(response)
keep_alive()
bot.run('MY TOKEN')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.