async def changebackground(ctx, background: int = None): if 0 <= background < 37: user_sql = session.query(User).filter_by(id=str(ctx.author.id)).first() if user_sql: user_sql.background_image = background session.commit() await ctx.send(ctx.author.mention + " Card background changed!")
def add_user_in_db(update, session): user = update.message.from_user username = getattr(user, 'username', sqlalchemy.null()) session.add(User(telegram_username=username, telegram_user_id=user.id, first_name=user.first_name, last_name=user.last_name, date_registration=datetime.datetime.now(), last_activity=datetime.datetime.now())) session.commit()
async def voice_tick(): print("Voice chat loop started") global reset_timer await client.wait_until_ready() while True: reset_timer += 1 if reset_timer >= 7200: activity = discord.Game(name=f"Restarting") await client.change_presence(status=discord.Status.do_not_disturb, activity=activity) await client.logout() exit() for server in client.guilds: server_sql = q(Server).filter_by(id=str(server.id)).first() if not server_sql: server_sql = create_server(server) for member in server.members: if isinstance(member, discord.ClientUser) or member.bot: continue if not member.voice: continue if member.voice.afk: continue if not member.voice.channel: continue guild = member.voice.channel.guild user_sql = q(User).filter_by(id=str(member.id)).first() membership_sql = q(Membership).filter_by( user_id=str(member.id), server_id=str(guild.id)).first() if not user_sql: print("User had no data") user_sql = create_member(member) if not membership_sql: membership_sql = create_membership(member, guild) membership_sql.voice_time += 1 user_sql.total_seconds += 1 xp_ready = False if str(member.id) not in voice_chat_cooldowns: xp_ready = True voice_chat_cooldowns[str(member.id)] = time.time() else: if time.time() - voice_chat_cooldowns[str( member.id)] >= 150: xp_ready = True voice_chat_cooldowns[str(member.id)] = time.time() if xp_ready: if user_sql.xp + 1 >= 10 + (user_sql.level * 4): user_sql.xp = 0 user_sql.level += 1 else: user_sql.xp += 1 session.commit() await asyncio.sleep(1)
def handler_confirm_delete_limit(update, context): if not update.message.text == Buttons.confirm: return limit = session.query(Limit).get(context.user_data['id_limit_to_delete']) session.delete(limit) session.commit() update.message.reply_text(text=text_delete_success()) return to_main_menu(update, context)
def create_server(dser): server_sql = q(Server).filter_by(id=str(dser.id)).first() if server_sql: print("Server already exists in the database") return None new_server = Server(id=str(dser.id)) session.add(new_server) session.commit() return new_server
async def on_guild_channel_update(b, a): guild_query = session.query(Server).filter_by(id=a.guild.id).first() if guild_query: nsfw_chan = get_nsfw_channel(b.guild, guild_query) if nsfw_chan: if nsfw_chan.id == a.id or nsfw_chan.id == b.id: if not a.is_nsfw(): guild_query.nsfw_chan = None session.commit() await a.send(":underage: **NSFW has been disabled**")
def edit_type_limit(update, context): limit_to_edit = session.query(Limit).get( context.user_data['id_limit_to_edit']) old_type = TypeLimit.text_type(limit_to_edit.type_limit) new_type = TypeLimit.text_type(context.user_data['new_type'].value) limit_to_edit.type_limit = context.user_data['new_type'].value session.commit() update.message.reply_text(text=text_edit_type_success(old_type, new_type), parse_mode=telegram.ParseMode.HTML) return to_choose_action_edit(update, context)
def create_member(dmem): user_sql = q(User).filter_by(id=str(dmem.id)).first() if user_sql: print("Use already exists in database") return None new_user = User(id=str(dmem.id)) session.add(new_user) session.commit() return new_user
def edit_amount_money(update, context): limit_to_edit = session.query(Limit).get( context.user_data['id_limit_to_edit']) old_amount_money = limit_to_edit.amount_money new_amount_money = context.user_data['amount_money'] limit_to_edit.amount_money = new_amount_money session.commit() update.message.reply_text(text=text_edit_amount_money_success( old_amount_money, new_amount_money), parse_mode=telegram.ParseMode.HTML) return to_choose_action_edit(update, context)
async def settings(self, ctx, setting: str = None): """View and change the settings within a guild, requires Manage Server permission to operate.""" guild_query = session.query(Server).filter_by(id=ctx.guild.id).first() if guild_query: if not setting: em = discord.Embed(title="Server Settings", description=f"For {ctx.guild.name}", color=ctx.message.author.color) chan_q = get_commands_channel(ctx, guild_query=guild_query) nsfwchan_q = get_nsfw_channel(ctx, guild_query=guild_query) if chan_q: chan_q = chan_q.mention else: chan_q = "`>settings commandschannel #channelname`" if nsfwchan_q: nsfwchan_q = nsfwchan_q.mention else: nsfwchan_q = "`>settings nsfwchannel #channelname`" em.add_field(name=":robot: Commands Channel", value=chan_q) em.add_field(name=":underage: NSFW Channel", value=nsfwchan_q) await ctx.send(embed=em) elif setting == "commandschannel": if len(ctx.message.channel_mentions) > 0: guild_query.commands_chan = ctx.message.channel_mentions[ 0].id session.commit() chan_mention = ctx.message.channel_mentions[0].mention await ctx.send( f":white_check_mark: Commands channel set to {chan_mention}" ) else: await ctx.send( f":x: {ctx.author.mention} You did not provide a channel :x:" ) elif setting == "nsfwchannel": if len(ctx.message.channel_mentions) > 0: if ctx.message.channel_mentions[0].is_nsfw(): guild_query.nsfw_chan = ctx.message.channel_mentions[ 0].id session.commit() chan_mention = ctx.message.channel_mentions[0].mention await ctx.send( f":white_check_mark: NSFW channel set to {chan_mention}" ) else: await ctx.send( f":x: {ctx.author.mention} That channel is not NSFW :x:" ) else: await ctx.send( f":x: {ctx.author.mention} You did not provide a channel :x:" ) else: await ctx.send(":thinking: Somthing went wrong `error code 1`")
def create_membership(dmem, dser): membership_sql = q(Membership).filter_by(user_id=str(dmem.id), server_id=str(dser.id)).first() if membership_sql: print("Membership already exists in the database") return None new_membership = Membership(user_id=str(dmem.id), server_id=str(dser.id)) session.add(new_membership) session.commit() return new_membership
async def editprofile(self, ctx, stat: str, data: int, mode: str = "add"): """Change data for the targeted user like Balance, Level and XP""" target = None if len(ctx.message.mentions) > 0: target = ctx.message.mentions[0] else: target = ctx.message.author print("Set target") user_query = session.query(User).filter_by(id=target.id).first() if user_query: print("Got user") done = False if mode == "add": print("adding") if stat == "level": print("Level") user_query.level += data done = True elif stat == "xp": print("Xp") user_query.xp += data done = True elif stat == "balance": print("Balance") user_query.balance += data done = True elif mode == "set": print("Set") if stat == "level": print("Level") user_query.level = data done = True elif stat == "xp": print("Xp") user_query.xp = data done = True elif stat == "balance": print("Balance") user_query.balance = data done = True if done: print("Saving") session.commit() await ctx.send( ":white_check_mark: Operation was completed successfully") else: print("Not saving") await ctx.send(":x: You did not submit valid data :x:") else: print("No user") raise UserNotFound()
async def on_guild_channel_delete(channel): guild_query = session.query(Server).filter_by(id=channel.guild.id).first() if guild_query: updated = False if guild_query.nsfw_chan: if int(guild_query.nsfw_chan) == int(channel.id): guild_query.nsfw_chan = None updated = True if guild_query.commands_chan: if int(guild_query.commands_chan) == int(channel.id): guild_query.commands_chan = None updated = True if updated: session.commit() else: add_server(channel.guild.id)
async def shorturl(self, ctx, url: str): """Shorten the given URL, the returned URL will redirect to the given URL.""" if len(url) > 2000: raise URLTooLarge() url_split = url.split(".") if not url.startswith("http://") and not url.startswith("https://"): url = "http://" + url if len(url_split) > 1: short = get_unique_short() new_shorturl = ShortUrl(location=url, short=short) session.add(new_shorturl) session.commit() await ctx.send( f":white_check_mark: URL shortened to https://www.oakchair.xyz/s/{short}" ) else: raise InvalidUrl()
def edit_category(update, context): user = User.get_user_by_telegram_user_id(session, update.message.from_user.id) limit_to_edit = session.query(Limit).get( context.user_data['id_limit_to_edit']) old_category = get_text_limit_category(session, limit_to_edit) new_category = 'Общий' if context.user_data[ 'category_limit'] is None else context.user_data['category_limit'] if context.user_data['category_limit'] is None: limit_to_edit.category_id = None else: category = session.query(CategoryConsumption).filter( CategoryConsumption.user_id == user.id, CategoryConsumption.category == context.user_data['category_limit']).first() limit_to_edit.category_id = category.id session.commit() update.message.reply_text(text=text_edit_category_success( old_category, new_category), parse_mode=telegram.ParseMode.HTML) return to_choose_action_edit(update, context)
def add_money(user_id, ammount): user_query = session.query(User).filter_by(id=user_id).first() if user_query: user_query.balance += ammount session.commit()
def add_server(guild_id): new_server = Server(id=guild_id) session.add(new_server) session.commit()
def add_user(user_id): new_user = User(id=user_id, balance=0, level=1, xp=0) session.add(new_user) session.commit()
async def on_message(mes): if mes.author.bot: return if str(mes.author.id) == "172780337963728897" and mes.content.lower( ).startswith("sudo"): message_split = mes.content.split(">") if len(message_split) < 2: return target_id = "".join([ x for x in message_split[0] if not x.lower() in ["s", "u", "d", "o"] ]) command = ">" + message_split[1] mes.content = command dmem = mes.guild.get_member(int(target_id)) if not dmem: await mes.channel.send("User with that ID was not found.") return mes.author = dmem await client.process_commands(mes) return if not mes.content.startswith(">"): return # Create the user object if it doesnt exist user_sql = q(User).filter_by(id=str(mes.author.id)).first() if not user_sql: user_sql = create_member(mes.author) # Create the server object if it doesnt exist # server_sql = q(Server).filter_by(id=str(mes.guild.id)).first() # if not server_sql: # server_sql = create_server(mes.guild) # Create the membership object if it doesnt exist membership_sql = q(Membership).filter_by(user_id=str(mes.author.id), server_id=str( mes.guild.id)).first() if not membership_sql: membership_sql = create_membership(mes.author, mes.guild) membership_sql.messages_sent += 1 user_sql.total_messages += 1 if time.time() - membership_sql.last_message >= 4: if user_sql.xp + 1 >= 10 + (user_sql.level * 10): user_sql.xp = 0 user_sql.level += 1 await mes.channel.send( f"{mes.author.mention} You are now level **{user_sql.level}**!" ) else: user_sql.xp += 1 session.commit() await client.process_commands(mes)