您好,这个简单的小型python discord bot模块遇到了问题。每当我使用命令时,即使它被编码为仅返回一个,它似乎仍会返回4或5个响应。我正在忽略某些东西,对此是否有简单的解决方法?
这是我正在使用的代码
import discord
import os
import logging as log
from datetime import datetime,timedelta
from discord.ext import commands
from discord import Forbidden, HTTPException
from .utils.dataIO import dataIO
from .utils import checks
import asyncio
class lurkout:
"""
add roles to new members after x amount of time
"""
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True, no_pm=True)
@checks.is_owner()
async def huntlurkers(self, ctx, *_):
"""Look for lurkers"""
serverid = '465536300452151298'
server = self.bot.get_server(serverid)
for member in server.members:
lurker_role = discord.utils.get(member.server.roles, name="Lurker")
if lurker_role not in member.roles:
await self.bot.say("No lurkers seen within the last 3 days.")
else:
await self.check_roles(serverid, member)
await self.bot.say("Success! You have manually added the Lurker role to those who haven't received a members role in 3 days.")
return
@commands.command(pass_context=True, no_pm=True)
@checks.is_owner()
async def kicklurkers(self, ctx, *_):
"""Mass kick lurkers"""
serverid = '465536300452151298'
server = self.bot.get_server(serverid)
for member in server.members:
lurker_role = discord.utils.get(member.server.roles, name="Lurker")
if lurker_role not in member.roles:
await self.bot.send_message("There are no lurkers to kick.")
else:
if lurker_role in member.roles:
await self.check_lurkers(serverid, member)
await self.bot.send_message("Kicked some lurkers :D.")
return
async def check_roles(self, serverid, member: discord.Member, *_):
serverid = '465536300452151298'
server = self.bot.get_server(serverid)
lurker_role = discord.utils.get(member.server.roles, name="Lurker")
members_role = discord.utils.get(member.server.roles, name="Members")
for member in server.members:
if member.joined_at + timedelta(days=3) <= datetime.today():
if members_role not in member.roles:
try:
await self.bot.add_roles(member, lurker_role)
log.info("[SUCCESS] Changing role of "+str(member)+" succeeded")
except (Forbidden, HTTPException) as e:
log.info("[FAILURE] Changing role of "+str(member)+" failed")
async def check_lurkers(self, serverid, member: discord.Member, *_):
serverid = '465536300452151298'
server = self.bot.get_server(serverid)
lurker_role = discord.utils.get(member.server.roles, name="Lurker")
members_role = discord.utils.get(member.server.roles, name="Members")
for member in list(self.bot.get_all_members()):
if member.joined_at + timedelta(days=0) <= datetime.today():
if lurker_role in member.roles:
try:
await self.bot.kick(member)
log.info("[SUCCESS] Changing role of "+str(member)+" succeeded")
except (Forbidden, HTTPException) as e:
log.info("[FAILURE] Changing role of "+str(member)+" failed")
async def check_day(self):
tomorrow = datetime.now()+timedelta(days=1)
midnight = datetime(year=tomorrow.year, month=tomorrow.month,
day=tomorrow.day, hour=0, minute=0, second=0)
await asyncio.sleep((midnight - datetime.now()).seconds)
print("About to start")
while self is self.bot.get_cog("check_roles"):
await self.autoaddrole_update()
await asyncio.sleep(86400) # Wait 24 hours
def setup(bot):
q = lurkout(bot)
loop = asyncio.get_event_loop()
loop.create_task(q.check_day())
bot.add_cog(q)
async def huntlurkers(self, ctx, *_):
和async def kicklurkers(self, ctx, *_):
命令仍然存在该问题,并且await
语句返回大约4次。
编辑:
这是我尝试放入代码中的修改,我遇到的问题是赋值之前引用的局部变量“ member”
@commands.command(pass_context=True, no_pm=True)
@checks.is_owner()
async def kicklurkers(self, ctx, *_):
"""Mass kick lurkers"""
serverid = '465536300452151298'
server = self.bot.get_server(serverid) # This could probably be ctx.message.server
lurker_role = discord.utils.get(server.roles, name="Lurker")
member_role = discord.utils.get(server.roles, name="Members")
any_lurkers = False
if member_role not in member.roles:
for member in server.members:
if lurker_role in member.roles:
if member.joined_at + timedelta(days=3) <= datetime.today():
await self.bot.kick(member)
any_lurkers=True
if any_lurkers:
await self.bot.say("Kicked all lurkers!")
else:
await self.bot.say("No lurkers to kick!")
答案 0 :(得分:0)
您的命令遍历服务器成员。 对于每个成员,如果他们不是潜伏者,您会发送一条消息。如果他们是潜伏者,则可以对该成员进行操作,然后停止处理。从您发送的消息中,您似乎想处理所有用户,然后根据是否发现潜伏者而打印两个消息之一。
@commands.command(pass_context=True, no_pm=True)
@checks.is_owner()
async def kicklurkers(self, ctx, *_):
"""Mass kick lurkers"""
serverid = '465536300452151298'
server = self.bot.get_server(serverid) # This could probably be ctx.message.server
lurker_role = discord.utils.get(server.roles, name="Lurker")
member_role = discord.utils.get(server.roles, name="Members")
any_lurkers = False
for member in server.members:
if member_role not in member.roles and lurker_role in member.roles:
if member.joined_at + timedelta(days=3) <= datetime.today():
await self.bot.kick(member)
any_lurkers=True
if any_lurkers:
await self.bot.say("Kicked all lurkers!")
else:
await self.bot.say("No lurkers to kick!")
我不明白您的check_lurkers
协程试图完成什么。是否要检查特定的Member
是否为潜伏者,从服务器中踢出所有潜伏者或更改其角色?