簡體   English   中英

類型錯誤:on_message() 缺少 1 個必需的位置參數:“成員”discord.py

[英]TypeError: on_message() missing 1 required positional argument: 'Member' discord.py

我正在處理的報告命令有問題。 每當我在 discord 服務器上運行命令時,都會出現錯誤“TypeError: on_message() missing 1 required positional argument: 'Member'”。 如果有人知道如何解決這個問題,你能告訴我嗎? 感謝您花時間閱讀本文。

# bot.py
from discord import Member
import os
import discord
from dotenv import load_dotenv
load_dotenv()
DISCORD_TOKEN = ''

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

@client.event
async def on_message(message, Member):

    Member = discord.Member(Member)

    if message.author == client.user:
        return

    if message.content.startswith('$report ', Member.mention):
        Sends = 'That user has been reported!'
        await message.channel.send(Sends)

client.run(DISCORD_TOKEN)

我可以看到您是 discord.py 的新手。 首先,on_message function 不帶成員。 它只需要消息。 當您將事件添加到代碼中時,您總是希望檢查事件參考並檢查該事件的參數。 您不能添加或刪除參數。 對於這種情況,您的代碼將像這樣開始

@client.event
async def on_message(message)

要獲取您將使用的成員:

member = message.author

另外我建議您使用命令

from discord.ext import commands

這會容易得多,您可以添加自己的參數。 我已經在命令中舉例說明了您對 on_message function 所做的事情

@client.command
async def report(ctx, member : discord.Member, reason=None)
    await ctx.send(f"Reported {member.mention} for {reason}")

還要記住,如果你有一個 on_message function,除非你最后使用 bot.process_commands,否則你的命令將不起作用。 閱讀有關如何執行此操作的常見問題解答。 這是鏈接:為什么 on_message 使我的命令停止工作? https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM