等待消息发送可能超时

时间:2019-05-13 01:01:24

标签: python python-3.x discord.py discord.py-rewrite

嗨,我遇到了一个问题,我的机器人多次发送了await ctx.send(f":white_check_mark: Member(s) {member_names} kicked from voice channel.")消息。这似乎发生在await member.move_to(channel = None, reason=None)上,例如如果语音通道中有4位成员被踢出,它将发送4条消息。

有什么办法可以阻止这种情况的发生。

这是我的代码:

 async def voicekick(self, ctx, *members: discord.Member):
    """Kicks a member from voice and restricts them from joining all voice channels.

    - member: The member to kick
    """
    vcr = get(ctx.guild.roles, name="VC Restricted")
    for member in members:
        member_names = ', '.join([x.name for x in members])
        if member.voice is None:
            await ctx.send(f"Member(s) {member_names} not in a voice channel.")
        else:
            await member.move_to(channel = None, reason=None)
            await member.add_roles(vcr)
        await ctx.send(f":white_check_mark: Member(s) {member_names} kicked from voice channel.")

1 个答案:

答案 0 :(得分:1)

只要您踢出一个成员,就将其名称添加到列表中,然后在您踢出所有成员后即可显示这些名称:

async def voicekick(self, ctx, *members: discord.Member):
    """Kicks a member from voice and restricts them from joining all voice channels.

    - member: The member to kick
    """
    vcr = get(ctx.guild.roles, name="VC Restricted")
    kicked_names = []
    for member in members:
        if member.voice is None:
            await ctx.send(f"Member(s) {member_names} not in a voice channel.")
        else:
            await member.move_to(channel = None, reason=None)
            await member.add_roles(vcr)
            kicked_names.append(member.name)
    kicked_names = ", ".join(kicked_names)
    await ctx.send(f":white_check_mark: Member(s) {kicked_names} kicked from voice channel.")