2024-03-26 00:30:05 +00:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
|
|
|
import os
|
2024-04-02 22:45:34 +00:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
import sqlite3
|
2024-03-26 00:30:05 +00:00
|
|
|
import logging
|
2024-04-02 22:45:34 +00:00
|
|
|
from datetime import datetime, timezone
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Load environment variables
|
2024-03-26 00:30:05 +00:00
|
|
|
load_dotenv()
|
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Setup logging
|
|
|
|
logging.basicConfig(filename='discord.log', level=logging.INFO)
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Define intents
|
2024-03-26 00:30:05 +00:00
|
|
|
intents = discord.Intents.all()
|
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Connect to SQLite database
|
2024-03-26 00:30:05 +00:00
|
|
|
conn = sqlite3.connect('member_data.db')
|
2024-04-02 22:45:34 +00:00
|
|
|
c = conn.cursor()
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Create table if not exists
|
|
|
|
c.execute('''CREATE TABLE IF NOT EXISTS members
|
|
|
|
(user_id INTEGER PRIMARY KEY, username TEXT, nickname TEXT, join_datetime TEXT, onboarding_status INTEGER, last_change_datetime TEXT)''')
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
conn.commit()
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Initialize bot
|
|
|
|
bot = commands.Bot(command_prefix='/', intents=intents)
|
2024-03-26 00:30:05 +00:00
|
|
|
|
|
|
|
@bot.event
|
2024-04-02 22:45:34 +00:00
|
|
|
async def on_ready():
|
|
|
|
print(f'Logged in as {bot.user}')
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Find the #welcome channel
|
|
|
|
welcome_channel_id = int(os.getenv('WELCOME_CHANNEL_ID'))
|
|
|
|
welcome_channel = discord.utils.get(bot.get_all_channels(), id=welcome_channel_id)
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Check if welcome message has already been sent in the channel
|
|
|
|
async for message in welcome_channel.history(limit=100):
|
|
|
|
if message.author == bot.user and message.embeds:
|
|
|
|
return # If the welcome message is found, exit the function
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Send welcome message in the #welcome channel
|
|
|
|
view = discord.ui.View()
|
|
|
|
button = discord.ui.Button(label="Complete Onboarding")
|
|
|
|
view.add_item(button)
|
|
|
|
embed = discord.Embed(title="Welcome to the Server!", description="Here's how to get started:")
|
|
|
|
embed.add_field(name="Step 1:", value="Read the server rules in #rules channel.")
|
|
|
|
embed.add_field(name="Step 2:", value="Check out people's projects.")
|
|
|
|
embed.add_field(name="Step 3:", value="Complete Onboarding procedure to unlock the rest of the server.")
|
|
|
|
embed.set_footer(text="Enjoy your stay!")
|
|
|
|
await welcome_channel.send(embed=embed, view=view)
|
2024-03-26 00:30:05 +00:00
|
|
|
|
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
@bot.event
|
|
|
|
async def on_member_join(member):
|
|
|
|
# Check if member already exists in the database
|
|
|
|
c.execute("SELECT * FROM members WHERE user_id = ?", (member.id,))
|
|
|
|
existing_member = c.fetchone()
|
|
|
|
|
|
|
|
if not existing_member: # If it's the member's first time joining
|
|
|
|
# Add new member to the database
|
|
|
|
c.execute(
|
|
|
|
"INSERT OR REPLACE INTO members (user_id, username, join_datetime, onboarding_status, last_change_datetime) VALUES (?, ?, ?, ?, ?)",
|
|
|
|
(member.id, member.name, member.joined_at.isoformat(), 0, datetime.now(timezone.utc).isoformat()))
|
2024-03-26 00:30:05 +00:00
|
|
|
conn.commit()
|
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Update member nickname
|
|
|
|
await member.edit(nick=c.execute("SELECT nickname FROM members WHERE user_id = ?", (member.id,)).fetchone()[0])
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Log member join
|
|
|
|
logging.info(f'Member {member.name} joined the server.')
|
2024-03-26 00:30:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bot.event
|
2024-04-02 22:45:34 +00:00
|
|
|
async def on_member_remove(member):
|
|
|
|
# Log member leave
|
|
|
|
logging.info(f'Member {member.name} left the server.')
|
|
|
|
|
|
|
|
|
|
|
|
# Load bot token and welcome channel id from .env file
|
|
|
|
TOKEN = os.getenv('BOT_TOKEN')
|
|
|
|
WELCOME_CHANNEL_ID = os.getenv('WELCOME_CHANNEL_ID')
|
2024-03-26 00:30:05 +00:00
|
|
|
|
2024-04-02 22:45:34 +00:00
|
|
|
# Start bot
|
|
|
|
bot.run(TOKEN)
|