config system

master
Emojigit 2021-03-22 11:59:14 +08:00
parent f9681b9eb4
commit 0c3f425ae2
2 changed files with 57 additions and 2 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ __pycache__/
*.pyc
baseURL.txt
token.txt
config.json

58
main.py
View File

@ -1,5 +1,5 @@
from modules import getlink
import requests, discord, logging, git, os
import requests, discord, logging, git, os, json
from discord.ext import commands
from git.exc import InvalidGitRepositoryError
log = logging.getLogger("MainScript" if __name__ == "__main__" else __name__)
@ -27,8 +27,21 @@ def token():
def baseURL():
return GFC("baseURL.txt")
def load():
try:
with open("config.json","r") as f:
return json.loads(f.read())
except FileNotFoundError:
log.error("No config.json!")
return {}
bot = commands.Bot(command_prefix='/', description="Wikipedia Link Bot")
def save(cont):
log.info("saving config with content:{}".format(str(cont)))
with open("config.json","w+") as f:
f.write(json.dumps(cont))
bot = commands.Bot(command_prefix='/', description="Wikipedia Link Bot", intents=discord.Intents.default())
@bot.event
async def on_ready():
@ -39,6 +52,32 @@ async def on_ready():
bu = baseURL()
@bot.command()
async def conf(ctx, cname: str, value: str):
if cname == "disable_link_reply":
if value == "True" or value == "true":
AVAL = True
STAT = "Success"
elif value == "False" or value == "false":
AVAL = False
STAT = "Success"
else:
AVAL = None
STAT = "Failed"
else:
await ctx.send("invalid setting")
return
ALLC = load()
try:
CONF = ALLC[str(ctx.guild.id)]
except KeyError:
ALLC[str(ctx.guild.id)] = {}
CONF = ALLC[str(ctx.guild.id)]
if AVAL != None:
CONF[cname] = AVAL
save(ALLC)
await ctx.send(STAT)
@bot.event
async def on_message(message):
# don't respond to ourselves
@ -48,13 +87,28 @@ async def on_message(message):
chans.append(message.channel)
if dirty():
await message.channel.send("This bot is not in the stable state.")
ALLC = load()
try:
CONF = ALLC[str(message.channel.guild.id)]
except KeyError:
ALLC[str(message.channel.guild.id)] = {}
CONF = ALLC[str(message.channel.guild.id)]
try:
if CONF["disable_link_reply"] == True:
print("dlr")
await bot.process_commands(message)
return
except KeyError:
pass
titles = getlink.gl(message.content)
RTXT = ""
for x in titles:
RTXT = RTXT + bu.format(x) + "\n"
if RTXT != "":
await message.channel.send(RTXT)
await bot.process_commands(message)
return
@bot.event
async def on_guild_join(G):