繁体   English   中英

Discord 带齿轮的问候,on_member_join,不起作用

[英]Discord greetings with cogs ,on_member_join , doen't work

我的on_member_join监听器没有正确执行。 这个想法是,当一个新人进入我的 discord 时,我的机器人会用自定义消息向他们打招呼。

没有语法错误,并且 class 加载正确。 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()事件引用,您需要启用服务器成员意图特权网关意图。 这必须在您的机器人页面和脚本(您已经完成)中完成:

  1. Go 到Discord 开发人员门户
  2. Select 应用程序,并在设置下导航到Bot
  3. 如果您向下滚动一点,您将到达标题Privileged Gateway Intents以及在其下的SERVER MEMBERS INTENT部分。
  4. SERVERS MEMBERS INTENT切换为ON

在下图中,它将是特权网关意图中的第二个。

特权网关意图


您的代码问题可能是因为您在 cog 文件和主文件中都定义了discord.Bot实例。 由于setup(client)在您的 cog 文件中定义并且client.load_extension()在您的主文件中被调用,您应该从 cog 文件中删除以下行:

齿轮.py

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

但是,为了保存意图,您需要在discord.Bot()调用之前添加以下行:

主文件

from discord.ext import commands

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

对于您的代码,我建议使用Client.fetch_channel方法,因为它消除了不正确的 Guild ID 导致channelNone的可能性。

async def on_member_join(self, member):
    channel = await client.fetch_channel(1234567890)
    if channel is not None:
        await channel.send(f"Welcome {member.mention}.")

或者,您可以只使用discord.utils.get()member.guild.channels

async def on_member_join(self, member):
    channel = discord.utils.get(member.guild.channels, id=1234567890)
    if channel is not None:
        await channel.send(f"Welcome {member.mention}.")

只是减少潜在错误的建议。

怎么修:

主文件

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM