<p>正如FluxedScript所说,您可能希望使用<a href="https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html" rel="nofollow noreferrer">^{<cd1>} command</a>,而不是使用<code>on_message</code>。如果您这样做,您可以尝试以下方法:</p>
<pre class="lang-py prettyprint-override"><code>import random
from discord.ext import commands
from discord.ext import tasks
class DiscordBot(commands.Bot):
def __init__(self):
intents = discord.intents.default()
intents.members = True
self.bot = commands.Bot(prefix="!", intents=intents)
self.bot.add_cog(RussianRoulette(self.bot))
class RussianRoulette(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.banned = {}
@commands.command(name="russian", *names)
async def russian(self, ctx):
members_list = [discord.utils.get(ctx.guild.members, name=n) for n in names]
member = random.choice(member_list)
member.ban(reasion="Russian Roulette")
self.banned.setdefault(
member,
datetime.datetime.now()+datetime.timedelta(days=1)
)
@tasks.loop(minutes=1.0)
async def check_unban(self):
for mem, dtime in self.banned.items():
if datetime.datetime.now() <= dtime:
mem.unban(reason="Ban expired")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
bot = DiscordBot()
loop.add_task(bot.start("TOKEN"))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
</code></pre>
<p>现在,假设您的bot实例已正确设置。您还需要激活一些<a href="https://discordpy.readthedocs.io/en/latest/intents.html" rel="nofollow noreferrer">gateway intents</a></p>