首页 > 解决方案 > 尝试使用 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。它什么也不返回。任何人都知道如何让它作为输入命令的用户发送,或者像这样屏蔽自己?

标签: pythondiscorddiscord.pybots

解决方案


您可以为此使用 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

参考:


推荐阅读