Last active
December 5, 2023 21:13
-
-
Save NNKTV28/fdfd64e03b023a0072ff495c92fd6acc to your computer and use it in GitHub Desktop.
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 os | |
import sys | |
import sqlite3 | |
import discord | |
import logging | |
import datetime | |
import paypalrestsdk | |
import schedule | |
from dotenv import load_dotenv | |
from discord.ext import commands | |
from pkgutil import iter_modules | |
from cogs import EXTENSIONS | |
from Handler import * | |
load_dotenv() | |
intents = discord.Intents.all() | |
# Initialize the PayPal SDK | |
paypalrestsdk.configure({ | |
"mode": os.getenv("paypal_mode"), | |
"client_id": os.getenv("paypal_client_id"), | |
"client_secret": os.getenv("paypal_client_secret") | |
}) | |
try: | |
con = sqlite3.connect("AngelMusic.db", check_same_thread=False) | |
cur = con.cursor() | |
except sqlite3.Error as error: | |
print(f"Error while connecting to the database: {error}") | |
sys.exit(1) | |
# Create a table to store premium guild information | |
cur.execute('''CREATE TABLE IF NOT EXISTS premium_guilds | |
(guild_id BIGINT PRIMARY KEY, | |
premium_start TEXT, | |
premium_end TEXT)''') | |
con.commit() | |
# Create a tble to store the custom playlists | |
cur.execute('''CREATE TABLE IF NOT EXISTS custom_playlists | |
(guild_id BIGINT PRIMARY KEY, | |
name TEXT, | |
link TEXT)''') | |
con.commit() | |
# Create a table to store the paypal payments | |
cur.execute('''CREATE TABLE IF NOT EXISTS paypal_payments | |
(user_id BIGINT, | |
amount FLOAT, | |
currency TEXT, | |
transaction_id TEXT PRIMARY KEY, | |
ticket_link TEXT)''') | |
con.commit() | |
# Create a table to store announcement channel information | |
cur.execute('''CREATE TABLE IF NOT EXISTS announcement_channels | |
(guild_id BIGINT PRIMARY KEY, | |
channel_id BIGINT)''') | |
con.commit() | |
# Create a table to store guild prefix information | |
cur.execute('''CREATE TABLE IF NOT EXISTS guild_prefixes | |
(guild_id BIGINT PRIMARY KEY, | |
prefix TEXT)''') | |
con.commit() | |
# Create a table to store users youtube information | |
cur.execute('''CREATE TABLE IF NOT EXISTS youtube_stats | |
(user_id BIGINT PRIMARY KEY, | |
youtube_channel_id TEXT)''') | |
con.commit() | |
#color codes | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKCYAN = '\033[96m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
class Bot(commands.Bot): | |
def __init__(self, bot): | |
self.prefixes = [] | |
super().__init__( | |
command_prefix="?", | |
intents=intents, | |
activity=discord.Activity( | |
type=discord.ActivityType.listening, | |
name='❌ | Do not use this bots commands | ❌' | |
), | |
case_insensitive = True | |
) | |
async def check_premium_guilds(self): | |
cur.execute("SELECT * FROM premium_guilds") | |
premium_guilds = cur.fetchall() | |
for guild in premium_guilds: | |
guild_id = guild[0] | |
premium_end = datetime.datetime.strptime(guild[2], "%Y-%m-%d %H:%M:%S") | |
if premium_end <= datetime.datetime.now(): | |
# remove expired premium server from database | |
cur.execute("DELETE FROM premium_guilds WHERE guild_id=?", (guild_id,)) | |
con.commit() | |
print(f"Premium expired for server {guild_id}") | |
schedule.every(1).hour | |
async def update_nickname(self, guild): | |
is_premium = await self.is_premium(guild.id) | |
if is_premium: | |
nickname = f"{self.user.name} (premium)" | |
else: | |
nickname = f"{self.user.name} (free)" | |
await guild.me.edit(nick=nickname) | |
async def on_ready(self): | |
print(bcolors.OKGREEN + f"{self.user.name} has connected to Discord" + bcolors.ENDC + "") | |
await self.check_premium_guilds() | |
async def change_nisckname(self): | |
for guild in self.guilds: | |
await self.update_nickname(guild) | |
async def on_guild_join(self, guild): | |
# update the bot's nickname when it joins a new server | |
await self.update_nickname(guild) | |
async def on_guild_update(self, before, after): | |
# update the bot's nickname when a server's premium status changes | |
if before.premium_tier != after.premium_tier: | |
await self.update_nickname(after) | |
async def setup_hook(self) -> None: | |
print(bcolors.OKGREEN + f"Loading cogs" + bcolors.ENDC + "") | |
for extension in EXTENSIONS: | |
print(extension) | |
try: | |
await self.load_extension(extension) | |
print(bcolors.OKBLUE + f"Loaded cog: {extension}" + bcolors.ENDC + "") | |
except Exception as error: | |
print(bcolors.FAIL + f"Failed to load cog: {extension}\nError: {error}" + bcolors.ENDC + "") | |
async def get_prefix(self, message): | |
cur.execute("SELECT prefix FROM guild_prefixes WHERE guild_id=?", (message.guild.id,)) | |
result = cur.fetchone() | |
if result is None: | |
prefix = "?" | |
else: | |
prefix = result[0] | |
return prefix | |
try: | |
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') | |
Bot().run(os.getenv("token"), log_handler=handler, log_level=logging.INFO) | |
except discord.LoginFailure: | |
print(bcolors.FAIL + "Invalid Discord Token. Please check your .env file." + bcolors.ENDC + "") | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment