对于python来说有点陌生,而且让它开始工作也变得毫无意义。在输入命令时,我在第23行中没有提到以下错误:
'NoneType‘对象没有属性’‘
我不知道为什么它试图使用这个响应部分作为它的时候,当有一个提到,无用的我错过了什么。
import discord
from redbot.core import commands
import random
class boop(commands.Cog):
"""My custom cog"""
def __init__(self, bot):
self.bot = bot
@commands.command()
async def boop(self, ctx, user: discord.User=None):
"""Boop Someone"""
# Your code will go here
author_name = ctx.author.mention
member = random.choice(ctx.guild.members)
randomuser = [f"{author_name} booped {member.mention}",
f"{author_name} booped {member.mention} on the nose",
f"{author_name} booped {member.mention}'s snoot",
f"{author_name} gave {member.mention} a boop"]
mentionuser = [f"{author_name} booped {user.mention}",
f"{author_name} booped {user.mention} on the nose",
f"{author_name} booped {user.mention}'s snoot",
f"{author_name} gave {user.mention} a boop"]
if not user:
await ctx.send(random.choice(randomuser))
else:
await ctx.send(random.choice(mentionuser))发布于 2022-01-09 21:42:48
运行命令时,user变为None,因为这是函数定义中设置的参数的默认值。
然后:
mentionuser = [f"{author_name} booped {user.mention}",
f"{author_name} booped {user.mention} on the nose",
f"{author_name} booped {user.mention}'s snoot",
f"{author_name} gave {user.mention} a boop"]您正在尝试检索mention属性的NoneType。这就是错误产生的原因。要避免这种情况,请将mentionuser定义包括在else块中:
...
else:
mentionuser = [f"{author_name} booped {user.mention}",
f"{author_name} booped {user.mention} on the nose",
f"{author_name} booped {user.mention}'s snoot",
f"{author_name} gave {user.mention} a boop"]
await ctx.send(random.choice(mentionuser))发布于 2022-01-10 15:04:24
@bot.command()
async def boop(ctx, target:discord.Member=None):
author=ctx.author.mention
if not target:
members=ctx.guild.members
target=random.choice(members)
target=target.mention
responses=[f'{author} has booped {target}!', f'{target}, {author} has booped you!']
await ctx.send(random.choice(responses))这对我有用,但我不使用齿轮,我的客户电话也不一样。我认为您的问题在于您使用的是用户对象而不是成员。
https://stackoverflow.com/questions/70645651
复制相似问题