【问题标题】:Discord.py: Unable to send welcome message after moving the welcome message to another moduleDiscord.py:将欢迎消息移动到另一个模块后无法发送欢迎消息
【发布时间】:2021-06-10 18:22:27
【问题描述】:

我正在尝试使用 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时程序运行良好?

【问题讨论】:

  • 错误说明一切,member 未定义。你可以尝试使用member = discord.Member
  • 您没有从 discord.py 开始所需的最少知识。了解更多 OOP 和一般编程,这对您来说会容易得多。 discord.py 不是为初学者准备的库

标签: python discord discord.py bots


【解决方案1】:

您没有将member 变量从main.py 提供给welcome_message.py。

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

  2. 解决方法:可以把welcome_message.py中的欢迎信息放到一个函数中,传入参数member

welcome_message.py:

def welcomemessage(member):
    ...
    return welcomeMessage

main.py:

import welcome_message
welcome_message.welcomemessage(member)

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2021-06-14
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2021-12-24
    • 2021-10-09
    相关资源
    最近更新 更多