Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Slash Commands Migration
Browse files Browse the repository at this point in the history
- Migrated all commands to Slash Ones
- Updated NextCord dependency
- Added command to force check
- Optimizations* disabled bot intents for messages which should have to load less..
  • Loading branch information
UrekD committed Jun 7, 2022
1 parent 2226d5e commit 29568d8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 136 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ where = 1234 #where to send update notification
nrole = 1234 #role to ping on update
ctime = 900 #check interval in seconds
#collectionid = 1332156191 #Collection to fill the config with, fills on start if left uncommented!
cname = 'workshop' #channel name where you can use commands $list $remove $add
cdelay = 1 #Seconds to delay check for each mod in config, increase if rate limit errors accur
rdelay = 5 # Delay on recheck in seconds
cretry = 3 # How many times to retry the recheck mod
Expand Down
38 changes: 3 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ services:
- where=935678087209856 #channel ID to send notification
- nrole=9124604480608221 #role id to ping on notify
- ctime=900 #time between checks in seconds
- cname=workshop #channel name where it responds to commands
- collectionid=1332156191 #workshop collection id found in url
- cdelay=1 #delay between each mods checked in seconds
- rdelay=5 #delay on recheck in seconds
- rdelay=5 #delay on recheck in seconds
- cretry=3 #how many times to retry the recheck mod
- fdelay=300 #how many seconds to wait until monitor runs again after failure ex. on internet failure
volumes:
Expand All @@ -59,41 +58,10 @@ volumes:
botdataa:

# Commands
**$ping**
Integrated with Discord Slash commands!

![image](https://user-images.githubusercontent.com/38784343/140180871-9647cb59-8bdd-4af5-bccf-f7864e572628.png)
![image](https://user-images.githubusercontent.com/38784343/172271297-94e52c55-05b8-4860-ad5d-cdd5c7506d00.png)

**$list**

Returns a list of mods in a JSON object, format 'MODID#TIME'.
Time format is in Unix epoch time https://www.epochconverter.com/

![image](https://user-images.githubusercontent.com/38784343/140181008-43802124-4154-461f-ad78-50a2a69f9425.png)

**$remove**

Firstly locate the mod via the ID and copy the whole format and remove it via the command.

![image](https://user-images.githubusercontent.com/38784343/140181570-7d4b4d49-3468-4919-9571-febe9ccd0ad8.png)

**$add**

Add the mod with the following format MODID#000 ex. "$add 450814997#000"
If you use 000 it should not trigger a update notification.

![image](https://user-images.githubusercontent.com/38784343/140181637-731a1a32-6538-406e-8fcc-0e5eb925c143.png)

**$clear**

Removes all mods from the config.

![image](https://user-images.githubusercontent.com/38784343/168385393-bf913d4a-b755-4a8f-b0bc-7228f1c09bb6.png)

**$refill**

Overwrites the config with the mods in workshop collection.

![image](https://user-images.githubusercontent.com/38784343/168385478-0656d059-3053-4be8-b769-4731daac4c20.png)

# Example

Expand Down
213 changes: 113 additions & 100 deletions WorkshopMonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import math
import nextcord
from nextcord.ext import commands
from nextcord import Interaction, SlashOption, ChannelType
import nextcord
from dotenv import load_dotenv

load_dotenv()
Expand All @@ -24,10 +26,10 @@
fdelay = int(os.getenv('fdelay'))
collectionid = os.getenv('collectionid')
status = os.getenv('status')
cname = os.getenv('cname')
oup = False

event = asyncio.Event()
intents = nextcord.Intents(messages=False, guilds=True)

async def Monitor():
now = datetime.datetime.now()
Expand Down Expand Up @@ -105,8 +107,7 @@ def CollectionToConfig(id):
ids.append(f"{temp}#000")
config["userdata"]['workshopid'] = ids
with open('data/config.json', "w") as jsfile:
json.dump(config, jsfile)
jsfile.close()
jsfile.write(json.dumps(config, indent=4, sort_keys=True))
except Exception as exc:
print(f"{Fore.RED}[ERROR] {Style.RESET_ALL}Collection was not parsed correctly try again! {exc}")

Expand All @@ -127,7 +128,7 @@ async def on_resumed(self):
async def on_ready(self):
x = status
if x is None:
x = "git.urek.eu | $help"
x = "git.urek.eu | /help"
await bot.change_presence(activity=nextcord.Game(name=x))
now = datetime.datetime.now()
print(now.strftime(f'{Fore.MAGENTA}[INFO] %H:%M:%S {Fore.RED}{bot.user}{Style.RESET_ALL} has connected to nextcord!'))
Expand All @@ -146,121 +147,133 @@ async def my_background_task(self,LA):
await channel.send("Checking finished sleeping...")
event.clear()
await asyncio.sleep(ctime)
except:
except Exception() as x:
event.clear()
now = datetime.datetime.now()
print(x)
print(now.strftime(f"{Fore.MAGENTA}[STOP] {Style.RESET_ALL}%H:%M:%S An error has accured current monitor cycle skipped!"))
await asyncio.sleep(fdelay)

bot = Bot(command_prefix='$')
bot = Bot(intents=intents)
bot.remove_command('help')

@bot.command()
async def ping(ctx):
if ctx.channel.name == cname:
await ctx.reply('Pong!')

@bot.command()
async def help(ctx):
if ctx.channel.name == cname:
embed=nextcord.Embed(title="Steam-Workshop-Monitor Help", url="https://github.com/UrekD/Steam-Workshop-Monitor", color=0xff0000)
embed.set_author(name="Steam-Workshop-Monitor Help", url="https://github.com/UrekD/Steam-Workshop-Monitor", icon_url="https://icons.iconarchive.com/icons/icons8/windows-8/512/Logos-Steam-icon.png")
embed.set_thumbnail(url="https://icons.iconarchive.com/icons/icons8/windows-8/512/Logos-Steam-icon.png")
embed.add_field(name="$ping", value="Pong!", inline=False)
embed.add_field(name="$list", value="Lists all mods from current config.", inline=False)
embed.add_field(name="$remove mod#time", value="Removes specified mod from current configuration ex. $remove 1439779114#1531480087 ", inline=False)
embed.add_field(name="$add modid#000", value="Adds a mod to config in format modid#000, ex. $add 1439779114#000 ", inline=False)
embed.add_field(name="$clear", value="Removes all mods from the config.", inline=False)
embed.add_field(name="$refill workshopid", value="Overwrites the config with the mods in workshop collection. ex, $refill 1332156191", inline=False)
await ctx.send(embed=embed)
@bot.slash_command(name="help", description="help")
async def help(interaction: nextcord.Interaction):
embed=nextcord.Embed(title="Steam-Workshop-Monitor Help", url="https://github.com/UrekD/Steam-Workshop-Monitor", color=0xff0000)
embed.set_author(name="Steam-Workshop-Monitor Help", url="https://github.com/UrekD/Steam-Workshop-Monitor", icon_url="https://icons.iconarchive.com/icons/icons8/windows-8/512/Logos-Steam-icon.png")
embed.set_thumbnail(url="https://icons.iconarchive.com/icons/icons8/windows-8/512/Logos-Steam-icon.png")
embed.add_field(name="/ping", value="Pong!", inline=False)
embed.add_field(name="/list", value="Lists all mods from current config.", inline=False)
embed.add_field(name="/remove mod#time", value="Removes specified mod from current configuration ex. /remove 1439779114#1531480087 ", inline=False)
embed.add_field(name="/add modid#000", value="Adds a mod to config in format modid#000, ex. /add 1439779114#000 ", inline=False)
embed.add_field(name="/clear", value="Removes all mods from the config.", inline=False)
embed.add_field(name="/fcheck", value="Force a check cycle indepentent of the interval.Force a check cycle indepentent of the interval", inline=False)
embed.add_field(name="/refill workshopid", value="Overwrites the config with the mods in workshop collection. ex, /refill 1332156191", inline=False)
await interaction.response.send_message(embed=embed)

@bot.command()
async def list(ctx):
if ctx.channel.name == cname:
if event.is_set() is False:
@bot.slash_command(name="list", description="Lists all mods from current config.")
async def list(interaction: nextcord.Interaction):
if event.is_set() is False:
try:
event.set()
with open('data/config.json', "rb") as infile:
config = json.load(infile)
mods = config["userdata"].get('workshopid')
c=math.ceil(len(mods)/80)
for mod in np.array_split(mods, c):
await ctx.send(mod)
await interaction.response.send_message(mod)
event.clear()
else:
await ctx.send("Wait until update check finishes!")

@bot.command()
async def remove(ctx,arg):
if ctx.channel.name == cname:
if event.is_set() is False:
try:
event.set()
idsx = config["userdata"]['workshopid']
idsx.remove(arg)
config["userdata"]['workshopid'] = idsx
async with aiofiles.open('data/config.json', mode='w') as jsfile:
await jsfile.write(json.dumps(config, indent=4, sort_keys=True))
await ctx.send(f"Removed element {arg}")
except Exception as exc:
await ctx.send(f"Error has accured {exc}")
except:
event.clear()
else:
await ctx.send("Wait until update check finishes!")
await interaction.response.send_message("Error is config empty?")
else:
await interaction.response.send_message("Wait until update check finishes!")

@bot.command()
@commands.check_any(commands.is_owner())
async def say(ctx,arg):
channel = bot.get_channel(where)
await channel.send(f"{arg}")

@bot.command()
async def clear(ctx):
if ctx.channel.name == cname:
if event.is_set() is False:
try:
event.set()
d = []
config["userdata"]['workshopid'] = d
async with aiofiles.open('data/config.json', mode='w') as jsfile:
await jsfile.write(json.dumps(config, indent=4, sort_keys=True))
await ctx.send("Cleared the config")
except Exception as exc:
await ctx.send(f"Error has accured {exc}")
event.clear()
else:
await ctx.send("Wait until update check finishes!")
@bot.slash_command(name="remove", description="Removes specified mod from current configuration ex. /remove 1439779114#1531480087")
async def remove(interaction: nextcord.Interaction, arg: str):
if event.is_set() is False:
try:
event.set()
idsx = config["userdata"]['workshopid']
idsx.remove(arg)
config["userdata"]['workshopid'] = idsx
async with aiofiles.open('data/config.json', mode='w') as jsfile:
await jsfile.write(json.dumps(config, indent=4, sort_keys=True))
await interaction.response.send_message(f"Removed element {arg}")
except Exception as exc:
await interaction.response.send_message(f"Error has accured {exc}")
event.clear()
else:
await interaction.response.send_message("Wait until update check finishes!")

@bot.command()
async def add(ctx,arg):
if ctx.channel.name == cname:
if event.is_set() is False:
try:
event.set()
idsx = config["userdata"]['workshopid']
idsx.append(arg)
config["userdata"]['workshopid'] = idsx
async with aiofiles.open('data/config.json', mode='w') as jsfile:
await jsfile.write(json.dumps(config, indent=4, sort_keys=True))
await ctx.send(f"Added element {arg}")
except Exception as exc:
await ctx.send(f"Error has accured {exc}")
event.clear()
else:
await ctx.send("Wait until update check finishes!")
@bot.slash_command(name="fcheck", description="Force a check cycle indepentent of the interval.Force a check cycle indepentent of the interval.")
async def fcheck(interaction: nextcord.Interaction):
if event.is_set() is False:
try:
event.set()
await interaction.response.send_message("Checking...")
channel = bot.get_channel(where)
await channel.send("Checking for updates!")
await Monitor()
await channel.send("Checking finished sleeping...")
event.clear()
except Exception as exc:
await interaction.response.send_message(f"Error has accured {exc}")
event.clear()
else:
await interaction.response.send_message("Wait until update check finishes!")

@bot.command()
async def refill(ctx,arg):
if ctx.channel.name == cname:
if event.is_set() is False:
try:
event.set()
CollectionToConfig(int(arg))
await ctx.send("Cleared and filled config with collection!")
except Exception as exc:
await ctx.send(f"Error has accured {exc}")
event.clear()
else:
await ctx.send("Wait until update check finishes!")
@bot.slash_command(name="clear", description="Removes all mods from the config.")
async def clear(interaction: nextcord.Interaction):
if event.is_set() is False:
try:
event.set()
d = []
config["userdata"]['workshopid'] = d
async with aiofiles.open('data/config.json', mode='w') as jsfile:
await jsfile.write(json.dumps(config, indent=4, sort_keys=True))
await interaction.response.send_message("Cleared the config")
except Exception as exc:
await interaction.response.send_message(f"Error has accured {exc}")
event.clear()
else:
await interaction.response.send_message("Wait until update check finishes!")

@bot.slash_command(name="add", description="Adds a mod to config in format modid#000, ex. /add 1439779114#000")
async def add(interaction: nextcord.Interaction, arg: str):
if event.is_set() is False:
try:
event.set()
idsx = config["userdata"]['workshopid']
idsx.append(arg)
config["userdata"]['workshopid'] = idsx
async with aiofiles.open('data/config.json', mode='w') as jsfile:
await jsfile.write(json.dumps(config, indent=4, sort_keys=True))
await interaction.response.send_message(f"Added element {arg}")
except Exception as exc:
await interaction.response.send_message(f"Error has accured {exc}")
event.clear()
else:
await interaction.response.send_message("Wait until update check finishes!")

@bot.slash_command(name="refill", description="Overwrites the config with the mods in workshop collection. ex, /refill 1332156191")
async def refill(interaction: nextcord.Interaction, arg: int):
if event.is_set() is False:
try:
event.set()
CollectionToConfig(int(arg))
await interaction.response.send_message("Cleared and filled config with collection!")
channel = bot.get_channel(where)
await channel.send("Checking for updates!")
await Monitor()
await channel.send("Checking finished sleeping...")
event.clear()
except Exception as exc:
await interaction.response.send_message(f"Error has accured {exc}")
event.clear()
else:
await interaction.response.send_message("Wait until update check finishes!")

with open('data/config.json', "rb") as infile:
config = json.load(infile)
Expand All @@ -273,5 +286,5 @@ async def refill(ctx,arg):
print(f"{Fore.MAGENTA}[Fill] Finished")
except:
print(f"{Fore.MAGENTA}[Fill] Error filling collection")

bot.run(TOKEN,reconnect=True)
Binary file modified requirements.txt
Binary file not shown.

0 comments on commit 29568d8

Please sign in to comment.