本文介紹了NameError: name 'client' is not defined 我該如何解決這個(gè)問題?(不和諧機(jī)器人)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
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:
'{message.content}'
"
f"This was in a Guild titled '{message.guild.name}' within Channel '{message.channel.name}'
"
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))
由于某種原因,我不斷收到錯(cuò)誤
For some reason I keep getting the error
Traceback(最近一次調(diào)用最后一次):第 4 行,在@client.eventNameError: name 'client' 未定義
Traceback (most recent call last): line 4, in @client.event NameError: name 'client' is not defined
推薦答案
你必須初始化你的 Discord 客戶端.導(dǎo)入后:
You must initialize your Discord client. After your imports:
bot = discord.Client()
您還應(yīng)該在定義所有函數(shù)和掛鉤后運(yùn)行機(jī)器人:
You should also then run the bot, after defining all the functions and hooks:
bot.run('discord_bot_token_here')
on_ready
塊是空的也是錯(cuò)誤代碼,所以……盲目修復(fù):
There is also wrong code in that the on_ready
block is empty, so... blindly fixing it:
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:
'{message.content}'
"
f"This was in a Guild titled '{message.guild.name}' within Channel '{message.channel.name}'
"
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')
這篇關(guān)于NameError: name 'client' is not defined 我該如何解決這個(gè)問題?(不和諧機(jī)器人)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!