【问题标题】:discord.py SyntaxError: 'await' outside functiondiscord.py SyntaxError:“等待”外部函数
【发布时间】:2021-06-26 11:07:06
【问题描述】:

当我尝试运行命令时,我总是收到以下错误消息: SyntaxError: 'await' outside function 可能是非常简单的错误,但我真的没有看到其中的错误。有人可以帮忙吗?

import discord
from discord.ext import commands

class unban(commands.Cog):

    def __init__(self, client):
        self.client = client

# Commandok

@commands.command()
async def unban(ctx, *, member): # unindent
    banned_users = await ctx.guild.bans() # unindent
    member_name, member_discriminator = member.split('#') # unindent

    for ban_entry in banned_users:
        user = ban_entry.user

    if (user.name, user.discriminator) == (member_name, member_discriminator):
        await ctx.guild.unban(user)
        await ctx.send(f'Unbanned {user.mention}')
    return

def setup(client):
    client.add_cog(unban(client))

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.unban' 引发错误:TypeError: cogs must derived from Cog sys:1: RuntimeWarning: coroutine 'Command.call' was万万没想到

【问题讨论】:

    标签: python if-statement discord discord.py


    【解决方案1】:

    我认为您的函数定义行可能缩进太多。

    @commands.command()
    async def unban(ctx, *, member): # unindent
        banned_users = await ctx.guild.bans() # unindent
        member_name, member_discriminator = member.split('#') # unindent
    
        for ban_entry in banned_users:
            user = ban_entry.user
    
        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
        return
    

    【讨论】:

    • 我刚刚收到此错误消息:discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.unban' 引发错误:TypeError: cogs must derived from Cog sys:1: RuntimeWarning: coroutine'从未等待过 Command.__call__'
    • 我不知道该代码,因为您从未提及它...请也提供它,您是否尝试在该行的开头添加await
    • 我不明白。我编辑并编写了完整的脚本。
    【解决方案2】:

    您的 if 语句不在您的 for 循环中。这应该有效:

    @commands.command()
    async def unban(ctx, *, member):
        banned_users = await ctx.guild.bans()
        member_name, member_discrinimator = member.split('#')
    
        for ban_entry in banned_users:
            user = ban_entry.user
    
            if (user.name, user.discriminator) == (member_name, member_discrinimator):
                await ctx.guild.unban(user)
    

    【讨论】:

    • 它不起作用,它写了一个错误:discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.unban' raised an error: TypeError: cogs must derive from Cog sys:1: RuntimeWarning: coroutine 'Command.__call__' was never awaited
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2021-01-13
    • 2021-03-05
    • 2022-08-21
    • 2019-03-02
    • 2021-10-03
    相关资源
    最近更新 更多