Skip to content

Instantly share code, notes, and snippets.

@khlizard
Last active May 18, 2022 11:13
Show Gist options
  • Save khlizard/c92770a2c315d471560687b23d566a81 to your computer and use it in GitHub Desktop.
Save khlizard/c92770a2c315d471560687b23d566a81 to your computer and use it in GitHub Desktop.
"""minimum_rtsp_bot.py
- 指定したRTSPストリームをプレイするだけのbotです
- DiscordのどこかのVoiceChに入った状態で '!playstream' とすると、そのchに入ってきてストリームを流し始めます
- 再生中に '!stopstream' とすると、再生を停止してvoice chからも抜けます
- 再生中に '!resyncstream' で再接続します
- 必要なパッケージは(多分)discord.py・discord.py[voice]・pynacl、あとffmpegがパスの通ってるところに必要だと思います
- botのTOKENの取得は https://qiita.com/1ntegrale9/items/cb285053f2fa5d0cccdf を参考にして下さい
"""
import os
import discord
from discord.channel import VoiceChannel
from discord.player import FFmpegPCMAudio
TOKEN = os.environ['DISCORD_BOT_TOKEN']
streamURL = 'rtsp://example.com/live/foobar9823'
client = discord.Client()
voiceCh: VoiceChannel = None
@client.event
async def on_ready():
print('login discord server.')
@client.event
async def on_message(message):
global voiceCh
if message.author.bot:
return
if message.content == '!playstream':
if voiceCh is None and message.author.voice is not None:
voiceCh = await VoiceChannel.connect(message.author.voice.channel)
voiceCh.play(FFmpegPCMAudio(streamURL))
await message.channel.send('Streamを再生します')
return
if message.content == '!resyncstream':
if voiceCh is not None:
voiceCh.stop()
voiceCh.play(FFmpegPCMAudio(streamURL))
await message.channel.send('Resyncします')
return
if message.content == '!stopstream':
if voiceCh is not None:
voiceCh.stop()
await voiceCh.disconnect()
voiceCh = None
await message.channel.send('再生を終了します')
return
client.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment