diff options
Diffstat (limited to 'optional')
-rw-r--r-- | optional/README.md | 21 | ||||
-rw-r--r-- | optional/pluscmds.py | 70 | ||||
-rw-r--r-- | optional/plussettingstodb.py | 101 |
3 files changed, 192 insertions, 0 deletions
diff --git a/optional/README.md b/optional/README.md new file mode 100644 index 0000000..9c86307 --- /dev/null +++ b/optional/README.md @@ -0,0 +1,21 @@ +# Optional Addons +Optional commands for the bot + +## Pluscmds: +### Info: +`plussettingstodb.py` Will make a new table in the MySQL database and load the contents from `vplusfile` setting in `config.py` to the table. If the table exist it will delete the table and remake it + +`pluscmds.py` Is bot commands for displaying Valheim Plus setting from the table created by `plussettingstodb.py` + +### Setup: +Move `plussettingstodb.py` to the `code` dir +Move `pluscmds.py` to the `code/botcmds` dir + +### Usage: +`python3 plussettingstodb.py` While in the `code` dir. Script will run and exit when finished. **NOTE:** This will have to be done every time you make a change to the Valheim Plus configuration file. + +After moving the `pluscmds.py` restart the bot. This only needs to be done once, you can run `plussettingstodb.py` at anytime without having to restart the bot. + +### Example Output: +## Pluscmds: +![](../example/vplushelp.png) diff --git a/optional/pluscmds.py b/optional/pluscmds.py new file mode 100644 index 0000000..6c91147 --- /dev/null +++ b/optional/pluscmds.py @@ -0,0 +1,70 @@ +import discord, typing +from discord.ext import commands + +class Plus(commands.Cog): + """ + Plus commands + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='vplus', + brief="Plus settings", + help="Shows Plus mod settings. \n Available arg: enabled, disabled, section name \n Enabled: Shows enabled sections. \n Disabled: Shows disabled sections. \n Section Name: Shows settings for that section.", + usage="<arg>", + ) + async def vplus(self, ctx, arg: typing.Optional[str] = 'help'): + ldrembed = discord.Embed(title="Valheim Plus Settings " + arg + "", color=0x33a163) + botsql = self.bot.get_cog('BotSQL') + mycursor = await botsql.get_cursor() + if arg == "help": + ldrembed.add_field(name="{}vplus enabled".format(self.bot.command_prefix), + value="Shows list of enabled sections.\n Example: `{}vplus enabled`".format(self.bot.command_prefix), + inline=True) + ldrembed.add_field(name="{}vplus disabled".format(self.bot.command_prefix), + value="Shows list of disabled sections.\n Example: `{}vplus disabled`".format(self.bot.command_prefix), + inline=True) + ldrembed.add_field(name="{}vplus <section>".format(self.bot.command_prefix), + value="Shows setting for section.\n Example: `{}vplus Fermenter`".format(self.bot.command_prefix), + inline=True) + elif arg == "enabled": + sql = """SELECT section FROM vplus WHERE enabled = 'true' AND option1 = '0'""" + mycursor.execute(sql) + Info = mycursor.fetchall() + row_count = mycursor.rowcount + vcount = 0 + for ind in Info: + if vcount == 1: + description = description + '\n' + ind[0] + else: + description = ind[0] + vcount = 1 + ldrembed = discord.Embed(title="Valheim Plus Settings " + arg + "", description="" + description + "", color=0x33a163) + elif arg == "disabled": + sql = """SELECT section FROM vplus WHERE enabled = 'false' AND option1 = '0'""" + mycursor.execute(sql) + Info = mycursor.fetchall() + row_count = mycursor.rowcount + vcount = 0 + for ind in Info: + if vcount == 1: + description = description + '\n' + ind[0] + else: + description = ind[0] + vcount = 1 + ldrembed = discord.Embed(title="Valheim Plus Settings " + arg + "", description="" + description + "", color=0x33a163) + else: + sql = """SELECT option1, settings FROM vplus WHERE section = '%s' AND option1 != '0'""" % (arg) + mycursor.execute(sql) + Info = mycursor.fetchall() + row_count = mycursor.rowcount + for ind in Info: + ldrembed.add_field(name="{}".format(ind[0]), + value="{}".format(ind[1]), + inline=False) + mycursor.close() + await ctx.send(embed=ldrembed) + +def setup(bot): + bot.add_cog(Plus(bot)) diff --git a/optional/plussettingstodb.py b/optional/plussettingstodb.py new file mode 100644 index 0000000..25a948f --- /dev/null +++ b/optional/plussettingstodb.py @@ -0,0 +1,101 @@ +import os, re, config, sys, mysql.connector +from colorama import Fore, Style, init +from mysql.connector import errorcode +from config import SQL_HOST as MYhost +from config import SQL_PORT as MYport +from config import SQL_USER as MYuser +from config import SQL_PASS as MYpass +from config import SQL_DATABASE as MYbase +from config import vplusfile + +section = '^\[([\w]*)\]$' +settings = '^(\w*) = (\w*)$' + +TABLES = {} +TABLES['vplus'] = ( + "CREATE TABLE `vplus` (" + " `id` int NOT NULL AUTO_INCREMENT," + " `section` varchar(75) DEFAULT NULL," + " `enabled` varchar(75) DEFAULT NULL," + " `option1` varchar(75) DEFAULT NULL," + " `settings` varchar(75) DEFAULT NULL," + " PRIMARY KEY (`id`)" + ") ENGINE=InnoDB") + +def mydbconnect(): + global mydb + mydb = mysql.connector.connect( + host=MYhost, + user=MYuser, + password=MYpass, + database=MYbase, + port=MYport, + ) + try: + if mydb.is_connected(): + db_Info = mydb.get_server_info() + print(Fore.GREEN + "Connected to MySQL database... MySQL Server version ", db_Info + Style.RESET_ALL) + except mysql.connector.Error as err: + print(Fore.RED + err + 'From MySQL database' + Style.RESET_ALL) + +mydbconnect() + +def maketable(): + mycursor = mydb.cursor() + for table_name in TABLES: + table_description = TABLES[table_name] + try: + print(Fore.GREEN + "Creating table {}: ".format(table_name), end='') + mycursor.execute(table_description) + except mysql.connector.Error as err: + if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: + print(Fore.RED + "already exists. Droping table and trying again." + Style.RESET_ALL) + sql = "DROP TABLE vplus" + mycursor.execute(sql) + maketable() + else: + print(Fore.RED + err.msg + Style.RESET_ALL) + else: + print(Fore.GREEN + "OK" + Style.RESET_ALL) + mycursor.close() + +def mainloop(): + try: + plusconf = open(vplusfile, mode="r") + Lines = plusconf.readlines() + print(Fore.GREEN + "Adding Valheim Plus settings to table vplus" + Style.RESET_ALL) + mycursor = mydb.cursor() + section1 = "ValheimPlus" + enabled1 = 0 + option = 0 + setting = 0 + for line in Lines: + insert = 0 + if(re.search(section, line)): + section1 = re.search(section, line).group(1) + insert = 0 + enabled1 = 0 + option = 0 + setting = 0 + if(re.search(settings, line)): + what = re.search(settings, line).group(1) + setting = re.search(settings, line).group(2) + insert = 1 + if what == "enabled": + enabled1 = setting + else: + option = what + if insert == 1: + sql = """INSERT INTO vplus (section, enabled, option1, settings) VALUES ('%s', '%s', '%s', '%s')""" % (section1, enabled1, option, setting) + mycursor.execute(sql) + mydb.commit() + plusconf.close() + mycursor.close() + print(Fore.GREEN + "Done" + Style.RESET_ALL) + except IOError: + print(Fore.RED + 'Could not read Valheim Plus config file. Please check config.py' + Style.RESET_ALL) + +maketable() +mainloop() +mydb.close() +exit() |