您好,我试图通过网络界面将命令发送到混乱的地方,但没有任何@ bot.event定义。 唯一的问题是我没有得到引用的ctx对象,我无法使用该对象来找到漫游器所在的通道等。
没有机器人事件,有没有办法使这项工作可行?我看了其他一些代码,但是没有什么比我想要达到的要高。
我通过从on_ready事件中获取Guild_ID作为全局变量来创建工作区。但是似乎可以找到该机器人的ctx。任何想法或对其他工作的引用都非常欢迎,因此我可以继续这个项目:)
bot.py:
import discord
from datetime import datetime
from discord.ext import commands
from discord.utils import get
import sys
import asyncio
import tornado.web
import tornado.websocket
import os
import socket
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
soundFolderLocation = os.path.dirname(os.path.abspath(__file__)) + "\\sounds\\"
bot = commands.Bot(command_prefix = '.')
GUILD_ID = ''
BOT_TOKEN = ''
@bot.event
async def on_ready():
print('Bot is ready.')
#channel = list(bot.get_all_channels())
#length = len(channel)
#for i in range(length):
#print(channel[i].name + ' id:' + str(channel[i].id))
print('logged in')
print('bot name : '+str(bot.user.name))
print('bot ID : '+str(bot.user.id))
print('discord version : '+str(discord.__version__))
print('GuildID : '+str(bot.guilds[0]))
GUILD_ID = bot.guilds[0]
class BaseHandler(tornado.web.RequestHandler):
# you can pass whatever you want
def initialize(self, bot, db):
self.bot = bot
async def prepare(self):
await self.bot.wait_until_ready()
class SoundboardHandler(tornado.web.RequestHandler):
def tornado_sounds(self):
serverFolder = soundFolderLocation + "\\"
soundList = []
for root, dirs, files in os.walk(serverFolder):
for filename in files:
sound = filename.replace(".mp3", "")
soundList.append(sound)
soundsofawsome = soundList
return soundsofawsome
def get(self):
items = self.tornado_sounds()
self.render("bot_template.html", title="Thanos", items=items)
class tornado_playsound(tornado.web.RequestHandler):
async def post(self):
global GUILD_ID
global bot
serverFolder = soundFolderLocation + "\\"
voice = get(bot.voice_clients, guild=GUILD_ID)
for member in bot.get_all_members():
if 'Thanos' in str(member):
user = member
print("Member has been found:" +str(member))
#### everything stops working here!!
channel = member.voice.channel
playthissound = serverFolder + str(self.request.body) + ".mp3"
if voice and voice.is_connected():
if ctx.message.author.voice.channel != voice.channel:
await voice.move_to(channel)
else:
voice = await channel.connect()
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.5
voice.play(
discord.FFmpegPCMAudio(playthissound),
after=lambda e: print(
f"[SOUND] [{ctx.guild.id}] [{ctx.guild}] [{ctx.author.name}] [STOP] {self.request.body}.mp3"
),
)
# what is passed to inilialize
extra = {
"my_bot_instance": bot,
"my_database": "I'm a database",
}
routes = [
(r"/", SoundboardHandler),
(r"/playsound", tornado_playsound),
]
settings = {
"cookie_secret": "1234",
}
app = tornado.web.Application(routes, settings)
app.listen(4242)
myIP = socket.gethostbyname(socket.gethostname())
print('*** Websocket Server Started at %s***' % myIP)
loop = asyncio.get_event_loop()
asyncio.ensure_future(bot.start(BOT_TOKEN), loop=loop)
loop.run_forever()
模板:
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).off('click').on('click', 'body', function(event){
if ( $( event.target ).is( ":button" ) ) {
respons = event.target.id
$.ajax({
type: 'POST',
url: "/playsound",
contentType: 'text/plain',
data: respons,
success: function (data) {
$("#debugfield").text("clicked something and it worked" + respons);
},
error: function (error) {
$("#debugfield").text("Error to load API");
}
});
}
});
</script>
<ul id="soundbuttons">
{% for item in items %}
<li>
<button class="button" id="{{escape(item)}}" type="button">{{escape(item)}}</button>
<br/>
</li>
{% end %}
</ul>
<span id="responsefield"></span>
</div>
</body>
</html>