【问题标题】:Discord greetings with cogs ,on_member_join , doen't work使用 cogs 的不和谐问候,on_member_join 不起作用
【发布时间】:2021-04-14 23:56:04
【问题描述】:

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

没有语法错误并且类加载正确。 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')

【问题讨论】:

  • 你试过合并两个文件/代码吗?
  • 不行,明天我试试
  • 没有给我这个错误:TypeError: event() missing 1 required positional argument: 'coro'

标签: python discord.py


【解决方案1】:

对于on_member_join() 事件引用,您需要启用服务器成员意图 特权网关意图。这必须在您的机器人页面和脚本(您已经完成)中完成:

  1. 转到Discord Developer Portal
  2. 选择应用程序,然后在设置下导航到 Bot
  3. 如果您向下滚动一点,您会看到一个部分标题Privileged Gateway Intents,在其下方是SERVER MEMBERS INTENT
  4. 服务器成员意图切换为开启

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


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

cog.py

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

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

ma​​in.py

from discord.ext import commands

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

对于您的代码,我建议使用Client.fetch_channel method,因为它消除了不正确的公会ID 导致channel 成为None 的可能性。

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}.")

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

【讨论】:

  • 感谢您的回答,但我已经完成了这两项工作,无论如何它都不起作用......
  • 那么,您的代码channel=guild.get_channel(828676048039706697) 中的行可能会返回None。如果没有错误消息,则可能是if 条件不是True 的问题。验证公会 ID 和频道 ID 是否正确,出于调试目的,我建议放置 else 语句,并在子句中打印一些简单的内容,例如 print("Reached the else clause.")
  • 我也通过添加 else 尝试了您编写的函数的两个版本,但它没有发送任何欢迎消息,并且在控制台中它不会在 else 中打印
  • @Tμrf 不幸的是没有。如果我有任何想法,我会继续尝试并更新您。
  • @JacobLee 我找到了解决方案。无论如何,感谢您昨天的时间。
【解决方案2】:

如何解决:

主文件

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

【讨论】:

    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 2020-09-19
    • 2021-02-25
    • 2023-03-12
    • 2021-03-31
    • 2021-01-28
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多