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

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

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

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

Python

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

Q&A

2回答

3513閲覧

DiscordのメンバーのIDの取得ができません。

HinataC

総合スコア0

Discord

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

Python

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

0グッド

0クリップ

投稿2021/12/06 08:16

実現したいこと

Discord.pyでチケットシステムを構築しようとしているのですが、
チケットを作る際に新しく作成したチャンネルのパーミッションの設定を行いたいのですが、ターゲットとするユーザのIDの取得方法について困っています。
そのユーザーIDは、Discordのチャンネルに設置したボタンを押した人から取得することを考えています。

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

Ignoring exception in on_socket_response Traceback (most recent call last): File "C:\Users\~\Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "C:\Users\~\Python\Python310\lib\site-packages\discord_buttons_plugin\__main__.py", line 17, in soclistener await self.emit(data["data"]["custom_id"], data) File "C:\Users\~\Python\Python310\lib\site-packages\discord_buttons_plugin\__main__.py", line 37, in emit await i(comp) File "C:\Users\~\bot.py", line 50, in start mem = await client.fetch_user(ctx.user.id) AttributeError: 'InteractionContext' object has no attribute 'user'

該当のソースコード

Python

1 2async def create_channel(ctx, channel_name): 3 category_id = ctx.channel.category_id 4 category = ctx.guild.get_channel(category_id) 5 new_channel = await category.create_text_channel(name=channel_name) 6 7 return new_channel 8@buttons.click 9async def start(ctx): 10 dt_now = datetime.datetime.now() 11 time = dt_now.strftime('%Y%m%d%H%M%S') 12 name = '????チケット' + time 13 14 #チャンネルを新規作成 15 newc = await create_channel(ctx, channel_name= name) 16 cid = newc.id 17 c = client.get_channel(cid) 18 19 # ターゲットとするメンバーを取得 20 mem = await client.fetch_user(ctx.user.id) 21 22 # 変更する 23 await c.set_permissions(member, send_messages=True, read_messages=True) 24 25@client.command() 26async def tset(message): 27 if message.author.bot: 28 return 29 await message.channel.send(embed=tmes) 30 await buttons.send( 31 content = "↓↓↓", 32 channel = message.channel.id, 33 components = [ 34 ActionRow([ 35 Button( 36 label="????チケット", 37 style=ButtonType().Primary, 38 custom_id="start" 39 ) 40 ]) 41 ] 42 ) 43 44

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

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

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

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

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

guest

回答2

0

python

1men = ctx.author.id 2fetch_men = await client.fetch_user(men)

これで
men : がユーザーid (18桁のメッセージ送信者のid)
fetch_men : がユーザーidに対応したユーザー名

が出力されます。

例:
men : 235088799074484224
の時、
fetch_men : Rythm#3722

となります。

投稿2021/12/07 03:31

編集2021/12/07 03:36
pecop

総合スコア409

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

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

HinataC

2021/12/07 04:28

これで一度試みたのですが、以下のようなエラーが出て実行できませんでした。 ``` AttributeError: 'InteractionContext' object has no attribute 'author' ``` ボタンを押した人の属性がctx内にあるのか分からないんです...
pecop

2021/12/12 08:29 編集

返信遅くなりました。 以下2つは間違えたのでこの2つ下のコードを参考にしてください。 ---------------------------- > AttributeError: 'InteractionContext' object has no attribute 'author' これは 'InteractionContext' に 'author' がないと言われているのですが・・ 自分は ``` from discord.ext import commands client = commands.Bot(command_prefix='.', help_command=None) token = 'your_token' @client.command() async def test(ctx: commands.Context): a = ctx.author.id x = await client.fetch_user(a) await ctx.send(x) client.run(token) ``` でふつうに動いています。 公式APIにも https://gyazo.com/7b86b29859652b445ba46f53f1da2453 Attributes の 上から2行目に `author` があります。 おそらくimportが上手くいってないかバージョンが古いかのどちらかかと。 あとこのコードをつかって `.test` と打って じぶんのユーザー名が返ってこなかったらいったんimportあたりも載せて見せてほしいです。
pecop

2021/12/12 07:27

すみません コード内のasync分のインデント付け忘れてました。
pecop

2021/12/12 08:32 編集

上の2つは間違えました。 こちらを参考にしてください。 返信遅くなりました。 > AttributeError: 'InteractionContext' object has no attribute 'author' これは 'InteractionContext' に 'author' がないと言われているのですが・・ 自分は ``` from discord.ext import commands import discord from discord_buttons_plugin import * import datetime bot = commands.Bot(command_prefix='!') buttons = ButtonsClient(bot) TOKEN = 'your_token' async def create_channel(ctx, channel_name): category_id = ctx.channel.category_id category = ctx.guild.get_channel(category_id) new_channel = await category.create_text_channel(name=channel_name) return new_channel @bot.event async def on_ready(): print("Ready") @buttons.click async def start(ctx): dt_now = datetime.datetime.now() time = dt_now.strftime('%Y%m%d%H%M%S') name = '????チケット' + time # チャンネルを新規作成 newc = await create_channel(ctx, channel_name=name) cid = newc.id c = client.get_channel(cid) # ターゲットとするメンバーを取得 mem = await client.fetch_user(ctx.user.id) # 変更する await c.set_permissions(member, send_messages=True, read_messages=True) @bot.command() async def x(ctx: commands.Context): if ctx.author.bot: return await buttons.send( content="↓↓↓", channel=ctx.channel.id, components=[ ActionRow([ Button( label="????チケット", style=ButtonType().Primary, custom_id="start" ) ]) ] ) bot.run(your_token) ``` で動きました。
guest

0

エラーの内容は
「'InteractionContext'っていうオブジェクト(ctxのこと)に'user'っていう属性はないよー。」
ってことです。
一度ctxをprintするとどこにidがあるかがわかると思いますんでそこを指定すればよいかと。
注記:discord.pyはサポート・開発が停止されました。Discord.jsへの移行も少し頭に入れておいていただけるといいと思います。

投稿2021/12/06 13:13

ozo_uni

総合スコア7

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

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

Za_discord

2022/01/20 06:16

pycordがありますnextcordも
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問