Make a child class for Client and use that, otherwise "base" listeners and commands won't work.

This commit is contained in:
Vegard Berg 2023-02-06 20:20:20 +01:00
parent bc8976590d
commit 09674aa86b
1 changed files with 112 additions and 86 deletions

View File

@ -7,6 +7,9 @@ from naff import (
listen,
slash_command,
InteractionContext,
AuditLogEventType,
AuditLogEntry,
RoleSelectMenu,
)
from naff.api import events
from naff.models.discord.embed import (
@ -19,105 +22,128 @@ from naff.models.discord.embed import (
from dotenv import load_dotenv
from database import GuildSettings as GuildSettingsModel, JoinLeave
bot = Client(intents=Intents.ALL, debug_scope=387153131378835456)
class HeimdallrClient(Client):
@listen()
async def on_ready(self):
print("------------------------------------")
print(f"Bot '{bot.user.username}' is ready!")
print(f"This bot is owned by {bot.owner}")
print("------------------------------------")
for guild in bot.guilds:
guild_q = GuildSettingsModel.get_or_none(GuildSettingsModel.guild_id == guild.id)
if guild_q is None:
guild_q = GuildSettingsModel(guild_id=guild.id)
guild_q.save()
@listen()
async def on_ready():
print(f"Bot '{bot.user.username}' is ready!")
print(f"This bot is owned by {bot.owner}")
@listen(events.MemberAdd)
async def on_member_join(self, event: events.MemberAdd):
joinleave_q: JoinLeave
joinleave_q, _ = JoinLeave.get_or_create(guild_id=event.guild.id)
for guild in bot.guilds:
guild_q = GuildSettingsModel.get_or_none(GuildSettingsModel.guild_id == guild.id)
if guild_q is None:
guild_q = GuildSettingsModel(guild_id=guild.id)
guild_q.save()
if joinleave_q.message_channel is None:
return
channel = await bot.fetch_channel(joinleave_q.message_channel)
if not joinleave_q.join_message_enabled:
return
if joinleave_q.join_message is None or joinleave_q.join_message == "":
await channel.send(
f"{event.member.mention} has joined the server!"
)
return
await channel.send(str(joinleave_q.join_message).format(member=event.member, guild=event.guild))
@listen(events.MemberRemove)
async def on_member_leave(self, event: events.MemberRemove):
joinleave_q: JoinLeave
joinleave_q, _ = JoinLeave.get_or_create(guild_id=event.guild.id)
guildsettings_q: GuildSettingsModel
guildsettings_q, _ = GuildSettingsModel.get_or_create(guild_id=event.guild.id)
entry: AuditLogEntry
async for entry in event.guild.audit_log_history(
action_type=AuditLogEventType.MEMBER_KICK,
limit=10
):
if (entry.target_id != event.member.id or
guildsettings_q.admin_channel is None):
continue
channel = await bot.fetch_channel(guildsettings_q.admin_channel)
await channel.send(f"{event.member.mention} was kicked with reason: {entry.reason}")
break
if joinleave_q.message_channel is None:
return
channel = await bot.fetch_channel(joinleave_q.message_channel)
if not joinleave_q.leave_message_enabled:
return
if joinleave_q.leave_message is None or joinleave_q.leave_message == "":
await channel.send(
f"{event.member.mention} has left the server!"
)
return
await channel.send(str(joinleave_q.leave_message).format(member=event.member, guild=event.guild))
# @listen()
# async def on_message_create(event: MessageCreate):
# print(f"{event.message.author.username}: {event.message.content}")
@listen(events.MemberAdd)
async def on_member_join(event: events.MemberAdd):
joinleave_q: JoinLeave
joinleave_q, _ = JoinLeave.get_or_create(guild_id=event.guild.id)
if joinleave_q.message_channel is None:
return
channel = await bot.fetch_channel(joinleave_q.message_channel)
if not joinleave_q.join_message_enabled:
return
if joinleave_q.join_message is None or joinleave_q.join_message == "":
await channel.send(
f"{event.member.mention} has joined the server!"
@slash_command(name="ping", description="Ping the bot")
async def ping_command(self, ctx: InteractionContext):
ctx.ephemeral = True
await ctx.send("Pong!",
components=RoleSelectMenu(placeholder="HONK")
)
return
await channel.send(str(joinleave_q.join_message).format(member=event.member, guild=event.guild))
@listen(events.MemberRemove)
async def on_member_leave(event: events.MemberRemove):
joinleave_q: JoinLeave
joinleave_q, _ = JoinLeave.get_or_create(guild_id=event.guild.id)
if joinleave_q.message_channel is None:
return
channel = await bot.fetch_channel(joinleave_q.message_channel)
if not joinleave_q.leave_message_enabled:
return
if joinleave_q.leave_message is None or joinleave_q.leave_message == "":
await channel.send(
f"{event.member.mention} has left the server!"
@slash_command(name="bot-info", description="Get info about the bot")
async def bot_info_command(self, ctx: InteractionContext):
await ctx.send(
ephemeral=True,
embed=Embed(
title=f"{bot.user.username}",
description=f"This bot is owned by {bot.owner}",
color=0x00FF00,
timestamp=bot.user.created_at,
url=bot.user.avatar.as_url(),
author=EmbedAuthor(
name=bot.user.username,
icon_url=bot.user.avatar.as_url(),
),
footer=EmbedFooter(
text=f"Created at {bot.user.created_at}",
icon_url=bot.user.avatar.as_url(),
),
fields=[
EmbedField(
name="**Extensions**",
value=f"{', '.join(bot.ext.keys())}",
),
EmbedField(
name="**Guilds**",
value="\n".join([g.name for g in sorted(bot.guilds, key=lambda g: g.name)]),
),
],
),
)
return
await channel.send(str(joinleave_q.leave_message).format(member=event.member, guild=event.guild))
bot = HeimdallrClient(intents=Intents.ALL, debug_scope=387153131378835456,
sync_interactions=True,
fetch_members=True,
)
@slash_command(name="ping", description="Ping the bot")
async def ping_command(ctx: InteractionContext):
ctx.ephemeral = True
await ctx.send("Pong!")
@slash_command(name="bot-info", description="Get info about the bot")
async def bot_info_command(ctx: InteractionContext):
await ctx.send(
ephemeral=True,
embed=Embed(
title=f"{bot.user.username}",
description=f"This bot is owned by {bot.owner}",
color=0x00FF00,
timestamp=bot.user.created_at,
url=bot.user.avatar.as_url(),
author=EmbedAuthor(
name=bot.user.username,
icon_url=bot.user.avatar.as_url(),
),
footer=EmbedFooter(
text=f"Created at {bot.user.created_at}",
icon_url=bot.user.avatar.as_url(),
),
fields=[
EmbedField(
name="**Extensions**",
value=f"{', '.join(bot.ext.keys())}",
),
EmbedField(
name="**Guilds**",
value="\n".join([g.name for g in sorted(bot.guilds, key=lambda g: g.name)]),
),
],
),
)
def set_loglevel(level: str):
loglevel = logging.WARNING
@ -161,4 +187,4 @@ def main():
bot.start(getenv("DISCORD_TOKEN"))
if __name__ == "__main__":
main()
main()