NameError:未定义名称“ client”(客户端),我该如何解决? (discord bot)

时间:2020-08-21 05:50:31

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

import discord
from discord.ext import commands

@client.event
async def on_ready():
@bot.event
async def on_message(message):
    if len(message.content) > 250 or message.author.bot:
        return
    if message.guild:
        messageL = f"{message.author.name.replace(message.author.discriminator, '')} posted: '{message.content}'"
        success1 = await SendHomeMML(messageL)
        if success1 is None:
            print("Message Log message failed.")
        descE = f"{message.author.name.replace(message.author.discriminator, '')} posted: \n'{message.content}'\n" \
            f"This was in a Guild titled '{message.guild.name}' within Channel '{message.channel.name}'\n"
        MessageE = discord.Embed(title="Message Log", description=descE, colour=8421376)
        MessageE.set_footer(text=f"Posted on: {message.created_at.isoformat(' ')}")
        success2 = await SendHomeEML(MessageE)
        if success2 is None:
            print("Message Log embed failed.")
        # and so on...

# Some time later... #

async def SendHomeEML(embedded):
    return await bot.get_channel(xxxxxx).send(embed=embedded)

async def SendHomeMML(message):
    return await bot.get_channel(xxxxxx).send(content=discord.utils.escape_mentions(message))

由于某些原因,我不断收到错误消息

回溯(最近通话最近): 第4行,在 @ client.event NameError:未定义名称“客户端”

2 个答案:

答案 0 :(得分:1)

您必须初始化Discord客户端。导入后:

bot = discord.Client()

在定义所有功能和钩子之后,还应该运行该机器人:

bot.run('discord_bot_token_here')

还有一个错误的代码,就是on_ready块为空,所以……盲目地修复它:

import discord
from discord.ext import commands

bot = discord.Client()

@bot.event
async def on_ready():
    # I moved this line that was hanging around in your main, since it would fail.
    # But you know better where to place it.
    bot.get_channel(xxxxxx).send(content=discord.utils.escape_mentions(message))

@bot.event
async def on_message(message):
    if len(message.content) > 250 or message.author.bot:
        return
    if message.guild:
        messageL = f"{message.author.name.replace(message.author.discriminator, '')} posted: '{message.content}'"
        success1 = await SendHomeMML(messageL)
        if success1 is None:
            print("Message Log message failed.")
        descE = f"{message.author.name.replace(message.author.discriminator, '')} posted: \n'{message.content}'\n" \
            f"This was in a Guild titled '{message.guild.name}' within Channel '{message.channel.name}'\n"
        MessageE = discord.Embed(title="Message Log", description=descE, colour=8421376)
        MessageE.set_footer(text=f"Posted on: {message.created_at.isoformat(' ')}")
        success2 = await SendHomeEML(MessageE)
        if success2 is None:
            print("Message Log embed failed.")
        # and so on...

# Some time later... #

async def SendHomeEML(embedded):
    return await bot.get_channel(xxxxxx).send(embed=embedded)

async def SendHomeMML(message):
    return await 



bot.run('discord_bot_token_here')

答案 1 :(得分:0)

哈哈,真的很简单,您必须定义客户端。就是这样做

client = commands.Bot(command_prefix="whatever prefix you want")

@client.event()
async def on_ready():
print("Signed In")

@client.event
async def on_message(message):
    if len(message.content) > 250 or message.author.bot:
        return
    if message.guild:
        messageL = f"{message.author.name.replace(message.author.discriminator, '')} posted: '{message.content}'"
        success1 = await SendHomeMML(messageL)
        if success1 is None:
            print("Message Log message failed.")
        descE = f"{message.author.name.replace(message.author.discriminator, '')} posted: \n'{message.content}'\n" \
            f"This was in a Guild titled '{message.guild.name}' within Channel '{message.channel.name}'\n"
        MessageE = discord.Embed(title="Message Log", description=descE, colour=8421376)
        MessageE.set_footer(text=f"Posted on: {message.created_at.isoformat(' ')}")
        success2 = await SendHomeEML(MessageE)
        if success2 is None:
            print("Message Log embed failed.")
        # and so on...

# Some time later... #

async def SendHomeEML(embedded):
    return await bot.get_channel(xxxxxx).send(embed=embedded)

async def SendHomeMML(message):
    return await bot.get_channel(xxxxxx).send(content=discord.utils.escape_mentions(message))

希望这可以帮助您:) 还要记住client.run("TOKEN")。 :>