Discord机器人程序中的NameError [英] NameError in a Discord bot program

查看:111
本文介绍了Discord机器人程序中的NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started writing my first Discord bot, but I ran into a little problem. Why is my code not working ?

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

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

On the idea this command writes to users of the server on behalf of the 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编程的初学者

I am quite a beginner in python programming

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

I tried to create a new bot, but got a new error

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的完整代码.

Its full code of bot w/o token.

================= 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'

推荐答案

message.server中的messagemessage命令,没有server. (这就是错误告诉您的内容).您应该传递pass_context=True

The message in message.server is the message command, which doesn't have a server. (That's what the error is telling you) You should pass the invocation context into the command with pass_context=True

@FiveStarBot.command(pass_context=True)
async def message(ctx, member: discord.Member, *, text : str):
    await FiveStarBot.send_message(member, text)

这利用了 discord.py转换器为您进行名称查找

This takes advantage of a discord.py converter to do the name lookup for you

这篇关于Discord机器人程序中的NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆