-
-
Save peco2282/d9d8b07730030c83c26ccbfd0b98c867 to your computer and use it in GitHub Desktop.
on_member_removeについて
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import discord | |
import motor.motor_asyncio as motor | |
from discord.ext import commands | |
import main.environments | |
allowed_mentions = discord.AllowedMentions(replied_user=False) | |
bot = commands.Bot(command_prefix="p ", intents=discord.Intents.all(), allowed_mentions=allowed_mentions) | |
dbclient = motor.AsyncIOMotorClient(main.environments.MONGODB) | |
db = dbclient["ProfileBot"] | |
profiles_collection = db.profiles | |
async def on_member_remove( | |
member: discord.Member | |
): | |
result = await profiles_collection.delete_one({ | |
"userid": member.id | |
}) | |
# メッセージを送信したいチャンネルIDを入れる。必要ない場合は `i = 0` の上までを消してください。 | |
channel = bot.get_channel(0) | |
if channel: | |
if result.deleted_count == 0: | |
await channel.send("みつからなかったロト~") | |
await channel.send("けしたロト~") | |
await channel.send("メッセージ削除を開始します") | |
# ------- 消す場合はここまで ------- | |
i = 0 | |
# メンバーが退出したギルドの中のテキストチャンネルすべてを回す | |
for text in member.guild.text_channels: | |
# DiscordAPIで負荷をかけすぎないように5回以上で1秒休憩 | |
i += 1 | |
# `limit=None` -> そのチャンネルのメッセージすべてを取得 | |
async for message in text.history(limit=None): | |
if i > 5: | |
i = 0 | |
# 1秒休む | |
await asyncio.sleep(1.) | |
# 送信者が脱退者 & set コマンド使用時(prefixがlistの時も考えてこうしてますが、必ずprefixは1つだというときは | |
# message.content.startswith(str(bot.command_prefix)))としてください。 | |
if message.author == member and message.content.startswith( | |
( | |
f"{prefix}set" for prefix in bot.command_prefix | |
) if isinstance(bot.command_prefix, list) | |
else str( | |
bot.command_prefix | |
) | |
): | |
# メッセージ削除 | |
await message.delete() | |
i += 1 | |
# メッセージ送信元を取得 (削除されたものはNoneが返る) | |
resolved = message.reference.resolved | |
# resolvedが存在 | |
if resolved: | |
# botの反応も消す場合 | |
await resolved.delete() | |
i += 1 | |
await asyncio.sleep(1.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment