尝试使用 discord.py 使机器人作为命令发起者发送消息

时间:2021-03-03 21:23:21

标签: python discord discord.py bots

我正在尝试创建一个不和谐的机器人来用语气指示符标记事物(即 /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。它什么都不返回。任何人都知道如何让它以输入命令的用户身份发送,或者像这样屏蔽自己?

1 个答案:

答案 0 :(得分:1)

您可以为此使用网络钩子

#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

参考: