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

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

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

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

Python 3.x

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

Q&A

解決済

1回答

311閲覧

discord.pyのグローバルチェックがうまく動作しない

tamat123

総合スコア10

Discord

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

Python 3.x

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

0グッド

0クリップ

投稿2022/05/08 12:53

編集2022/05/08 13:22

discordbotの管理者のみ実行できるコマンドをまとめたクラスでグローバルチェックがうまくいかず、@commands.is_owner()を使っていました。うまく動作しない理由を教えてください。
以下がコードの一部です

class SuperUser(commands.Cog, Everyone): # 現在うまく動作しない部分(オーナーのidの中には個人のuser_idが入る(int)) @commands.check async def is_owner(self, ctx): return ctx.author.id == オーナーのid # 再接続 @commands.command() @commands.is_owner() async def db_conn(self, ctx): self.conn = await asyncpg.connect(self.dsn) await ctx.send("データベースに接続します") # 入力クエリ実行 @commands.command() @commands.is_owner() async def db_query1(self, ctx, *, query): await ctx.send(query) await self.conn.execute(query) # 出力クエリ実行 @commands.command() @commands.is_owner() async def db_query2(self, ctx, *, query): txt = "" rows = await self.conn.fetch(query) for row in rows: txt += f"{row}\n" await ctx.send(txt)

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

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

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

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

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

guest

回答1

0

自己解決

2022/05/10 19:25解決

解決方法

cog_checkメソッドを使用

ソースコード

python

1class Administrator(commands.Cog, Every): 2 def __init__(self, bot, dsn): 3 super().__init__(bot, dsn) 4 5 def cog_check(self, ctx): 6 return ctx.author.id == オーナーのid 7 8 @commands.command() 9 async def db_conn(self, ctx): 10 """データベースに再接続する""" 11 self.conn = await asyncpg.connect(self.dsn) 12 await ctx.send("データベースに接続します") 13 14 @commands.command() 15 async def db_close(self, ctx): 16 """データベースから切断する""" 17 await ctx.send("データベースから切断します") 18 await self.conn.close() 19 20 @commands.command() 21 async def db_query1(self, ctx, *, query): 22 """クエリを実行する""" 23 await ctx.send(query) 24 await self.conn.execute(query) 25 26 @commands.command() 27 async def db_query2(self, ctx, *, query): 28 """クエリを実行し結果を出力する""" 29 txt = "" 30 rows = await self.conn.fetch(query) 31 for row in rows: 32 txt += f"{row}\n" 33 await ctx.send(txt) 34 35 @commands.command() 36 async def reset(self, ctx): 37 """国力をリセットする""" 38 await ctx.send("国力を初期化します") 39 await self.conn.execute("UPDATE country SET (country_power, today_vote) = (0, FALSE)") 40 41 @commands.command() 42 async def add_country(self, ctx, country, flag, player): 43 """国家を追加する""" 44 await ctx.send(f"国家を追加しました。\n国名:{country}\n国旗:{flag}\nプレイヤーid:{player}") 45 await self.conn.execute("INSERT INTO country (country_name, flag, user_id) VALUES (($1), ($2), ($3))", country, 46 flag, player)

投稿2022/05/10 10:28

tamat123

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問