NameError in a Discord bot program

自古美人都是妖i 提交于 2020-01-25 07:39:17

问题


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)

On the idea this command writes to users of the server on behalf of the bot

Error:

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

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)

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'

回答1:


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)

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



来源:https://stackoverflow.com/questions/51465704/nameerror-in-a-discord-bot-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!