Дискорд менеджер инвайт менеджер с фейковыми розыгрышами
Python:
import disnake, sqlite3, random
from disnake.ext import commands
from datetime import datetime
from config import *
#подключение дискорд
bot = commands.Bot(command_prefix='.', intents=disnake.Intents.all())
client = disnake.Client()
#подключение бд
db = sqlite3.connect('.databaze.db')
sql = db.cursor()
async def create_db():
sql.execute("""CREATE TABLE IF NOT EXISTS users(
user TEXT,
invites INT,
feels INT
)""")
db.commit()
invites = {}
def find_invite_by_code(invite_list, code):
for inv in invite_list:
if inv.code == code:
return inv
@bot.event
async def on_ready():
print(f"{bot.user} запущен!")
for guild in bot.guilds:
invites[guild.id] = await guild.invites()
@bot.event
async def on_member_join(member):
invites_before_join = invites[member.guild.id]
invites_after_join = await member.guild.invites()
for invite in invites_before_join:
if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
try:
sql.execute(f"SELECT invites FROM users WHERE user = {invite.inviter.id}")
except sqlite3.OperationalError:
await create_db()
if sql.fetchone() is None:
sql.execute(f"INSERT INTO users VALUES({invite.inviter.id}, {1}, {0})")
text = f"{invite.inviter} пригласил {member.name}\nТеперь у него 1 инвайт "
elif invite.inviter.mention == member.mention:
text = f"{member.mention} попытался поломать систему и пригласил себя"
else:
u_invites = sql.execute(f"SELECT invites FROM users WHERE user = {invite.inviter.id}").fetchone()[0]
sql.execute(f"UPDATE users SET invites = {u_invites+1} WHERE user = {invite.inviter.id}")
text = f"{invite.inviter.mention} пригласил {member.mention}\nТеперь у него {u_invites+1} инвайта(ов)"
channel = member.guild.system_channel
await channel.send(text)
invites[member.guild.id] = invites_after_join
db.commit()
@bot.command("con")
async def create_contest(ctx, *, describe: str = None):
if ctx.author.id == admin_id:
if describe is None:
await ctx.send("Укажи описание розыгрыша")
else:
button = [disnake.ui.Button(label="Учавствовать", style=disnake.ButtonStyle.success, custom_id="yes")]
contest_channel = bot.get_channel(channel_id)
await contest_channel.send(describe, components=button)
else:
pass
@bot.listen("on_button_click")
async def button_listener(ctx):
if ctx.component.custom_id not in ["yes"]:
return
if ctx.component.custom_id == "yes":
try:
feels = sql.execute(f"SELECT feels FROM users WHERE user = {ctx.author.id}").fetchone()[0]
sql.execute(f"SELECT feels FROM users WHERE user = {ctx.author.id}")
except sqlite3.OperationalError:
await create_db()
if sql.fetchone() is None:
sql.execute(f"INSERT INTO users VALUES({ctx.author.id}, {0}, {0})")
await ctx.send("У тебя недостаточно инвайтов для участия", ephemeral=True)
elif feels == 1:
await ctx.send("Ты уже учавствуешь в розыгрыше", ephemeral=True)
else:
u_invites = sql.execute(f"SELECT invites FROM users WHERE user = {ctx.author.id}").fetchone()[0]
if u_invites >= need_invites:
sql.execute(f"UPDATE users SET feels = {1} WHERE user = {ctx.author.id}")
await ctx.send("Ты принял участие в розыгрыше", ephemeral=True)
else:
await ctx.send("У тебя недостаточно инвайтов для участия", ephemeral=True)
db.commit()
@bot.command("stat")
async def stat_contest(ctx):
if ctx.author.id == admin_id:
max_index = 0
for userses in sql.execute(f"SELECT user FROM users WHERE feels = {1}"):
max_index += 1
await ctx.send(f"Участников: {max_index}")
@bot.command("fin")
async def finish_contest(ctx):
if ctx.author.id == admin_id:
max_index = 0
feel = []
for userses in sql.execute(f"SELECT user FROM users WHERE feels = {1}"):
max_index += 1
feel.append(userses[0])
user = int(userses[0])
for user in feel:
user = int(user)
sql.execute(f"UPDATE users SET feels = {0} WHERE user = {user}")
max_index -=1
if max_index > 0:
index = random.randint(0, max_index)
vinner = bot.get_user(int(feel[index])).mention
contest_channel = bot.get_channel(channel_id)
await vinner.send("Поздравляю, ты выиграл в розыгрыше")
await contest_channel.send(f"Победил {vinner}")
db.commit()
elif max_index == 0:
await ctx.send("Невозможно! В розыгрыше учавствует только один человек\nНо его участие было обнулено")
elif max_index <0:
await ctx.send("В розыгрыше никто не учавствует")
@bot.command("lie")
async def lie_finish_contest(ctx, who: disnake.Member=None):
if ctx.author.id == admin_id:
if who is None:
await ctx.send("Ты не указал пользователя")
else:
feel = []
for userses in sql.execute(f"SELECT user FROM users WHERE feels = {1}"):
feel.append(userses[0])
for user in feel:
user = int(user)
sql.execute(f"UPDATE users SET feels = {0} WHERE user = {user}")
contest_channel = bot.get_channel(channel_id)
await contest_channel.send(f"Победил {who.mention}")
db.commit()
@bot.command("start")
async def rec_user(ctx):
try:
sql.execute(f"SELECT invites FROM users WHERE user = {ctx.author.id}")
except sqlite3.OperationalError:
await create_db()
if sql.fetchone() is None:
sql.execute(f"INSERT INTO users VALUES({ctx.author.id}, {0}, {0})")
await ctx.send("Ты успешно зарегестрирован")
else:
await ctx.send("Привет!")
bot.run(token)
Python:
token = "токен бота"
admin_id = 0 #id админа
channel_id = 0 #id канала с розыгрышами
need_invites = 1 #нужное количество инвайтов для участия
Так же req
Так же req
disnake
sqlite3
random
sqlite3
random