python - 尝试使用 discord.py 让机器人作为命令发起者发送消息
问题描述
我正在尝试在不和谐上创建一个机器人来用语气指示器标记事物(即 /s 用于讽刺)这个项目一开始只是一个库,但后来我意识到我可以让它将语气指示器附加到消息的末尾通过将其用作论据。但是,我只能让机器人自己发送它,这令人失望,因为它打断了对话的流程。我正在使用python编写它。这是我的代码:
import discord
from discord_slash import SlashCommand, SlashContext # Importing the newly installed library.
client = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(client, sync_commands=True) # Declares slash commands through the client.
guild_ids = [guild id] # Put your server ID in this array.
@client.event
async def on_ready():
print("Ready!")
@slash.slash(name="j", description="joking", guild_ids=guild_ids)
async def j(ctx:SlashContext, message:str): # Defines a new "context" (ctx) command called "ping."
await ctx.respond(eat=True)
send_message = await ctx.send(f"{message} /j")
def check(message):
return message.author.id == ctx.author_id
try:
answer = await bot.wait_for('message', check=check, timeout = 60.0)
except asyncio.TimeoutError:
await ctx.send("You took to long to answer...", hidden=True)
else:
# do something with answer (convert to tone indicator)
pass
client.run("bot token")
我知道我实际上不需要任何最后一部分,但有人建议我作为一种解决方法使其成为用户的 ID。它什么也不返回。任何人都知道如何让它作为输入命令的用户发送,或者像这样屏蔽自己?
解决方案
您可以为此使用 webhook
#inside your command
guild = client.get_channel(ctx.channel_id)
if channel is None:
#error here
return
webhooks = filter(lambda x: x.user == client.user, await channel.webhooks()) #gets only webhooks created by us
if len(webhooks) == 0:
webhook = await channel.create_webhook(name='something')
else: webhook = webhooks[0]
await webhook.send(content, username=ctx.author.username, avatar = ctx.author.avatar_url
参考:
推荐阅读
- c# - DirectoryInfo 从不同方法调用时返回不同的值
- python - 无法在 Win10 上运行 tensorflow
- laravel - vps服务器上的docker问题
- python - AIX 中的库文件 libpython3.7m.a 是什么?静态库还是共享库?
- git - 主机密钥验证失败请确保您拥有正确的访问权限
- javascript - 上传表单后,我的反应多选不为空
- ios - 仅取消今天重复的 UNNotification
- json - 将 AWS S3 中的 json 文件合并到一个文件中
- installation - 搜索独立于平台的安装程序
- typescript - Typescript - 传入多个键值时缩小函数中的 T[K]