The chat reading bot used in Discord has temporarily become unusable, so I made it on a temporary basis. I made it by rewriting a part of the code referring to Creating a bot that reads out Discord chat.
I did the bot creation and environment construction while looking at the following.
-[Discord bot creation tutorial](https://ikayome.hateblo.jp/entry/2019/07/03/Discord_bot%E4%BD%9C%E6%88%90%E3%83%81%E3%83% A5% E3% 83% BC% E3% 83% 88% E3% 83% AA% E3% 82% A2% E3% 83% AB) -Speech synthesis on Windows Open JTalk -Install ffmpeg on windows
ffmpeg
puts the unzipped files in C: \ open_jtalk \ bin
.
Don't forget the Path
setting ...
Please rewrite the token of each bot in the part of the bottom line, which is the token copied when the bot was created.
read_bot.py
import discord
from discord.ext import commands
import asyncio
import os
import subprocess
import ffmpeg
from voice_generator import creat_WAV
client = commands.Bot(command_prefix='.')
voice_client = None
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.command()
async def join(ctx):
print('#Get voicechannel')
vc = ctx.author.voice.channel
print('#Connect to voice channel')
await vc.connect()
@client.command()
async def bye(ctx):
print('#Disconnect')
await ctx.voice_client.disconnect()
@client.event
async def on_message(message):
msgclient = message.guild.voice_client
if message.content.startswith('.'):
pass
else:
if message.guild.voice_client:
print(message.content)
creat_WAV(message.content)
source = discord.FFmpegPCMAudio("output.wav")
message.guild.voice_client.play(source)
else:
pass
await client.process_commands(message)
client.run("Token copied when creating the bot")
There are various comments out in the voice file path, but if you download the Source code
of Sample Script
in MMD Agent You can change it to a female voice.
(Since there is a htsvoice file
in the Voice / mei
directory of the unzipped file, place the mei directory
in the same hierarchy as the source code.)
voice_generator.py
import subprocess
import re
# remove_custom_emoji
#Emoji ID is not read aloud
def remove_custom_emoji(text):
pattern = r'<:[a-zA-Z0-9_]+:[0-9]+>' #Custom emoji pattern
return re.sub(pattern,'',text) #Replacement process
# urlAbb
#Omitted for URL
def urlAbb(text):
pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
return re.sub(pattern,'The URL is omitted!',text) #Replacement process
# creat_WAV
# message.Write content to a text file
def creat_WAV(inputText):
# message.Write content to a text file
inputText = remove_custom_emoji(inputText) #Emoji ID is not read aloud
inputText = urlAbb(inputText) #Omitted for URL
input_file = 'input.txt'
with open(input_file,'w',encoding='shift_jis') as file:
file.write(inputText)
command = 'C:/open_jtalk/bin/open_jtalk -x {x} -m {m} -r {r} -ow {ow} {input_file}'
#Dictionary Path
x = 'C:/open_jtalk/bin/dic'
#Voice file Path
m = 'C:/open_jtalk/bin/nitech_jp_atr503_m001.htsvoice'
#m = 'C:/open_jtalk/bin/mei/mei_sad.htsvoice'
#m = 'C:/open_jtalk/bin/mei/mei_angry.htsvoice'
#m = 'C:/open_jtalk/bin/mei/mei_bashful.htsvoice'
#m = 'C:/open_jtalk/bin/mei/mei_happy.htsvoice'
#m = 'C:/open_jtalk/bin/mei/mei_normal.htsvoice'
#Vocalization speed
r = '1.0'
#Output file name and Path
ow = 'output.wav'
args= {'x':x, 'm':m, 'r':r, 'ow':ow, 'input_file':input_file}
cmd= command.format(**args)
print(cmd)
subprocess.run(cmd)
return True
if __name__ == '__main__':
creat_WAV('test')
The composition is like this.
There was a description up to the point of inviting the bot, but it was not written about how to use it, so I will write it appropriately.
Then the bot will log in
First of all, I enter the voicing.
(This bot basically comes in at the person who called the voicing.)
The command to call is .join
If you chat appropriately, it will be read. It doesn't read emojis or custom emojis ... Also, I try to omit the URL.
The command to leave the bot from the voice channel is .bye
At the command prompt, use Ctrl + C
or close the window.
I feel that even simple English can be read in alphabets, so I'd like to add a dictionary registration function.
Well it will be done soon. maybe
Recommended Posts