teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

質問内容から試したことを分離

2021/07/24 06:24

投稿

hoge_fuga
hoge_fuga

スコア22

title CHANGED
@@ -1,1 +1,1 @@
1
- [discord]slashコマンドのguild_ids
1
+ [discord]slashコマンドのを利用側で決める
body CHANGED
@@ -1,18 +1,20 @@
1
1
  ## 問題
2
- `discord-py-slash-command`を用いてslash commandを扱うcogの開発をしています.
2
+ `discord-py-slash-command`を用いてスラッシュコマンドを扱うcogの開発をしています.
3
- サンプルコードを参考にcogを作成したのすが,`guild_ids`をcogのファイルに書かなければけないことが気になりました.
3
+ cogのサンプルコードを見ると,cogで`guild_ids`を指定していました.
4
- そこで,`guild_ids`をcogの利用側(`main.py`)に移そうと考えました.
5
- そのために,下記「プロラム例」のように`hoge`コマンドを定義しました.
4
+ そのためライブラリとしてcogを作成すると,利用側からはギルドコマンドするか,グローバルコマンドにするか選択できせんでした.
6
- しかし実際に`hoge`コマンドを用いると,エラーが発生してしまいました.
7
- `guild_ids`をcogの利用側で定めるにはどうればいいしょうか?
5
+ ようにすれば利用側で`guild_ids`を指定することがきますか?
8
6
 
9
7
  ## 環境
10
8
  * python 3.7
11
9
  * discord.py 1.7.3
12
10
  * discord-py-slash-command 2.3.1
13
11
 
14
- ## プログラム例
12
+ ## 試したこと
15
13
 
14
+ 通常コマンドは`ping`のようにデコレータで登録されます.
15
+ そこで,`hoge`のように`@`を用いずに直接デコレータを適用すればいいのではないかと考えました.
16
+ しかし実際に`hoge`コマンドを用いると,エラーが発生してしまいました.
17
+
16
18
  `ping/cog.py` (`ping`ライブラリとして定義)
17
19
 
18
20
  ```python
@@ -54,9 +56,8 @@
54
56
  bot.add_cog(ping.PingCog(bot, guild_ids))
55
57
  ```
56
58
 
57
- ----
58
59
 
59
- ## 動作とエラー
60
+ ### 動作とエラー
60
61
 
61
62
  `/ping` → `pong`が投稿される(OK)
62
63
  `/hoge` → `インタラクションに失敗しました`が表示される(NG)

2

タイトルの編集とコメントの追加

2021/07/24 06:24

投稿

hoge_fuga
hoge_fuga

スコア22

title CHANGED
@@ -1,1 +1,1 @@
1
- [discord]利用側でslashコマンドのguild_ids
1
+ [discord]slashコマンドのguild_ids
body CHANGED
@@ -25,11 +25,13 @@
25
25
  class PingCog(commands.Cog):
26
26
  def __init__(self, bot: commands.Bot, g_ids: List[int] = None):
27
27
  self.bot = bot
28
+ # 関数でコマンドを登録(NG)
28
29
  self.hoge = cog_ext.cog_slash(name='hoge', description='hoge fuga', guild_ids=g_ids)(self.hoge)
29
30
 
30
31
  async def hoge(self, ctx: SlashContext):
31
32
  await ctx.send('fuga')
32
33
 
34
+ # デコレータでコマンドを登録(OK)
33
35
  @cog_ext.cog_slash(name='ping', description='ping pong', guild_ids=guild_ids)
34
36
  async def ping(self, ctx: SlashContext):
35
37
  await ctx.send('pong')

1

サンプルを簡略化して書き直した

2021/07/12 08:57

投稿

hoge_fuga
hoge_fuga

スコア22

title CHANGED
@@ -1,1 +1,1 @@
1
- [discord]利用側でslashコマンドを使用するサーバを指定する
1
+ [discord]利用側でslashコマンドのguild_ids
body CHANGED
@@ -1,18 +1,11 @@
1
1
  ## 問題
2
2
  `discord-py-slash-command`を用いてslash commandを扱うcogの開発をしています.
3
- cogの数が多いため,cogを別々にライブラリとして開発し,それらを利用側(`main.py`)でインポートするようにしています.
4
- guild commandとする`guild_ids`が必要なため,以下「プログラム例」のようになっていま
3
+ サンプルコードを参考cogを作成したのですが,`guild_ids`をcogファイル書かければけないことが気になりした
4
+ そこで,`guild_ids`をcogの利用側(`main.py`)に移そうと考えました.
5
+ そのために,下記「プログラム例」のように`hoge`コマンドを定義しました.
6
+ しかし実際に`hoge`コマンドを用いると,エラーが発生してしまいました.
7
+ `guild_ids`をcogの利用側で定めるにはどうすればいいでしょうか?
5
8
 
6
- このままでもきちんと
7
- 動作はしますが,cogの数だけ`guild_ids`が存在してしまいます.
8
- ~~`main.py`側で`guild_ids`を定義するにはどうすればいいでしょうか?~~
9
-
10
- ## 問題[更新]
11
-
12
- 下記の「成功例」のように,クラスの定義を関数内に入れることで,利用側による`guild_ids`の変更が反映されるようになりました.
13
- ですが,実行時までクラスが分からなくなるなど,欠点が大きいです.
14
- より良い解決策はありませんか?
15
-
16
9
  ## 環境
17
10
  * python 3.7
18
11
  * discord.py 1.7.3
@@ -23,23 +16,23 @@
23
16
  `ping/cog.py` (`ping`ライブラリとして定義)
24
17
 
25
18
  ```python
19
+ from typing import List
26
20
  from discord.ext import commands
27
21
  from discord_slash import cog_ext, SlashContext
28
22
 
29
- guild_ids = [11111111111111111111] # <-- cogの数だけ存在してしまう
23
+ guild_ids = [11111111111111111111]
30
24
 
31
-
32
25
  class PingCog(commands.Cog):
33
- def __init__(self, bot: commands.Bot):
26
+ def __init__(self, bot: commands.Bot, g_ids: List[int] = None):
34
27
  self.bot = bot
28
+ self.hoge = cog_ext.cog_slash(name='hoge', description='hoge fuga', guild_ids=g_ids)(self.hoge)
35
29
 
30
+ async def hoge(self, ctx: SlashContext):
31
+ await ctx.send('fuga')
32
+
36
33
  @cog_ext.cog_slash(name='ping', description='ping pong', guild_ids=guild_ids)
37
- async def embed(self, ctx: SlashContext):
34
+ async def ping(self, ctx: SlashContext):
38
35
  await ctx.send('pong')
39
-
40
-
41
- def setup(bot):
42
- return bot.add_cog(PingCog(bot))
43
36
  ```
44
37
 
45
38
  ----
@@ -49,50 +42,29 @@
49
42
  ```python
50
43
  from discord.ext import commands
51
44
  from discord_slash import SlashCommand
45
+ import ping
52
46
 
53
47
  bot_token = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
54
- # guild_ids = [11111111111111111111] # <-- 固有の値はmain.pyでまとめて管理したい
55
48
 
56
49
  bot = commands.Bot(command_prefix='!')
57
50
  slash = SlashCommand(bot, sync_commands=True)
51
+ guild_ids = [11111111111111111111]
58
- bot.load_extension('ping.cog')
52
+ bot.add_cog(ping.PingCog(bot, guild_ids))
59
- bot.run(bot_token)
60
53
  ```
61
54
 
62
- ## 失敗例
55
+ ----
63
- * `main.py`で`guild_ids`を書き換える
64
56
 
65
- `main.py`
57
+ ## 動作とエラー
66
- ```python
67
- from ping.cog import guild_ids # <-- この時点でguild_idsを用いてPingCogが定義されてしまう
68
- guild_ids = [11111111111111111111] # <-- この値は使われない
69
58
 
70
- ...
71
- bot.load_extension('ping.cog')
59
+ `/ping` → `pong`が投稿される(OK)
72
- bot.run(bot_token)
60
+ `/hoge` → `インタラクションに失敗しました`が表示される(NG)
73
- ```
74
61
 
75
- ## 成功例
76
- * クラスの定義を関数内で行う
77
-
78
- `ping/cog.py`
79
- ```python
80
- def define_cog():
81
- class PingCog(commands.Cog): # <-- 実行時までPingCongの存在すら分からない
82
- ...
83
- return PingCog
84
-
85
- def setup(bot):
86
- PingCog = define_cog() # <-- ここでクラスを定義
87
- return bot.add_cog(PingCog(bot))
88
62
  ```
89
-
90
- `main.py`
91
- ```python
92
- from ping.cog import guild_ids # <-- ここではまだPingCogは定義されていない
63
+ An exception has occurred while executing command `hoge`:
93
- guild_ids = [11111111111111111111]
64
+ Traceback (most recent call last):
94
-
65
+ File "X:XXXXX\venv\lib\site-packages\discord_slash\client.py", line 1185, in invoke_command
95
- ...
66
+ await func.invoke(ctx, **args)
67
+ File "X:XXXXX\venv\lib\site-packages\discord_slash\model.py", line 208, in invoke
68
+ return await self.func(self.cog, *args, **kwargs)
96
- bot.load_extension('ping.cog') # <-- この中のsetupでPingCogが定義される
69
+ TypeError: hoge() takes 2 positional arguments but 3 were given
97
- bot.run(bot_token)
98
70
  ```