【问题标题】:Trying to use discord.py to make the bot send a message as the command originator尝试使用 discord.py 让机器人作为命令发起者发送消息
【发布时间】:2021-06-02 12:21:39
【问题描述】:

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

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

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

    参考资料:

    【讨论】:

    • 所以我尝试在await ctx.respond(eat=True) 行之后立即实施此解决方案,但发现它只是吃掉了消息,然后什么也没发生。我认为这是某种链接问题,所以我尝试将await webhook.send(content, username=ctx.author.username, avatar = ctx.author.avatar_url) 更改为await webhook.send(f"{meesage} /j", username=ctx.author.username, avatar = ctx.author.avatar_url)。机器人似乎只是在吃信息,没有给我任何输出。我知道 python 对订单很挑剔,所以我的位置有问题还是其他问题?
    • 我的错,混淆了 discord_slash 的 ctx 和 discord.py 的 ctx。现在更新答案
    猜你喜欢
    • 2019-09-19
    • 2022-01-03
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2020-11-04
    • 2020-10-10
    相关资源
    最近更新 更多