Add inventory DB cog and fix music cog

This commit is contained in:
rzmk 2021-07-27 13:49:48 -04:00
parent 743943effd
commit 0c21b2ee01
6 changed files with 137 additions and 33 deletions

View file

@ -9,6 +9,7 @@ class PingCog(commands.Cog):
# Commands
@commands.command(aliases=['latency'])
async def ping(self, ctx):
"""Returns the bot client latency"""
await ctx.send(f'Pong! {round(self.client.latency * 1000)}ms')
def setup(client):

View file

@ -0,0 +1,61 @@
import os
import discord
import DiscordUtils
import asyncio
from google.cloud import firestore
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
class InventoryCog(commands.Cog):
def __init__(self, client):
self.client = client
# Commands
# Get a single item from inventory
@commands.command(aliases=['inv'])
@commands.is_owner()
async def inventory(self, ctx, arg1):
"""Get a single item from the inventory database"""
# Make single input embed
def single_embed(item, id):
embed = discord.Embed(title=item["item_name"], description=id)
if item["item_type"] != "": embed.add_field(name="Item Type", value=item["item_type"], inline=True)
if item["color"] != "": embed.add_field(name="Color", value=item["color"], inline=True)
if item["brand"] != "": embed.add_field(name="Brand", value=item["brand"], inline=True)
if item["model"] != "": embed.add_field(name="Model", value=item["model"], inline=True)
if item["size"] != "": embed.add_field(name="Size", value=item["size"], inline=True)
if item["quantity"] != "": embed.add_field(name="Quantity", value=item["quantity"], inline=True)
if item["box"] != "": embed.add_field(name="Box", value=item["box"], inline=True)
return embed
# Connect to Firestore DB inventory collection and get the item arg1 as doc
if "INVENTORY_PROJECT_ID" in os.environ and "GOOGLE_APPLICATION_CREDENTIALS" in os.environ:
try:
db = firestore.AsyncClient(project=os.environ.get("INVENTORY_PROJECT_ID"))
inventory_ref = db.collection("inventory").document(arg1)
doc = await inventory_ref.get()
if doc.exists:
await ctx.send(embed=single_embed(doc.to_dict(), doc.id))
else:
await ctx.send("That ID doesn't exist!")
except:
await ctx.send("The DB connection is not working.")
elif "INVENTORY_PROJECT_ID" not in os.environ and "GOOGLE_APPLICATION_CREDENTIALS" in os.environ:
await ctx.send("Inventory Project ID not found!")
elif "GOOGLE_APPLICATION_CREDENTIALS" not in os.environ:
await ctx.send("GAPP Credentials not found!")
@inventory.error
async def inventory_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Missing required argument! (e.g. d!inventory __10000__)")
if isinstance(error, commands.NotOwner):
await ctx.send("You do not have permissions to run this command.")
def setup(client):
client.add_cog(InventoryCog(client))

View file

@ -9,17 +9,18 @@ class ModerationCog(commands.Cog):
# Commands
@commands.command()
@commands.has_permissions(kick_members = True)
async def kick(self, ctx, member : discord.Member, *, reason="No reason provided."):
async def kick(self, ctx, member : commands.MemberConverter, *, reason="No reason provided."):
"""Kick a user from the server"""
try:
await member.kick(reason=reason)
await ctx.send(member.mention + " has been kicked.")
except:
await ctx.send(f"Unable to kick {member.mention}.\nIs {member.mention} at the same role level or higher than {self.client.user.name}?")
@commands.command()
@commands.has_permissions(ban_members = True)
async def ban(self, ctx, member : discord.Member, *, reason="No reason provided."):
async def ban(self, ctx, member : commands.MemberConverter, *, reason="No reason provided."):
"""Ban a user from the server"""
try:
await member.ban(reason=reason)
await ctx.send(member.mention + " has been banned.")

View file

@ -53,21 +53,30 @@ class MusicCog(commands.Cog):
# Commands
@commands.command()
async def play(self, ctx, *, url):
"""Streams from a url (same as yt, but doesn't predownload)"""
async def play(self, ctx, url):
"""Streams from a url"""
try:
voiceChannel = discord.utils.get(ctx.guild.voice_channels, guild=ctx.guild)
await voiceChannel.connect()
channel = ctx.author.voice.channel
await channel.connect()
except:
pass
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.client.loop, stream=True)
ctx.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None)
await ctx.send(f'Now playing: {player.title}')
@commands.command()
@play.error
async def play_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
voice = ctx.voice_client
if voice != None and voice.is_paused():
voice.resume()
else:
await ctx.send("Missing URL!")
@commands.command(aliases=['disconnect, dc'])
async def leave(self, ctx):
"""Disconnects bot from the voice channel"""
voice = ctx.voice_client
if voice.is_connected():
await voice.disconnect()
@ -86,6 +95,7 @@ class MusicCog(commands.Cog):
@commands.command()
async def pause(self, ctx):
"""Pauses audio"""
voice = ctx.voice_client
if voice.is_playing():
voice.pause()
@ -94,6 +104,7 @@ class MusicCog(commands.Cog):
@commands.command()
async def resume(self, ctx):
"""Resumes currently paused audio"""
voice = ctx.voice_client
if voice.is_paused():
voice.resume()
@ -109,20 +120,15 @@ class MusicCog(commands.Cog):
@commands.command(aliases=['join'])
async def connect(self, ctx):
"""Connects bot to currently connected voice channel"""
channel = ctx.author.voice.channel
try:
await channel.connect()
except:
voice = ctx.voice_client
if voice.is_connected():
if voice.is_connected() and voice.channel != channel:
await voice.disconnect()
await channel.connect()
else:
await channel.connect()
'''@commands.command(aliases=['dc','leave'])
async def disconnect(self, ctx):
await ctx.voice_client.disconnect()'''
def setup(client):
client.add_cog(MusicCog(client))