首页 > 解决方案 > Discordpy 商店角色以字符串形式提及

问题描述

所以我有这个命令可以让机器人在我想要的任何频道中写入,问题是我也希望能够提及它,所以我尝试更改以 @ 开头的 args 并将其转换为角色提及,但似乎我不能只在 args 中存储角色提及。有什么建议可以通过提及来更改某些参数吗?

async def echo(ctx, id, *echowords: str):
if ctx.message.author.id in [212620735754010624]:
    sendchannel = client.get_channel(int(id))
    for i in range(len(echowords)):
        if echowords[i].startswith("@ "):
            echowords[i] = get(
                sendchannel.guild.roles, name=echowords[i].replace("@ ", "")
            ).mention

    await sendchannel.send(" ".join(echowords[:]))
else:
    await ctx.send("Bot developers only :<")

标签: pythondiscorddiscord.py

解决方案


最后,这一切都使用列表而不是元组解决了。

async def echo(ctx, id, *echowords: str):
if ctx.message.author.id in [212620735754010624]:
    msg = list()
    sendchannel = client.get_channel(int(id))
    for i in range(len(echowords)):
        if echowords[i].startswith("@ "):
            msg.append(
                get(
                    sendchannel.guild.roles, name=echowords[i].replace("@ ", "")
                ).mention
            )
        else:
            print(str(i))
            msg.append(echowords[i])
    await sendchannel.send(" ".join(msg[:]))
else:
    await ctx.send("Bot developers only :<")

推荐阅读