Discord机器人程序中的NameError

编程入门 行业动态 更新时间:2024-10-10 23:17:00
本文介绍了Discord机器人程序中的NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

@botmand() 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.extmands.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) @FiveStarBotmand() 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.extmands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Command' object has no attribute 'server'

推荐答案

message.server中的message是message命令,没有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

@FiveStarBotmand(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

本文发布于:2023-11-23 13:10:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机器人   程序   Discord   NameError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!