Created
December 28, 2020 23:56
-
-
Save lynn/39a6c283b3fda9366801dcb896d81fff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Discord bot for playing ABC notation music. | |
# | |
# ### Installation | |
# - Get the dependencies: abc2midi, timidity, ffmpeg, and lame. | |
# - `pip install discord.py` | |
# - `ACCORD_TOKEN=(your bot token) python3 accord.py` | |
# | |
# ### Usage | |
# In any channel where the bot is, say: $play (abc notation) | |
# The input may be multiple lines long, and may be in a ```code block```. | |
# The bot will respond by uploading an mp3 file. | |
import discord | |
import os | |
client = discord.Client() | |
@client.event | |
async def on_ready(): | |
print('logged in as {0.user}'.format(client)) | |
@client.event | |
async def on_message(message): | |
if message.author == client.user: | |
return | |
if message.content.startswith('$play'): | |
lines = message.content[5:].strip(' \n\r\t`').split('\n') | |
if not any(l.startswith('M:') for l in lines): lines = ['M:none'] + lines | |
if not any(l.startswith('K:') for l in lines): lines = ['K:C major'] + lines | |
if not any(l.startswith('X:') for l in lines): lines = ['X:1'] + lines | |
with open('input.abc', 'w') as f: | |
f.write('\n'.join(lines) + '\n') | |
os.system('rm -f output.mid && abc2midi input.abc -o output.mid') | |
if not os.path.isfile('output.mid'): | |
await message.channel.send("Couldn't convert to MIDI. Check your syntax!") | |
return | |
if 0 != os.system('timidity output.mid -EFreverb=0 -Ow -o - | ffmpeg -y -hide_banner -loglevel panic -i - -acodec libmp3lame -ab 256k output.mp3'): | |
await message.channel.send("Couldn't convert to mp3. Maybe try again.") | |
return | |
await message.channel.send(file=discord.File('output.mp3')) | |
client.run(os.environ['ACCORD_TOKEN']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment