簡體   English   中英

Discord.py:將歡迎消息移動到另一個模塊后無法發送歡迎消息

[英]Discord.py: Unable to send welcome message after moving the welcome message to another module

我正在嘗試使用 Discord.py 設置歡迎消息,這就是我目前擁有的:

hydrabot/hydra/settings.py

import discord

# Welcomer settings
welcomeChannel = "819624923445985310"

# Bot intents settings
intents = discord.Intents(members=True)

# Command prefix
command_prefix = '$'

# Bot help page
description = f'''Hydra Commands
Hydra's command prefix is "{command_prefix}"
'''

hydrabot/hydra/main.py

import os

import discord
from discord.ext import commands

from settings import(
    intents,
    command_prefix,
    description)
from welcome_message import welcomeMessage

bot = commands.Bot(command_prefix=command_prefix, description=description, intents=intents)
@bot.event

async def on_member_join(member):
    print(f"{member.name} has joined the server. Attempting to send welcome message...")
    await member.send(welcomeMessage)
    print(f'Sent welcome message to {member.name}.')

hydrabot/hydra/welcome_message.py

import discord

welcomeMessage = f'Welcome to the server, {member.mention}!'

每當我運行程序時,我都會收到此錯誤:

Traceback (most recent call last):
  File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\main.py", line 12, in <module>
    from welcome_message import welcomeMessage
  File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\welcome_message.py", line 3, in <module>
    welcomeMessage = f'Welcome to the server, {member.mention}!'
NameError: name 'member' is not defined

我導入discord時沒有定義member嗎? 如果我使用from discord import member而不是import discord會發生以下情況: hydrabot/hydra/welcome_message.py

Traceback (most recent call last):
  File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\main.py", line 12, in <module>
    from welcome_message import welcomeMessage
  File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\welcome_message.py", line 3, in <module>
    welcomeMessage = f'Welcome to the server, {member.mention}!'
AttributeError: module 'discord.member' has no attribute 'mention'

怎么沒有一個名為mention的屬性,但是每當我將歡迎消息移回main.py時程序運行良好?

您沒有將welcome_message.py中的member變量提供給main.py

  1. 解決方法:直接在main.py中定義歡迎信息即可,不值得在其他文件中定義

  2. 解決方案:您可以將歡迎消息放在welcome_message.py 中的function 中,您可以在其中傳遞參數member

歡迎消息.py:

def welcomemessage(member):
    ...
    return welcomeMessage

主要.py:

import welcome_message
welcome_message.welcomemessage(member)

暫無
暫無

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

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