Make last och time guild specific
This commit is contained in:
23
bot.py
23
bot.py
@@ -10,7 +10,8 @@ import gtts
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from lib.config import config, config_meta, config_load, config_save, config_get, config_set, config_get_descriptions
|
from lib.config import config, config_meta, config_load, config_save, config_get, config_set, config_get_descriptions, \
|
||||||
|
config_set_raw
|
||||||
from lib.utils import async_filter
|
from lib.utils import async_filter
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@@ -44,8 +45,6 @@ OWNER_ID = 327126546970312739
|
|||||||
|
|
||||||
OCH_LOEH_SOUND = "assets/och_loeh.mp3"
|
OCH_LOEH_SOUND = "assets/och_loeh.mp3"
|
||||||
|
|
||||||
last_och = 0
|
|
||||||
|
|
||||||
config_load()
|
config_load()
|
||||||
bot = commands.Bot(command_prefix=config.get('prefix'))
|
bot = commands.Bot(command_prefix=config.get('prefix'))
|
||||||
|
|
||||||
@@ -60,6 +59,14 @@ async def _get_loeh(guild: discord.Guild) -> Optional[discord.Member]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_last_och_time(guild: discord.Guild) -> int:
|
||||||
|
return config_get('last-och-time', guild.id)
|
||||||
|
|
||||||
|
|
||||||
|
def _set_last_och_time(guild: discord.Guild):
|
||||||
|
config_set_raw('last-och-time', time.time(), guild.id)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f'{bot.user} has connected to Discord!')
|
print(f'{bot.user} has connected to Discord!')
|
||||||
@@ -67,17 +74,17 @@ async def on_ready():
|
|||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message: discord.Message):
|
async def on_message(message: discord.Message):
|
||||||
global last_och
|
|
||||||
if message.guild is not None and not message.author.bot:
|
if message.guild is not None and not message.author.bot:
|
||||||
if config_get('loeh-enable', message.guild.id) and LOEH_REGEX.match(message.content):
|
if config_get('loeh-enable', message.guild.id) and LOEH_REGEX.match(message.content):
|
||||||
if message.author.id == LOEH_ID:
|
if message.author.id == LOEH_ID:
|
||||||
await message.channel.send("https://siphalor.de/img/spidy-is-that-you.jpg")
|
await message.channel.send("https://siphalor.de/img/spidy-is-that-you.jpg")
|
||||||
else:
|
else:
|
||||||
t = time.time()
|
t = time.time()
|
||||||
if t - config_get('och-cooldown', message.guild.id) <= last_och:
|
if t - config_get('och-cooldown', message.guild.id) <= _get_last_och_time(message.guild):
|
||||||
await message.channel.send('429: Too many requests')
|
await message.channel.send('429: Too many requests')
|
||||||
return
|
return
|
||||||
last_och = t
|
_set_last_och_time(message.guild)
|
||||||
|
|
||||||
loeh = await _get_loeh(message.guild)
|
loeh = await _get_loeh(message.guild)
|
||||||
if loeh is None:
|
if loeh is None:
|
||||||
await message.channel.send('404: Löh not found!')
|
await message.channel.send('404: Löh not found!')
|
||||||
@@ -107,10 +114,10 @@ async def on_message(message: discord.Message):
|
|||||||
elif config_get('och-anyone-enable', message.guild.id) and (
|
elif config_get('och-anyone-enable', message.guild.id) and (
|
||||||
match := OCH_ANYONE_REGEX.search(message.content)) is not None:
|
match := OCH_ANYONE_REGEX.search(message.content)) is not None:
|
||||||
t = time.time()
|
t = time.time()
|
||||||
if t - config_get('och-cooldown', message.guild.id) <= last_och:
|
if t - config_get('och-cooldown', message.guild.id) <= _get_last_och_time():
|
||||||
await message.channel.send('429: Too many requests')
|
await message.channel.send('429: Too many requests')
|
||||||
return
|
return
|
||||||
last_och = t
|
_set_last_och_time()
|
||||||
|
|
||||||
guild: discord.Guild = message.guild
|
guild: discord.Guild = message.guild
|
||||||
bot_member: discord.Member = guild.get_member(bot.user.id)
|
bot_member: discord.Member = guild.get_member(bot.user.id)
|
||||||
|
|||||||
@@ -78,6 +78,14 @@ def config_set(key: str, value, guild: Optional[int] = None) -> str:
|
|||||||
return 'This config can only be changed globally by the bot owner!'
|
return 'This config can only be changed globally by the bot owner!'
|
||||||
|
|
||||||
|
|
||||||
|
def config_set_raw(key: str, value, guild: Optional[int] = None):
|
||||||
|
if guild is None:
|
||||||
|
config[key] = value
|
||||||
|
else:
|
||||||
|
_get_guild_config(guild)[key] = value
|
||||||
|
config_save()
|
||||||
|
|
||||||
|
|
||||||
def config_get_descriptions() -> iter:
|
def config_get_descriptions() -> iter:
|
||||||
return map(lambda entry: (entry[0], entry[1]), config_meta.items())
|
return map(lambda entry: (entry[0], entry[1]), config_meta.items())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user