'check queue command in discord movie bot
I've been trying to make a discord bot for movies with plexapi. Everything till now is working even the queue command for it. I am not to get check queued list to show all the programs that have been queued. I will attach the part of that code below and the full code below that. If anyone can help please let me know what i am having problems in.
check queue code:
#queue command
@bot.command()
async def check_queue(ctx):
if queues[ctx] != []:
bot = queues[ctx].pop(0)
bots[ctx] = bot
bot.start()
await ctx.message.reply("Queued list:")
whole code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=".p ", help_command=None)
#bot connected
@bot.event
async def on_ready():
print("Bot connected as:",bot.user.name)
#help command
@bot.command()
async def help(ctx):
await ctx.message.reply(".p search <Moviename> .p play <Moviename> .p queue (to see the queue)")
#queue command
@bot.command()
async def check_queue(ctx):
if queues[ctx] != []:
bot = queues[ctx].pop(0)
bots[ctx] = bot
bot.start()
await ctx.message.reply("Queued list:")
#plex api shit
from plexapi.myplex import MyPlexAccount
import plexapi
account = MyPlexAccount('user', 'pass')
plex = account.resource('Main').connect() # returns a PlexServer instance
print(type(plex))
client = plex.client("Chrome")
queue = None
#search
@bot.command()
async def search(ctx, *query):
q ="""
Search results: (Note when queueing a movie only do the name dont add the year)
"""
for video in plex.library.section('Movies').search(' '.join(query)):
try:
q += '\n' + ('%s (%s)' % (video.title, video.year))
except AttributeError:
pass
await ctx.message.reply(q)
'''
function similarityBetween(s1, s2) {
let longer = s1;
let shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
const longerLength = longer.length;
if (longerLength === 0) {
return 1.0;
}
return (
(longerLength - editDistance(longer, shorter)) /
parseFloat(longerLength)
);
}
'''
def simbet(s1,s2):
longer = s1
shorter = s2
if len(s1) < len(s2):
longer = s2
shorter = s1
longerlength = len(longer)
if longerlength == 0:
return 1.0
return (longerlength - editDistance(longer,shorter))/float(longerlength)
def editDistance(s1,s2):
s1 = s1.lower()
s2 = s2.lower()
lastval = None
costs = [None]*100
for i in range(0,len(s1)):
lastval = i
for j in range(0,len(s2)):
if i == 0:
costs[j] = j
else:
if j > 0:
newval = costs[j-1]
if s1[i-1] != s2[j-1]:
min(costs[j],(min(newval,lastval))) + 1
costs[j-1] = lastval
lastval = newval
if i > 0:
costs[len(s2)] = lastval
return costs[len(s2)]
'''
function editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
const costs = [];
for (let i = 0; i <= s1.length; i++) {
let lastValue = i;
for (let j = 0; j <= s2.length; j++) {
if (i === 0) {
costs[j] = j;
} else {
if (j > 0) {
let newValue = costs[j - 1];
if (s1.charAt(i - 1) !== s2.charAt(j - 1)) {
newValue =
Math.min(
Math.min(newValue, lastValue),
costs[j]
) + 1;
}
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) {
costs[s2.length] = lastValue;
}
}
return costs[s2.length];
}
'''
#play
@bot.command()
async def play(ctx,*item):
global queue
item = ' '.join(item)
movies = plex.library.section('Movies')
try:
def check(m):
return m.author == ctx.message.author and m.channel == ctx.channel
x = movies.search(item)
x.sort(reverse = True, key=lambda a: simbet(a.title,item))
if not x:
return
y = []
for l in x:
y.append(l.title)
if item == x[0].title:
movie = x[0]
else:
o = await ctx.send(f"Did you mean **{x[0].title}**?? yes/no")
try:
msg = await bot.wait_for("message", check=check,timeout=10)
if msg.content.lower() == 'yes':
movie = x[0]
else:
return
except:
await o.reply("Timed out")
return
await ctx.message.reply("Playing `"+movie.title+'`')
if not queue:
queue = plexapi.playqueue.PlayQueue.create(plex,movie)
client.playMedia(queue)
await ctx.message.reply("Playing `"+item+'`')
else:
queue.addItem(movie)
client.refreshPlayQueue(queue)
await ctx.message.reply("Added to queue!")
except plexapi.exceptions.NotFound:
await ctx.message.reply("Movie not found!")
bot.run("xxxx")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
