Python 与cogs的不和谐问候,在成员加入时,不起作用

Python 与cogs的不和谐问候,在成员加入时,不起作用,python,discord.py,Python,Discord.py,我的on_member_join侦听器未正确执行。这个想法是,当一个新的人进入我的不和谐,我的机器人用一个定制的消息来迎接他们 没有语法错误,类加载正确。on_ready侦听器正确响应Welcome:on。如果我尝试进行调试打印,则不会执行 我哪里做错了?我不明白,我觉得这是对的 import discord from discord.ext import commands intents = discord.Intents.default() intents.members = True c

我的on_member_join侦听器未正确执行。这个想法是,当一个新的人进入我的不和谐,我的机器人用一个定制的消息来迎接他们

没有语法错误,类加载正确。on_ready侦听器正确响应Welcome:on。如果我尝试进行调试打印,则不会执行

我哪里做错了?我不明白,我觉得这是对的

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

class Welcome(commands.Cog):
    def __init__(self,client):
        self.client=client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Welcome: ON")
    
    @commands.Cog.listener()
    async def on_member_join(self, member):
        guild= client.get_guild(828676048039706694)
        channel=guild.get_channel(828676048039706697)
        if channel is not None:
            await channel.send(f'Welcome {member.mention}.')

    @commands.command()
    async def Modulo_benvenuto(self,ctx):
        await ctx.send('')

def setup(client):
    client.add_cog(Welcome(client))
这是我的主文件:

import discord
import os
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

@client.command()
async def reload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')
        

client.run('TOKEN')
使用新的机器人程序,代码如下:

import discord
from  discord.ext import commands

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print("Bot is on")

@client.event
async def on_member_join(member):
    
    print(member)
    await member.send("hello")

    guild = client.get_guild(831588406089744435) 
    channel = discord.utils.get(member.guild.channels, id=831588406089744438)

    if guild:
           print("guild ok")
    else:
        print("guild not found")

    if channel is not None:
        await channel.send(f'Welcome to the {guild.name} Discord Server, {member.mention} !  :partying_face:')
    else:
        print("id channel wrong")


client.run('TOKEN')
对于on_member_join事件引用,您需要打开服务器成员意图特权网关意图。这必须在bot的页面以及您已经完成的脚本中完成:

去医院。 选择应用程序,然后在“设置”下导航到Bot。 如果您向下滚动一点,就会看到一个标题为Privileged Gateway Intents的部分,在该标题下是SERVER MEMBERS INTENT。 切换要打开的服务器。 在下图中,这将是第二个特权网关意图

代码出现问题可能是因为cog文件和主文件中都定义了discord.Bot实例。由于setupclient在cog文件中定义,并且client.load_扩展名在主文件中调用,因此您应该从cog文件中删除以下行:

焦距

意图=discord.intents.default intents.members=True 客户端=不一致。客户端内容=意图 但是,为了保留意图,您需要在discord.Bot调用之前添加以下行:

main.py

从discord.ext导入命令 意图=discord.intents.default intents.members=True 客户端=命令。Botcommand\u前缀=,意图=意图 对于您的代码,我建议使用,因为它消除了不正确的公会ID导致通道为无的可能性

_member_joinself上的异步定义,成员: 通道=等待客户端。获取\u通道1234567890 如果通道不是无: 等待channel.sendfWelcome{成员.提及}。 或者,您可以只使用discord.utils.get和member.guild.channels:

_member_joinself上的异步定义,成员: channel=discord.utils.getmember.guild.channels,id=1234567890 如果通道不是无: 等待channel.sendfWelcome{成员.提及}。 只是一个减少潜在错误的建议。

如何修复:

主文件

齿轮锉


谢谢你的回答,但我已经完成了这两个步骤,而且无论如何都不起作用……那么,也许你的代码通道=guild.get\u通道82867604803970706697中的行没有返回任何内容。如果没有错误消息,则可能是If条件不正确的问题。验证帮会ID和频道ID是否正确,出于调试目的,我建议在子句中添加一个else语句,只需打印一些简单的内容,如printReach the else子句..我还通过添加else尝试了您编写的函数的两个版本,但它不会发送任何欢迎信息,在控制台中也不会打印出来else@T不幸的是,没有。如果我有任何想法,我会继续尝试,并向您提供最新信息。@JacobLee我已经找到了解决方案。无论如何,谢谢你昨天的时间。你试过合并这两个文件/代码吗?不,明天我会尝试没有任何错误:TypeError:event缺少1个必需的位置参数:“coro”
import discord
import os
from discord.ext import commands

intents = discord.Intents.default()
client = commands.Bot(command_prefix = '.', intents=intents)
intents.members = True

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

@client.command()
async def reload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')
        

client.run('TOKEN')
import discord
from discord.ext import commands

class Welcome(commands.Cog):
    def __init__(self, client):
        self.client=client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Welcome: ON")
    
    @commands.Cog.listener()
    async def on_member_join(self, member):
        print(member)
        await member.send("hello")
        guild = self.client.get_guild(831588406089744435)
        channel = discord.utils.get(member.guild.channels, id=831588406089744438)
        if guild:
            print("guild ok")
        else:
            print("guild not found")
        
        if channel is not None:
                await channel.send(f'Welcome to the {guild.name} Discord Server, {member.mention} !  :partying_face:')
        else:
            print("id channel wrong")

    @commands.command()
    async def Modulo_benvenuto(self, ctx):
        await ctx.send('test')

def setup(client):
    client.add_cog(Welcome(client))