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

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

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

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

Q&A

解決済

3回答

1828閲覧

Discord.py2.0.0で、prefixが反応しないので反応するようにしたい

matsuochinyu

総合スコア57

Discord

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

0グッド

0クリップ

投稿2022/04/07 16:39

前提

ここに質問の内容を詳しく書いてください。
slashコマンドや、button機能を使いたく、バージョンを2.0.0にしました。
その中で、Botを起動し動くか確認したとこ、コマンドどころか prefixすら反応しんくなりました。
解決方法を教えてください。

実現したいこと

・コマンドに反応するようにする。
・prefixが反応するようにする。

発生している問題・エラーメッセージ

表示されません。

該当のソースコード

python

1import asyncio 2import datetime 3import os 4import traceback 5 6import discord 7from discord.ext import commands 8from dotenv import load_dotenv 9 10from help_command import HelpCommand 11 12now = datetime.datetime.now() 13load_dotenv() 14TOKEN = os.environ["TOKEN"] 15NOTICE = チャンネルID 16 17 18class MatsuochinyuBot(commands.Bot): 19 def __init__(self, command_prefix, intents, help_command): 20 super().__init__(command_prefix=command_prefix, intents=intents, help_command=help_command) 21 self.success = 0 22 self.failure = 0 23 self.filename = [file.replace(".py", "") for file in os.listdir("./cogs") if file.endswith(".py")] 24 25 async def load_extensions(self): 26 print("Cogを読み込みます") 27 for filename in self.filename: 28 print("-" * 10) 29 print(filename) 30 try: 31 await self.load_extension(f"cogs.{filename}") 32 print(f"Read success:{filename}") 33 self.success += 1 34 except Exception as e: 35 print(e) 36 print(f"Read failure:{filename}") 37 self.failure += 1 38 print("-" * 10) 39 print(f"成功:{self.success} 失敗:{self.failure}\n") 40 41 async def on_ready(self): 42 embed = discord.Embed(title="Info", description="", color=0x00FF00) 43 embed.add_field(name="Cogの読み込み", value=f"成功:{self.success} 失敗:{self.failure}") 44 await self.get_channel(NOTICE).send(embed=embed) 45 46 with open("cogs/reload.txt", "r", encoding="utf-8") as f: 47 read_file = f.read() 48 if read_file == "": 49 pass 50 else: 51 await self.get_channel(int(read_file)).send( 52 "``loading...``", delete_after=3 53 ) 54 embed = discord.Embed( 55 title="___リロード完了通知___", description="使えるようになりました。", color=0x00FF00 56 ) 57 await self.get_channel(int(read_file)).send(embed=embed, delete_after=3) 58 with open("cogs/reload.txt", "w", encoding="utf-8") as f: 59 f.write("") 60 61 async def on_command_error(self, ctx, error): 62 """すべてのコマンドで発生したエラーを拾う""" 63 if isinstance(error, commands.CommandInvokeError): 64 orig_error = getattr(error, "original", error) 65 error_msg = "".join( 66 traceback.TracebackException.from_exception(orig_error).format() 67 ) 68 error_message = f"```{error_msg}```" 69 70 ch = ctx.guild.get_channel(NOTICE) 71 embed = discord.Embed( 72 title="エラーログ", description=error_message, color=0xF04747 73 ) 74 embed.set_footer( 75 text=f"Time:{now:%Y-%m-%d %H:%M:%S}\nGuild:{ctx.guild}\nChannel:{ctx.channel}\nUser:{ctx.author.display_name} " 76 ) 77 await ch.send(embed=embed) 78 79 80# Botの設定 81intent = discord.Intents.default() 82intent.typing = False 83intent.members = True 84intent.messages = True 85bot = MatsuochinyuBot( 86 command_prefix=commands.when_mentioned_or("m:"), 87 intents=intent, 88 help_command=HelpCommand(), 89) 90 91 92# ログイン処理 93async def main(): 94 async with bot: 95 await bot.load_extensions() 96 await bot.start(TOKEN) 97 98 99if __name__ == "__main__": 100 print("ログイン中...\n") 101 print(f"Information's:{datetime.datetime.now()}") 102 print("-" * 40) 103 print(f"Discord.py Version:{discord.__version__}") 104 print("-" * 40) 105 print("ログイン完了\n") 106 asyncio.run(main()) 107

試したこと

色々なネット記事を参考にして試みてみましたが、動きませんでした。

補足情報(FW/ツールのバージョンなど)

discord.py:Version: 2.0.0a4058+gabeea73
python:3.9.12
mac OS:catalina 10.15.7

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

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

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

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

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

guest

回答3

0

おそらくintentsの問題ですね。
discord.py 2.0だと、message_contentのインテントがデフォルトではFalseに指定されているためコマンドが反応しません。
気をつけてほしいのは、このインテントはintents.messagesではなくintents.message_contentになります。Intent指定のところでintents.message_content = Trueとすれば解決するのではないでしょうか。

投稿2022/05/18 14:51

yaakiyu

総合スコア124

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

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

0

自己解決

解決 とは言い難いのですが、最初から書き直しました。
答えてくださった方々へありがとうございます。

投稿2022/05/26 05:10

matsuochinyu

総合スコア57

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

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

0

cogファイルの使い方については こちら が参考になります。

py

1 2INITIAL_EXTENTIONS = [ 3 "ファイル名", 4 "ファイル名" 5] 6 7 8class MatsuochinyuBot(commands.Bot): 9 def __init__( 10 self, 11 command_prefix, 12 intents, help_command 13 ): 14 super().__init__( 15 command_prefix=command_prefix, 16 intents=intents, 17 help_command=help_command 18 ) 19 self.success = 0 20 self.failure = 0 21 self.filename = [file.replace(".py", "") for file in os.listdir("./cogs") if file.endswith(".py")] 22 """ 23 async def load_extensions(self): 24 print("Cogを読み込みます") 25 for filename in self.filename: 26 print("-" * 10) 27 print(filename) 28 try: 29 self.load_extension(f"cogs.{filename}") 30 print(f"Read success:{filename}") 31 self.success += 1 32 except Exception as e: 33 print(e) 34 print(f"Read failure:{filename}") 35 self.failure += 1 36 print("-" * 10) 37 print(f"成功:{self.success} 失敗:{self.failure}\n") 38 """ 39 40 async def on_ready(self): 41 for cog in INITIAL_EXTENTIONS: 42 try: 43 self.load_extension("cogs.{}".format(cog)) 44 self.success += 1 45 46 except errors.ExtensionNotFound as e: 47 print(traceback.TracebackException.from_exception(e)) 48 self.failure += 1 49 50 except: 51 pass 52 53 embed = discord.Embed(title="Info", description="", color=0x00FF00) 54 embed.add_field(name="Cogの読み込み", value=f"成功:{self.success} 失敗:{self.failure}") 55 56 await self.get_channel(NOTICE).send(embed=embed) 57 58 with open("cogs/reload.txt", "r", encoding="utf-8") as f: 59 read_file = f.read() 60 if read_file == "": 61 pass 62 else: 63 await self.get_channel(int(read_file)).send( 64 "``loading...``", delete_after=3 65 ) 66 embed = discord.Embed( 67 title="___リロード完了通知___", description="使えるようになりました。", color=0x00FF00 68 ) 69 await self.get_channel(int(read_file)).send(embed=embed, delete_after=3) 70 with open("cogs/reload.txt", "w", encoding="utf-8") as f: 71 f.write("") 72 73 async def on_command_error(self, ctx, error): 74 """すべてのコマンドで発生したエラーを拾う""" 75 if isinstance(error, commands.CommandInvokeError): 76 orig_error = getattr(error, "original", error) 77 error_msg = "".join( 78 traceback.TracebackException.from_exception(orig_error).format() 79 ) 80 error_message = f"```{error_msg}```" 81 82 ch = ctx.guild.get_channel(NOTICE) 83 embed = discord.Embed( 84 title="エラーログ", description=error_message, color=0xF04747 85 ) 86 embed.set_footer( 87 text=f"Time:{now:%Y-%m-%d %H:%M:%S}\nGuild:{ctx.guild}\nChannel:{ctx.channel}\nUser:{ctx.author.display_name} " 88 ) 89 await ch.send(embed=embed) 90 91""" 92# Botの設定 93intent = discord.Intents.default() 94intent.typing = False 95intent.members = True 96intent.messages = True 97bot = MatsuochinyuBot( 98 command_prefix=commands.when_mentioned_or("m:"), 99 intents=intent, 100 help_command=None 101) 102 103 104# ログイン処理 105async def main(): 106 async with bot: 107 await bot.load_extensions() 108 await bot.start(TOKEN) 109""" 110 111 112if __name__ == "__main__": 113 print("ログイン中...\n") 114 print(f"Information's:{datetime.datetime.now()}") 115 print("-" * 40) 116 print(f"Discord.py Version:{discord.__version__}") 117 print("-" * 40) 118 print("ログイン完了\n") 119 intents = discord.Intents.default() 120 intents.typing = True 121 intents.members = True 122 intents.messages = True 123 bot: commands.Bot = MatsuochinyuBot( 124 command_prefix="m:", 125 intents=intents, 126 help_command=HelpCommand() 127 ) 128 bot.run(TOKEN)

import分より下です。これでどうでしょうか。

投稿2022/04/22 04:42

pecop

総合スコア409

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

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

matsuochinyu

2022/04/23 10:35 編集

回答ありがとうございます。返信が遅れ申し訳ありません。 またエラーが出まして、少し質問したいところがあるのですが、 ``` async def on_ready(self): for cog in INITIAL_EXTENTIONS: try: self.load_extension("cogs.{}".format(cog)) self.success += 1 except errors.ExtensionNotFound as e: print(traceback.TracebackException.from_exception(e)) self.failure += 1 except: pass ``` こちらの``errors``はどこから出てきたのでしょうか? PS:すみません。コメントでの`をどう書けばいいのか分からず、見ずらいと思います。
pecop

2022/05/17 07:48

申し訳ありません。返信遅くなりました。 `import discord.errors` をしてください。
matsuochinyu

2022/05/18 14:52

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問