机器人将具有特定角色的成员踢出服务器,并在机器人员工记录频道和用户的DM(被踢出的人)中发送消息

问题描述 投票:0回答:1

我目前正在开发一个机器人,可以根据特定角色从服务器中踢出成员。它旨在向员工记录通道发送消息,并通过直接消息通知用户被踢的原因。如果用户加入并选择该角色,机器人将执行双击,并且在第三次发生时,它将继续禁止该成员。通知将发送到员工记录频道和用户的私信中。

到目前为止,代码似乎运行正常,成功执行了踢出成员并在员工机器人频道中发送消息的任务。但是,它在尝试向用户的直接消息发送消息时遇到问题,导致出现异常,表明它无法将消息传递给特定用户。

下面的代码中是否需要进行任何必要的修复来解决此问题?此外,该机器人已被授予必要的消息权限,以及踢出和禁止权限。

**此外,请确保用户打开直接消息 (DM)。如果仍然存在问题,是否有任何特定的内容我应该查找或添加到代码中以诊断错误? ** 这是代码:-

main.py

import discord
from discord.ext import commands

intents = discord.Intents.all()

# Import your bot token from config.py
from config import TOKEN

# Define your bot prefix
PREFIX = "!"

# Define your role ID
ROLE_ID = 1224742347816960063  # Replace with the actual role ID

bot = commands.Bot(command_prefix=PREFIX, intents=intents)

# Define the default channel ID as None
CHANNEL_ID = None
STAFF_BOTS_CHANNEL = "ㆍ😄┇staff-bots"  # Replace with the actual channel name

# Define a dictionary to track the number of kicks for each user
offense_count = {}

@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')

@bot.event
async def on_member_join(member):
    if ROLE_ID in [role.id for role in member.roles]:
        await member.kick(reason="Automatic kick on join - 1st Offence")
        await member.send(f"1st offence: You have been kicked from the server")

@bot.event
async def on_member_update(before, after):
    if ROLE_ID in [role.id for role in after.roles] and ROLE_ID not in [role.id for role in before.roles]:
        user_id = after.id
        if user_id in offense_count:
            offense_count[user_id] += 1
        else:
            offense_count[user_id] = 1

        if offense_count[user_id] >= 3:
            await after.ban(reason="3rd Offence - Automatic ban")
            if STAFF_BOTS_CHANNEL:
                staff_bots_channel = bot.get_channel(1224742349410795575)  # Use the actual channel ID
                if staff_bots_channel:
                    await staff_bots_channel.send(f"3rd offence: Banned {after.name}")
        else:
            if STAFF_BOTS_CHANNEL:
                staff_bots_channel = bot.get_channel(1224742349410795575)  # Use the actual channel ID
                if staff_bots_channel:
                    if offense_count[user_id] > 1:
                        kick_reason = f"{offense_count[user_id]}th Offence"
                    else:
                        kick_reason = "Picked role - 1st Offence"
                    await staff_bots_channel.send(f"{kick_reason}: Kicked {after.name} - Picked role")
                    await after.kick(reason=kick_reason)

@bot.command(name="setup")
async def setup(ctx, channel_id: int):
    global CHANNEL_ID
    CHANNEL_ID = 1224742349410795575  # Set CHANNEL_ID to the provided channel_id
    await ctx.send("Setup complete!")

@bot.event
async def on_member_ban(guild, user):
    if CHANNEL_ID:
        await bot.get_channel(CHANNEL_ID).send(f"3rd offence: Banned {user.name}")

@bot.event
async def on_member_remove(member):
    if ROLE_ID in [role.id for role in member.roles]:
        try:
            await member.send("You have been banned from the server")
        except discord.errors.Forbidden:
            print(f"Failed to send message to {member.name}: User cannot receive direct messages")

bot.run(TOKEN)

剩下还有两个文件。 config.py 仅包含令牌和requirements.txt,即导出discord.py。

我需要帮助来解决这个问题。请遇到此问题或知道为什么会发生这种情况的人可以回复!

automation discord discord.py bots
1个回答
0
投票

如果您没有任何共同的服务器,则无论他们的 DM 是否打开,您都无法从您的 Discord 机器人向用户私信。所以在 on_member_join 和 on_member_update 你可以交换执行它们的顺序,这样你就可以在踢/禁止之前成功PM。不幸的是,您无法通过 on_member_ban 和 on_member_remove 事件来实现这一点

© www.soinside.com 2019 - 2024. All rights reserved.