async def userinfo(self, ctx, *, user: discord.Member = None): user = user or ctx.author user_roles_dict = [x.name for x in user.roles if x.name != '@everyone'] user_roles = ', '.join(user_roles_dict) or "-" user_created_at = user.created_at.strftime("%d %b %Y %H:%M:%S") user_joined_at = user.joined_at.strftime("%d %b %Y %H:%M:%S") lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang embed = discord.Embed(title=f'{user}', color=self.embed_color) embed.set_thumbnail(url="{}".format(user.avatar_url)) embed.add_field(name=lang["utility_uinfo_id"], value=user.id, inline=True) embed.add_field(name=lang["utility_uinfo_nickname"], value=user.nick, inline=True) embed.add_field(name=lang["utility_uinfo_created"], value=f'{user_created_at} UTC', inline=False) embed.add_field(name=lang["utility_uinfo_joined"], value=f'{user_joined_at} UTC', inline=True) embed.add_field(name=lang["utility_uinfo_sroles"].format( len(user_roles_dict)), value=user_roles, inline=False) await ctx.send(embed=embed)
async def give(self, ctx, amount, *, user: discord.User): author = ctx.author author_info = await user_get(author.id) user_info = await user_get(user.id) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if author != user: try: amount = int(amount) if amount > 0: if author_info['currency'] <= 0 or amount > author_info[ 'currency']: await ctx.send(lang["economy_insufficient_funds"], delete_after=15) return else: raise commands.UserInputError( "This command accepts only positive values.") except ValueError: if isinstance(amount, str) and amount.lower() == "all": amount = author_info['currency'] else: raise commands.UserInputError( f"Invalid user input provided to the command '{ctx.command.qualified_name}'." ) author_info['currency'] -= amount user_info['currency'] += amount await user_set(author.id, author_info) await user_set(user.id, user_info) await ctx.send( embed=discord.Embed(title=lang["economy_give_success"].format( author, user, amount, self.currency_symbol), color=self.embed_color)) else: await ctx.send(lang["economy_give_self"], delete_after=15)
async def settings_reset(self, ctx): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang embed = discord.Embed( title=lang["settings_reset_confirmation_title"], description=lang["settings_reset_confirmation_description"], color=self.embed_color) embed.set_footer(text=lang["settings_reset_confirmation_footer"]) await ctx.send(embed=embed) try: msg = await self.bot.wait_for( 'message', check=lambda m: (m.content.lower() in ['yes', 'y', 'no', 'n']) and m.channel == ctx.channel and m.author == ctx.author, timeout=60.0) if msg.content.lower() == 'yes' or msg.content.lower() == 'y': self.PrefixHandler.remove_guild( ctx.guild.id ) # PrefixHandler takes care of language data removal as well since its tied to the same table await self.CommandController.remove_disabled_commands( ctx.guild.id) await self.CogController.remove_disabled_cogs(ctx.guild.id) await self.Toggles.wipe_guild_data(ctx.guild.id) await self.Modlog.wipe_data(ctx.guild.id) await ctx.send(embed=discord.Embed( title=lang["settings_reset_success_title"], description=lang["settings_reset_success_description"], color=self.embed_color)) else: await ctx.send(lang["wait_for_cancelled"], delete_after=15) except asyncio.TimeoutError: await ctx.send(lang["wait_for_timeout"], delete_after=15)
async def module_unload(self, ctx, *, input_module: str): try: input_module_path = f'modules.{input_module}' if input_module_path in loaded_modules: self.bot.unload_extension(input_module_path) except Exception as e: embed = discord.Embed( title=f'**`ERROR:`** {type(e).__name__} - {e}', color=self.embed_color) else: lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if input_module_path not in loaded_modules: embed = discord.Embed(title=lang["core_module_unload_fail"], color=self.embed_color) else: embed = discord.Embed( title=lang["core_module_unload_success"].format( input_module), color=self.embed_color) loaded_modules.remove(input_module_path) with open(os.path.join(bot_path, 'data', 'modules.mdls'), 'r+') as modules_file: modules_output = modules_file.read() modules_file.seek(0) for i in modules_output.split(): if i != input_module: modules_file.write(f'{i}\n') modules_file.truncate() modules_file.close() await ctx.send(embed=embed, delete_after=15)
async def on_member_join(self, member): status = await self.Toggles.get_greet_status(member.guild.id) if status: channel_id = await self.Toggles.get_greetings_channel( member.guild.id) if channel_id: channel = await self.bot.fetch_channel(channel_id) else: channel = discord.utils.find(lambda c: c.name == "general", member.guild.text_channels) if not channel: channel = discord.utils.find( lambda c: member.guild.me.permissions_in(c). send_messages == True, member.guild.text_channels) if channel: if await self.Toggles.has_custom_greeting(member.guild.id): message = await self.Toggles.get_custom_greeting( member.guild.id) message = await Automation.format_message(message, member) else: lang = main.get_lang(member.guild.id) or main.lang message = lang["automation_greet_default"].format( member.mention, member.guild.name) if status == 2: await member.send(message) else: await channel.send(message)
async def translate(self, ctx): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang embed = discord.Embed(title=lang["core_translate_title"], description=lang["core_translate_description"], color=self.embed_color) embed.set_thumbnail(url=str(self.bot.user.avatar_url)) await ctx.send(embed=embed)
async def mass_nickname_reset(self, ctx, members: commands.Greedy[discord.Member]): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if members: output = "" for member in members: try: await member.edit(nick=None) output += f'{str(member)} ({member.mention})\n' except discord.Forbidden: pass await ctx.message.delete() output = self.slice_userlist(output, 20, lang) if len(output.strip()) > 0: embed = discord.Embed( title=lang["utility_massnick_reset_embed_title"], description=lang["utility_massnick_reset_embed_desc"], timestamp=datetime.utcnow(), color=self.embed_color) embed.add_field(name=lang["users_string"], value=output or lang["empty_string"], inline=True) embed.set_footer( text=lang["utility_massnick_reset_embed_footer"].format( str(ctx.author))) await ctx.send(embed=embed) else: await ctx.send(lang["utility_massnick_empty"].format( ctx.author.mention), delete_after=15) else: await ctx.send(lang["utility_massnick_not_found"].format( ctx.author.mention), delete_after=15)
async def setactivity_stats_disable(self, ctx): self.botstats_loop.cancel() # pylint: disable=no-member await ctx.bot.change_presence(activity=None, shard_id=None) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send(embed=discord.Embed( title=lang["core_setactivity_stats_reset"], color=self.embed_color) )
async def commands_enable(self, ctx, *, command: str): cmds_list = [x.name for x in self.bot.commands] aliases_list = [ x.aliases for x in self.bot.commands if len(x.aliases) > 0 ] aliases_list = [item for sublist in aliases_list for item in sublist] if ctx.command.root_parent and str( ctx.command.root_parent).strip() in cmds_list: cmds_list.remove(str(ctx.command.root_parent).strip()) if len(ctx.command.root_parent.aliases) > 0: for alias in ctx.command.root_parent.aliases: aliases_list.remove(alias.strip()) if command.lower() in cmds_list or command.lower() in aliases_list: command_obj = self.bot.get_command(command) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if CommandController.is_disabled(command_obj.name, ctx.guild.id): await CommandController.enable_command(command_obj.name, ctx.guild.id) embed = discord.Embed(title=lang["core_command_enable"].format( command_obj.name), color=self.embed_color) else: embed = discord.Embed( title=lang["core_command_already_enabled"].format( command_obj.name), color=self.embed_color) await ctx.send(embed=embed, delete_after=15) else: raise commands.errors.BadArgument( "Could not enable/disable command. Command not found.")
async def setactivity(self, ctx, input_activity: str = None, *, text: str = None): if not ctx.invoked_subcommand: lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if input_activity and text: input_activity = input_activity.lower() if input_activity in ('playing', 'listening', 'watching'): type_dict = {'playing': 0, 'listening': 2, 'watching': 3} activity = discord.Activity(type=type_dict.get( input_activity, None), name=text) await ctx.bot.change_presence(activity=activity, shard_id=None) await ctx.send( embed=discord.Embed(title=lang["core_setactivity"]. format(f'{input_activity} {text}'), color=self.embed_color)) else: self.botstats_loop.cancel() # pylint: disable=no-member await ctx.bot.change_presence(activity=None, shard_id=None) await ctx.send( embed=discord.Embed(title=lang["core_setactivity_reset"], color=self.embed_color))
async def roll(self, ctx, number: int = 100): random.seed() lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send(embed=discord.Embed( title=lang["utility_roll_msg"].format(random.randrange(number + 1)), color=self.embed_color))
async def eight_ball(self, ctx, *, string: str): random.seed() ball_answer = random.randrange(1, 21) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send(embed=discord.Embed( description=f'\U0001F3B1 {lang[f"utility_8ball_{ball_answer}"]}', color=self.embed_color))
async def daily(self, ctx, *, user: discord.User = None): author_info = await user_get(ctx.author.id) user = user or ctx.author if user.id is not ctx.author.id: user_info = await user_get(user.id) time_on_command = datetime.utcnow().replace(microsecond=0) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if author_info['daily_time']: daily = datetime.strptime(author_info['daily_time'], '%Y-%m-%d %H:%M:%S') daily_active = daily + dt.timedelta(days=1) if time_on_command < daily_active: await ctx.send(embed=discord.Embed( title=lang["economy_daily_claimed_title"], description=lang["economy_daily_claimed_description"]. format(f'**{qulib.humanize_time(lang, daily_active)}**'), color=self.embed_color)) return if user.id is ctx.author.id: author_info['currency'] += self.daily_amount embed = discord.Embed(title=lang["economy_daily_received"].format( user, self.daily_amount, self.currency_symbol), color=self.embed_color) else: user_info['currency'] += self.daily_amount await user_set(user.id, user_info) embed = discord.Embed(title=lang["economy_daily_gifted"].format( ctx.author, self.daily_amount, self.currency_symbol, user), color=self.embed_color) author_info['daily_time'] = time_on_command await user_set(ctx.author.id, author_info) await ctx.send(embed=embed)
async def vote_command(self, ctx): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send(embed=discord.Embed( title=lang["voting_vote_embed_title"], description=lang["voting_vote_embed_description"].format( self.vote_reward, self.currency_symbol), color=self.embed_color))
async def choose_random_items(self, ctx, *, choices: str): items = choices.split(';') lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send( embed=discord.Embed(description=lang["utility_choose_msg"].format( "\U0001F3B2", secrets.choice(items).lstrip()), color=self.embed_color))
async def avatar(self, ctx, *, user: discord.User = None): user = user or ctx.author lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang embed = discord.Embed(title=lang["utility_avatar_msg"].format( user.name), color=self.embed_color) embed.set_image(url=user.avatar_url) await ctx.send(embed=embed)
async def currency(self, ctx, *, user: discord.User = None): user = user or ctx.author user_info = await user_get(user.id) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send( embed=discord.Embed(title=lang["economy_currency_msg"].format( user, user_info['currency'], self.currency_symbol), color=self.embed_color))
async def goodbye_custom_message_reset(self, ctx): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if await self.Toggles.has_custom_goodbye(ctx.guild.id): await self.Toggles.reset_bye_msg(ctx.guild.id) await ctx.send(embed=discord.Embed( title=lang["automation_gbmessage_default_success"], color=self.embed_color)) else: await ctx.send(lang["automation_gbmessage_default_already_used"], delete_after=15)
async def giveaway_start(self, ctx, value: int): await ctx.message.delete() lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang embed = discord.Embed(title=lang["economy_cgiveaway_title"], description=lang["economy_cgiveaway_msg"].format( self.currency_symbol, value, self.currency_symbol), color=self.embed_color) message = await ctx.send(embed=embed) await message.add_reaction(self.currency_symbol) await GiveawayHandler.start_giveaway(message.id, value)
async def restart(self, ctx): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.send(embed=discord.Embed(title=lang["core_restart"], color=self.embed_color)) try: process = psutil.Process(os.getpid()) for handler in process.open_files() + process.connections(): os.close(handler.fd) except Exception as e: logger.error(e) os.execl(sys.executable, sys.executable, *sys.argv)
async def greetings_disable(self, ctx): status = await self.Toggles.get_greet_status(ctx.guild.id) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if status: await self.Toggles.disable_greeting(ctx.guild.id) await ctx.send(embed=discord.Embed( title=lang["automation_gdisable_embed_title"], description=lang["automation_gdisable_embed_description"], color=self.embed_color)) else: await ctx.send(lang["automation_gdisable_already_disabled"], delete_after=15)
async def prefix(self, ctx, *, new_prefix: str = None): if not ctx.invoked_subcommand: lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if not new_prefix: await ctx.invoke(self.prefix_show) elif len(new_prefix) > main.max_prefix_length: await ctx.send(lang["settings_prefix_length_limit"].format( main.max_prefix_length), delete_after=15) else: self.PrefixHandler.set_prefix(ctx.guild.id, new_prefix) await ctx.send(embed=discord.Embed( title=lang["settings_prefix_success"].format(new_prefix), color=self.embed_color))
async def dictionary_get_urbandict(self, ctx, *, input: str): result = await quDict.get_urbandict_definitions(input) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if result != None: embed = discord.Embed( title=lang["dictionaries_term"].format(input), color=self.embed_color) formatted = '\n'.join(result) embed.add_field(name=lang["dictionaries_urbandict_title"], value=formatted, inline=False) await ctx.send(embed=embed) else: await ctx.send(lang["dictionaries_word_not_found"])
async def modules(self, ctx): if not ctx.invoked_subcommand: modules_list = '' loaded_modules_names = [ i.replace('modules.', '') for i in loaded_modules ] modules_config = qulib.get_module_config() for i in loaded_modules_names: if i not in modules_config.setdefault("hidden_modules", []): modules_list += f'\u2022 {i}\n' lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang await ctx.author.send( embed=discord.Embed(title=lang["core_modules_list"], description=modules_list, color=self.embed_color))
async def modules_disable(self, ctx, *, input_module: str): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang modules_config = qulib.get_module_config() loaded_modules_names = [ i.replace('modules.', '') for i in loaded_modules ] loaded_modules_lowercase = [i.lower() for i in loaded_modules_names] if input_module.lower() in loaded_modules_lowercase: cog_name = loaded_modules_names[loaded_modules_lowercase.index( input_module.lower())] if not CogController.is_disabled(cog_name, ctx.guild.id): disabled_cogs = await CogController.disabled_cogs(ctx.guild.id) loaded_dependencies = [] for cog in modules_config.setdefault("dependencies", {}): if cog_name in modules_config.setdefault( "dependencies", {})[cog]: if not disabled_cogs or cog not in disabled_cogs: loaded_dependencies.append(cog) if len(loaded_dependencies) > 0: embed = discord.Embed( title=lang["core_module_disable_dependencies"].format( ', '.join(str(e) for e in loaded_dependencies)), color=self.embed_color) embed.set_footer( text=lang["core_module_disable_dependencies_hint"]) await ctx.send(embed=embed, delete_after=30) return if cog_name not in modules_config["restricted_modules"]: await CogController.disable_cog(cog_name, ctx.guild.id) embed = discord.Embed( title=lang["core_module_disable_msg"].format(cog_name), color=self.embed_color) else: embed = discord.Embed( title=lang["core_module_disable_restricted"].format( cog_name), color=self.embed_color) else: embed = discord.Embed( title=lang["core_module_disable_already_disabled"].format( cog_name), color=self.embed_color) else: embed = discord.Embed(title=lang["core_module_disable_not_found"], color=self.embed_color) await ctx.send(embed=embed, delete_after=30)
async def dictionary_getmeanings(self, ctx, *, input: str): result = await quDict.get_top_meanings(input) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if result != None: embed = discord.Embed( title=lang["dictionaries_term"].format(input), color=self.embed_color) for category in result: meanings = "" for item in result[category]: meanings += f"- {item};\n" embed.add_field(name=f"**{category}**", value=meanings, inline=False) await ctx.send(embed=embed) else: await ctx.send(lang["dictionaries_word_not_found"])
async def adjust(self, ctx, value: int, *, user: discord.User): await ctx.message.delete() user_info = await user_get(user.id) user_info['currency'] += value await user_set(user.id, user_info) lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if value > 0: embed = discord.Embed( title=lang["economy_adjust_award_msg"].format( user, value, self.currency_symbol), color=self.embed_color) else: embed = discord.Embed( title=lang["economy_adjust_subtract_msg"].format( user, abs(value), self.currency_symbol), color=self.embed_color) await ctx.send(embed=embed)
async def botinfo(self, ctx): lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang shards_guild_counter = 0 total_guild_users = 0 current_shard_latency = None for shard in self.bot.latencies: guild_list = (g for g in self.bot.guilds if g.shard_id is shard[0]) for guild in guild_list: shards_guild_counter += 1 total_guild_users += guild.member_count if guild.shard_id == shard[0]: current_shard_latency = "%.4f" % float(shard[1] * 1000) app_info = await self.bot.application_info() embed = discord.Embed(title=lang["utility_binfo_title"], color=self.embed_color) embed.set_thumbnail(url=f"{self.bot.user.avatar_url}") embed.add_field(name=lang["utility_binfo_bname"], value=app_info.name, inline=True) embed.add_field(name=lang["owner_string"], value=str(app_info.owner), inline=True) embed.add_field(name=lang["version_string"], value=main.version, inline=True) embed.add_field(name=lang["guilds_string"], value=shards_guild_counter, inline=True) embed.add_field(name=lang["users_string"], value=total_guild_users, inline=True) embed.add_field(name=lang["utility_binfo_latency"], value=f'{current_shard_latency} ms', inline=True) embed.add_field( name=lang["uptime_string"], value=f"```{qulib.humanize_time(lang, bot_starttime)}```", inline=False) embed.set_footer(text=lang["utility_binfo_author_footer"]) await ctx.send(embed=embed)
async def lang_set(self, ctx, lang_code: str = None): lang_directory_list = [ os.path.splitext(i)[0] for i in os.listdir(os.path.join(bot_path, 'data', 'localization')) if ("language" in os.path.splitext(i)[0] and os.path.splitext(i)[1] == ".json") ] lang_list = [x.replace('language_', '') for x in lang_directory_list] lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang if lang_code in lang_list: if lang_code != self.Localization.get_language( ctx.guild.id, main.languagecode): self.Localization.set_language(ctx.guild.id, lang_code) await ctx.send(embed=discord.Embed( title=lang["settings_langset_success"].format(lang_code), color=self.embed_color)) else: await ctx.send(lang["settings_langset_same"], delete_after=15) else: await ctx.send(lang["settings_langset_notfound"], delete_after=15)
async def on_guild_join(self, guild): lang = main.get_lang(guild.id) if guild else main.lang guild_prefix = PrefixHandler.get_prefix( guild.id, main.prefix) if guild else main.prefix app_info = await self.bot.application_info() channel = discord.utils.find(lambda c: c.name == "general", guild.text_channels) if not channel: channel = discord.utils.find( lambda c: guild.me.permissions_in(c).send_messages == True, guild.text_channels) if channel: embed = discord.Embed( title=lang["bot_guild_join_title"], description=lang["bot_guild_join_description"].format( app_info.name, guild_prefix), color=self.embed_color) embed.set_thumbnail(url=f"{self.bot.user.avatar_url}") await channel.send(embed=embed)