我正在研究 discord.py 但我的 on_member_removed(member) 什么都不做
I am working on a discord.py but my on_member_removed(member) does nothing
我已经添加了所有必需的东西,例如意图和网关,但我的机器人没有反应,我放置了一个调试打印命令,以便当有人离开它时打印“机器人已检测到”+member.mention+ “已离开服务器”,但无论我做什么,它都没有任何作用。
import discord
from discord.ext.commands import has_permissions
import asyncio
import logging
intents = discord.Intents.default()
intents.typing = True
intents.presences = True
intents.members = True
def replace_line(file_name, line_num, text):
with open(file_name, 'r') as file:
data = file.readlines()
print(data)
data[line_num] = text
with open(file_name, 'w') as file:
file.writelines( data )
print(data)
def leavechannel(serverid, channel):
flc = open("leavechannels.txt", "r")
checker = (flc.read())
flc.close
x = checker.find(serverid)
print (x)
if x >= 0:
lookup = serverid
with open("leavechannels.txt") as myFile:
for num, line in enumerate(myFile, 0):
if lookup in line:
print (serverid +' found at line:', num)
linecache= num
print(linecache)
replace_line("leavechannels.txt", linecache, "\n"+serverid + " = " + channel)
else:
flc = open("leavechannels.txt", 'a+')
flc.write(+serverid + " = " + channel)
client = discord.Client()
prefix = "ez!"
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_member_remove(member):
print("recognised that "+member.mention+" has left")
with open("leavechannels.txt") as myFile:
for item in myFile.split("\n"):
if member.server.id in item:
leavechannelcache = item.strip()
embedVar= discord.Embed(title= member.mention + " has left :(", description="Come back plox!", color=12151512)
await discord.Object(id=leavechannelcache).send(embed=embedVar)
print("Sent message to #CHANNEL")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(prefix+'leavechannel'):
if message.author.guild_permissions.manage_channels or message.author.guild_permissions.administration:
leavechannelhash = message.content.replace(prefix+'leavechannel ', '')
print (leavechannelhash)
await message.channel.send("Server leave channel has been set to "+leavechannelhash)
leavechannel(str(message.guild.id), str(leavechannelhash))
else:
await message.channel.send("Sorry, "+message.author+"; You do not have sufficient Permissions to do this.")
client.run('token')
我省略了很多不必要的内容(主要是与帮助和 hello world 填充命令相关的内容)但手头的主要问题是我有 intents.members 并且我已经激活了它在不和谐的应用程序面板中,但即使这样做之后,当有人离开服务器时它甚至没有给我 DEBUG 打印,这显然表明它检测到成员离开时出现了问题。您可以提出任何修复建议吗?
您需要更改您的代码,如果您使用的是 dpy 1.5.1,则有一个意图更新,因此请在您的代码中使用它。
from discord import Intents
from discord.ext.commands import Bot
intent = Intents().all()
bot = Bot(command_prefix='prefix', intents=intent)
... # Some code under this line
并尝试使用 Intents().all
?
在你的情况下,这样的意图discord.Client(intents=intent)
我已经添加了所有必需的东西,例如意图和网关,但我的机器人没有反应,我放置了一个调试打印命令,以便当有人离开它时打印“机器人已检测到”+member.mention+ “已离开服务器”,但无论我做什么,它都没有任何作用。
import discord
from discord.ext.commands import has_permissions
import asyncio
import logging
intents = discord.Intents.default()
intents.typing = True
intents.presences = True
intents.members = True
def replace_line(file_name, line_num, text):
with open(file_name, 'r') as file:
data = file.readlines()
print(data)
data[line_num] = text
with open(file_name, 'w') as file:
file.writelines( data )
print(data)
def leavechannel(serverid, channel):
flc = open("leavechannels.txt", "r")
checker = (flc.read())
flc.close
x = checker.find(serverid)
print (x)
if x >= 0:
lookup = serverid
with open("leavechannels.txt") as myFile:
for num, line in enumerate(myFile, 0):
if lookup in line:
print (serverid +' found at line:', num)
linecache= num
print(linecache)
replace_line("leavechannels.txt", linecache, "\n"+serverid + " = " + channel)
else:
flc = open("leavechannels.txt", 'a+')
flc.write(+serverid + " = " + channel)
client = discord.Client()
prefix = "ez!"
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_member_remove(member):
print("recognised that "+member.mention+" has left")
with open("leavechannels.txt") as myFile:
for item in myFile.split("\n"):
if member.server.id in item:
leavechannelcache = item.strip()
embedVar= discord.Embed(title= member.mention + " has left :(", description="Come back plox!", color=12151512)
await discord.Object(id=leavechannelcache).send(embed=embedVar)
print("Sent message to #CHANNEL")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(prefix+'leavechannel'):
if message.author.guild_permissions.manage_channels or message.author.guild_permissions.administration:
leavechannelhash = message.content.replace(prefix+'leavechannel ', '')
print (leavechannelhash)
await message.channel.send("Server leave channel has been set to "+leavechannelhash)
leavechannel(str(message.guild.id), str(leavechannelhash))
else:
await message.channel.send("Sorry, "+message.author+"; You do not have sufficient Permissions to do this.")
client.run('token')
我省略了很多不必要的内容(主要是与帮助和 hello world 填充命令相关的内容)但手头的主要问题是我有 intents.members 并且我已经激活了它在不和谐的应用程序面板中,但即使这样做之后,当有人离开服务器时它甚至没有给我 DEBUG 打印,这显然表明它检测到成员离开时出现了问题。您可以提出任何修复建议吗?
您需要更改您的代码,如果您使用的是 dpy 1.5.1,则有一个意图更新,因此请在您的代码中使用它。
from discord import Intents
from discord.ext.commands import Bot
intent = Intents().all()
bot = Bot(command_prefix='prefix', intents=intent)
... # Some code under this line
并尝试使用 Intents().all
?
在你的情况下,这样的意图discord.Client(intents=intent)