Introduces a simple cog that welcomes new users via customizable messages in designated channels, enhancing community engagement. Handles channel selection with fallbacks and includes error resilience for permission issues.
40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
# welcomer.py
|
|
|
|
from redbot.core import commands
|
|
from redbot.core.bot import Red
|
|
import discord
|
|
|
|
class Welcomer(commands.Cog):
|
|
"""A simple cog to welcome new users."""
|
|
|
|
def __init__(self, bot: Red):
|
|
self.bot = bot
|
|
|
|
@commands.Cog.listener()
|
|
async def on_member_join(self, member):
|
|
"""Greets a new member when they join the server."""
|
|
|
|
guild = member.guild
|
|
while True:
|
|
# Try to find a channel named 'welcome'
|
|
channel = discord.utils.get(guild.text_channels, name='welcome')
|
|
if channel is None:
|
|
# If not found, try to find a channel named 'general'
|
|
channel = discord.utils.get(guild.text_channels, name='general')
|
|
if channel is None:
|
|
# If still not found, use the first text channel available
|
|
if guild.text_channels:
|
|
channel = guild.text_channels[0]
|
|
else:
|
|
# If no text channels exist, exit the function
|
|
return
|
|
try:
|
|
await channel.send(f"Welcome to the server, {member.mention}!")
|
|
break # Exit the loop after successfully sending the message
|
|
except discord.Forbidden:
|
|
# If we don't have permission to send messages in this channel, try again
|
|
continue
|
|
|
|
# This function allows Red to load the cog
|
|
async def setup(bot: Red):
|
|
await bot.add_cog(Welcomer(bot)) |