質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

967閲覧

【Discordpy】読み上げBOTの接続するコマンドを送信したチャンネルだけ読み上げられるようにしたい

kuripasanda

総合スコア5

Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/09/05 10:11

編集2021/09/05 10:19

環境

OS : Windows10
言語 : Discord.py-1.7.3 (Python-3.9.7)

実現したいこと

Discord.pyの読み上げBOTをこの記事のコードを少し改造して作ったんですが、
すべてのチャンネルのチャットが読み上げられてしまいます。

kai.con / kai.join で接続(+読み上げ開始)と設定しているんですが、
このコマンドを実行したチャンネルだけ読み上げるようにするには、どうすればいいでしょうか?

python

1if message.channel.id != チャンネルID : 2 return

このコードで、特定のチャンネルだけ反応させることができるようなので、応用すれば、できると思うんですが、私は、まだ初めて2日ぐらいの素人で.. どのようなコードを組めば、実現できるのかわかる方、教えてほしいですm(__)m

コード

read_bot.py

python

1 from http import client 2import discord 3from discord.ext import commands 4from discord.ext import tasks 5import asyncio 6import os 7import subprocess 8import ffmpeg 9import time 10from voice_generator import creat_WAV 11from voice_generator import remove_custom_emoji 12import shlex 13 14 15TOKEN = 'TOKEN' 16client = discord.Client() 17 18 19# デフォルトのhelpコマンドを変更 20client = commands.Bot(command_prefix='kai.') 21client.remove_command('help') 22 23@client.command() 24async def help(ctx): 25 embed=discord.Embed(title="ヘルプ", description="コマンド一覧", color=0xff1f1f) 26 embed.add_field(name="プレフィックス", value="`kai.` \n", inline=False) 27 embed.add_field(name="読み上げ開始(VC接続)", value="`kai.con / kai.join` \n", inline=True) 28 embed.add_field(name="読み上げ終了(VC切断)", value="`kai.dc / kai.disconnect` \n", inline=True) 29 embed.add_field(name="Google検索", value="`..検索`", inline=True) 30 embed.add_field(name="天気予報(東京)", value="`天気予報`(をチャットの内容に含める) \n", inline=True) 31 await ctx.send(embed=embed) 32 33voice_client = None 34 35 36 37@client.event 38async def on_ready(): 39 print('起動しました') 40 time.sleep(1) 41 print('ログイン完了') 42 print(client.user.name) 43 print(client.user.id) 44 print('------') 45 46 47@client.command() 48async def join(ctx): 49 print('#join') 50 print('#voicechannelを取得') 51 vc = ctx.author.voice.channel 52 print('#voicechannelに接続') 53 await vc.connect() 54 55 56@client.command() 57async def con(ctx): 58 print('#con') 59 print('#voicechannelを取得') 60 vc = ctx.author.voice.channel 61 print('#voicechannelに接続') 62 await vc.connect() 63 64@client.command() 65async def dc(ctx): 66 print('#dc') 67 print('#切断') 68 await ctx.voice_client.disconnect() 69 70@client.command() 71async def disconnect(ctx): 72 print('#disconnect') 73 print('#切断') 74 await ctx.voice_client.disconnect() 75 76@client.command() 77async def register(ctx, arg1, arg2): 78 with open('dic.txt', mode='a') as f: 79 f.write('\n'+ arg1 + ',' + arg2) 80 print('dic.txtに書き込み:''\n'+ arg1 + ',' + arg2) 81 await ctx.send('`' + arg1 + '` を `' + arg2 + '` として登録しました') 82 83 84@client.event 85async def on_message(message): 86 # メッセージ送信者がBotだった場合は無視する 87 if message.author.bot: 88 pass 89 90 91 msgclient = message.guild.voice_client 92 print(msgclient) 93 if message.content.startswith('kai.'): 94 pass 95 if message.content.startswith('kai2.'): 96 pass 97 98 else: 99 100 if message.channel.id == '868779813056430110' : 101 if message.guild.voice_client: 102 103 remove_custom_emoji(message.content) 104 creat_WAV(message.content) 105 source = discord.FFmpegPCMAudio("output.wav") 106 message.guild.voice_client.play(source) 107 else: 108 pass 109 await client.process_commands(message) 110 111 112client.run(TOKEN) 113

voice_generator.py

python

1import subprocess 2import re 3from typing import Text 4import shlex 5 6 7# remove_custom_emoji 8# 絵文字IDは読み上げない 9def remove_custom_emoji(text): 10 pattern = r'<:[a-zA-Z0-9_]+:[0-9]+>' # カスタム絵文字のパターン 11 return re.sub(pattern,'',text) # 置換処理 12 13# urlAbb 14# URLなら省略 15def urlAbb(text): 16 pattern = "https?://[\w/:%#$&?()~.=+\-]+" 17 return re.sub(pattern,'URL省略',text) # 置換処理 18 19# remove_custom_emoji 20# kaiのコマンドは読み上げない 21def remove_command(text): 22 pattern = 'kai' # カスタム絵文字のパターン 23 return re.sub(pattern,'',text) # 置換処理 24 25 26 27encode_code = 'utf-8' 28 29def creat_WAV(text): 30 #message.contentをテキストファイルに書き込み 31 input_file = 'input.txt' 32 text = remove_custom_emoji(text) # 絵文字IDは読み上げない 33 text = urlAbb(text) # URLなら省略 34 35 with open(input_file,'w',encoding=encode_code) as file: 36 file.write(text) 37 38 #辞書のPath 39 x = 'dic' 40 41 #ボイスファイルのPath 42 m = 'mei/mei_happy.htsvoice' 43 44 #発声のスピード 45 r = '1.0' 46 47 #出力ファイル名 and Path 48 ow = 'output.wav' 49 50 command = 'open_jtalk -x '+x+' -m '+m+' -r '+r+' -ow '+ow+' '+input_file 51 52 args= {'x':x, 'm':m, 'r':r, 'ow':ow, 'input_file':input_file} 53 54 cmd = command.format(**args) 55 subprocess.run(shlex.split(cmd)) 56 print(cmd) 57 58 subprocess.run(shlex.split(cmd)) 59 return True 60 61if __name__ == '__main__': 62 creat_WAV('テスト')

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kuripasanda

2021/09/05 14:45

マルチポストが禁止だと気づきませんでしたm(__)m 教えていただきありがとうございます!m(__)m 以後気お付けます
guest

回答1

0

自己解決

configに、下記のような感じで保存することで、このサーバーではこのチャンネルを読み上げる、ということができました。
[DATA]
{serverid} = {channelid}

接続の時
configを編集
VCに接続

読み上げの時(メッセージが送信されたとき)
チャットの読み上げファイル作成
ifで、送信されたチャンネルID と 保存されているチャンネルIDを比較
(あっていたら)
作成されたファイルをVCで再生

投稿2021/09/07 10:29

kuripasanda

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問