簡體   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