NameError: name #39;client#39; is not defined How exactly do I fix this? (discord bot)(NameError: name client is not defined 我该如何解决这个问题?(不和谐机器人))
问题描述
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))
由于某种原因,我不断收到错误
For some reason I keep getting the error
Traceback(最近一次调用最后一次):第 4 行,在@client.eventNameError: name 'client' 未定义
Traceback (most recent call last): line 4, in @client.event NameError: name 'client' is not defined
推荐答案
你必须初始化你的 Discord 客户端.导入后:
You must initialize your Discord client. After your imports:
bot = discord.Client()
您还应该在定义所有函数和挂钩后运行机器人:
You should also then run the bot, after defining all the functions and hooks:
bot.run('discord_bot_token_here')
on_ready块是空的也是错误代码,所以……盲目修复:
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')
这篇关于NameError: name 'client' is not defined 我该如何解决这个问题?(不和谐机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NameError: name 'client' is not defined 我该如何解决这个问题?(不和谐机器人)
基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
