Discord机器人程序中的NameError

时间:2018-07-22 13:44:29

标签: python nameerror discord.py

我开始编写我的第一个Discord机器人,但是遇到了一个小问题。为什么我的代码不起作用?

@bot.command()
async def msg(user : str, text : str):
    s = message.server
    await bot.send_message(s.get_member_named(name = user), text)

基于这个想法,该命令代表bot写入服务器用户

错误:

================== RESTART: C:/Users/Roman-PC/Desktop/t2.py ==================
Logged in as
FiveStar Role Play
470014458215792641
------
Ignoring exception in command msg
Traceback (most recent call last):
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "C:/Users/Roman-PC/Desktop/t2.py", line 47, in msg
    s = message.server
NameError: name 'message' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'message' is not defined

我是python编程的初学者


我尝试创建一个新的漫游器,但遇到了新的错误

import discord
from discord.ext import commands
import random

FiveStarBot = commands.Bot(command_prefix='!')
client = discord.Client()

@FiveStarBot.event
async def on_ready():
    print('Connection success!')
    print('Name: ',FiveStarBot.user.name)
    print('ID: ',FiveStarBot.user.id)

@FiveStarBot.command()
async def message(name : str, text : str):
    s = message.server
    await FiveStarBot.send_message(s.get_member_named(name = name), text)

其没有代币的bot的完整代码。

================= RESTART: C:/Users/Roman-PC/Desktop/ggg.py =================
Connection success!
Name:  FiveStar Role Play
ID:  470014458215792641
Ignoring exception in command message
Traceback (most recent call last):
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "C:/Users/Roman-PC/Desktop/ggg.py", line 16, in message
    s = message.server
AttributeError: 'Command' object has no attribute 'server'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Command' object has no attribute 'server'

1 个答案:

答案 0 :(得分:0)

message中的message.servermessage命令,它没有server。 (这就是错误告诉您的内容)。您应该使用pass_context=True

invocation context传递到命令中
@FiveStarBot.command(pass_context=True)
async def message(ctx, member: discord.Member, *, text : str):
    await FiveStarBot.send_message(member, text)

这利用discord.py converter的优势为您查找名称