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

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

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

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

Python

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

Q&A

解決済

1回答

3016閲覧

discord.pyでメンバーのニックネームを変更する方法が分からない

meoto2408

総合スコア52

Python 3.x

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

Python

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

0グッド

0クリップ

投稿2019/03/13 16:08

編集2019/03/14 13:41

コマンドを実行すると役職を付与、ニックネームを変更したいです。

python

1 @commands.command() 2 async def agree(self, ctx, mcid): 3 if ctx.channel.id == 499427177444409366: 4 if mcid == "notid": 5 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 6 role1 = discord.utils.get(ctx.guild.roles, name="通知") 7 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 8 await ctx.author.add_roles(role, role1, role2) 9 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 10 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 11 await ctx.author.send(embed=embed) 12 await ctx.author.edit(ctx.author, ctx.author_name + "#非スポンサー") 13 else: 14 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 15 role1 = discord.utils.get(ctx.guild.roles, name="通知") 16 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 17 await ctx.author.add_roles(role, role1, role2) 18 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 19 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 20 await ctx.author.send(embed=embed) 21 await ctx.author.edit(ctx.author, mcid + "#非スポンサー") 22 if ctx.channel.id == 555051444596768789: 23 if mcid == "notid": 24 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 25 role1 = discord.utils.get(ctx.guild.roles, name="通知") 26 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 27 await ctx.author.add_roles(role, role1, role2) 28 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 29 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 30 await ctx.author.send(embed=embed) 31 await ctx.author.edit(ctx.author, ctx.author_name + "#非スポンサー") 32 else: 33 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 34 role1 = discord.utils.get(ctx.guild.roles, name="通知") 35 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 36 await ctx.author.add_roles(role, role1, role2) 37 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 38 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 39 await ctx.author.send(embed=embed) 40 await ctx.author.edit(ctx.author, mcid + "#非スポンサー") 41 else: 42 return

#追記
ですが、ニックネームを変更する方法がわかりません。
どうかご教授お願い致します。

#追記2
pythonのバージョンは3.6.6です。
discord.pyは1.0.0aです。

discord.pyのドキュメント等は既に確認済みです。
それでできませんでした。

どうかご教授ください。

#追記3

それでできませんでした。について・・
以下を読んでも分からなかった部分を質問してください。という部分から読んでできませんでした、という意味でした。わかりずらい表記をしてすみません。

ニックネームの変更がわかりません。
また、こういう場所で質問する場合はドキュメントをしっかり読んで、日本語じゃないなら翻訳を使いながら読み取ってそれで分からなければ聞くという流れで質問しなければ失礼だとわかってます。
なので、実装したいことを説明しますと、
!?agree Monster2408と入力するとニックネームがMonster2408#非スポンサーとなるようにしたいです。

await ctx.bot.change_nickname(mcid + "#非スポンサー")このようにしたり、ctxself.botmcidにしたり、ここを参考にawait after.edit(nick=last)を使ってみたりと試行錯誤しましたが、ニックネームを変更する部分のみうまくできませんでした。

#追記3.5
どういう結果だったかを忘れていました。
コマンド実行時に同じようなエラーがでるといった感じです。

error

1Ignoring exception in command agree: 2Traceback (most recent call last): 3 File "C:\Users\enjoy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 62, in wrapped 4 ret = await coro(*args, **kwargs) 5 File "c:\Users\enjoy\OneDrive\デスクトップ\Python\DiscordBot\cogs\testcog.py", line 275, in agree 6 await ctx.bot.change_nickname(nick=ctx.author_name + "#非スポンサー") 7AttributeError: 'MyBot' object has no attribute 'change_nickname' 8 9The above exception was the direct cause of the following exception: 10 11Traceback (most recent call last): 12 File "C:\Users\enjoy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 898, in invoke 13 await ctx.command.invoke(ctx) 14 File "C:\Users\enjoy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 611, in invoke 15 await injected(*ctx.args, **ctx.kwargs) 16 File "C:\Users\enjoy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 71, in wrapped 17 raise CommandInvokeError(exc) from exc 18discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'MyBot' object has no attribute 'change_nickname'

#追記4

python

1 @commands.command() 2 async def agree(self, ctx, mcid): 3 if ctx.channel.id == 499427177444409366: 4 if mcid == "notid": 5 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 6 role1 = discord.utils.get(ctx.guild.roles, name="通知") 7 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 8 await ctx.author.add_roles(role, role1, role2) 9 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 10 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 11 await ctx.author.send(embed=embed) 12 await ctx.bot.change_nickname(nick=ctx.author_name + "#非スポンサー") 13 else: 14 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 15 role1 = discord.utils.get(ctx.guild.roles, name="通知") 16 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 17 await ctx.author.add_roles(role, role1, role2) 18 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 19 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 20 await ctx.author.send(embed=embed) 21 await ctx.change_nickname(mcid + "#非スポンサー") 22 if ctx.channel.id == 555051444596768789: 23 if mcid == "notid": 24 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 25 role1 = discord.utils.get(ctx.guild.roles, name="通知") 26 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 27 await ctx.author.add_roles(role, role1, role2) 28 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 29 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 30 await ctx.author.send(embed=embed) 31 await ctx.bot.change_nickname(nick=ctx.author_name + "#非スポンサー") 32 else: 33 role = discord.utils.get(ctx.guild.roles, name="m._.m(モン民)") 34 role1 = discord.utils.get(ctx.guild.roles, name="通知") 35 role2 = discord.utils.get(ctx.guild.roles, name="再認証者") 36 await ctx.author.add_roles(role, role1, role2) 37 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 38 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#499571567823421440>にて`!?help`と入力するとコマンド一覧が、\n<#546337968042803222>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 39 await ctx.author.send(embed=embed) 40 await ctx.bot.change_nickname(mcid + "#非スポンサー")

これが最新のコードです。

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

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

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

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

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

quickquip

2019/03/14 00:48

質問がないです。
ryu22e

2019/03/14 06:47

使っているライブラリの名前・バージョンを書いてください。 ソースコードの内容から、「discord.pyを使ってDiscord botを作ろうとしている」と推測していますが、もしそうならdiscord.pyには公式ドキュメントがあります。以下を読んでも分からなかった部分を質問してください。 https://discordpy.readthedocs.io/en/latest/ ざっと検索したところだと、change_nicknameというメソッドでニックネームの変更ができるようです。 https://discordpy.readthedocs.io/en/latest/api.html?highlight=util#discord.Client.change_nickname また、質問のタイトルは質問文の要約にしたほうが回答者の目に止まりやすいです。 (例: discord.pyでメンバーのニックネームを変更する方法が分からない)
ryu22e

2019/03/14 07:51

最初の質問内容より状況は分かりやすくなってきましたが、まだ肝心の「何を試して、どういう結果だったか」が書かれていません。 「追記2」に書かれている「それでできませんでした。」とはどういう状況でしょうか? 本当に何も手が付けられないということだと、回答はされにくいと思います。 英語のドキュメントなので読むのは大変だと思いますが、Google翻訳などを使って何とか頑張ってみてください。
ryu22e

2019/03/14 13:25

「追記3.5」でどんなエラーが発生しているのか分かりました。私が最初のコメントで書いたchange_nicknameメソッドも、実際に試していたのですね。 エラー内容を読むと、testcog.pyの275行目で「MyBot」というオブジェクトのchange_nicknameメソッドを呼ぼうとして、存在しないためにAttributeErrorになっています。 おそらく、change_nicknameメソッドを呼ぶ前の段階でdiscord.pyの使い方を間違えているのだと思います。 質問文の最初に書かれているコードではchange_nicknameメソッドを呼んでいる部分がないので、これ以上こちらから調査することは難しいです。 なるべく、実際に書いたコードの全文を質問文に書いてください。 (ただし、コードの中にDiscord API の Client Secret Keyのような公開できない値が書かれている場合は、そこだけ伏せ字にしてください)
ryu22e

2019/03/15 04:25 編集

「追記4」に書いているソースコードは本当に実際に書いたコードの**全文**ですか? 見たところ、「ctx.bot」の中身が何なのか調べる必要がありますが、agreeメソッドの呼び出し元のコードが載っていないので何も分かりません。 また、discord.pyのバージョンは1.0.0aとのことですが、どうやってインストールしたのでしょうか? 実際に手元のPCでdiscord.pyインストールしようとしたのですが、「pip install "discord.py==1.0.0a"」だと以下の「該当するバージョンがない」旨のエラーになります。 Could not find a version that satisfies the requirement discord.py==1.0.0a (from versions: 0.1.0, 0.2.0, 0.2.1, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.6.0, 0.6.1, 0.6.2, 0.6.3, 0.7.0, 0.8.0, 0.9.0, 0.9.1, 0.9.2, 0.10.0, 0.11.0, 0.12.0, 0.13.0, 0.14.0, 0.14.1, 0.14.2, 0.14.3, 0.15.0, 0.15.1, 0.16.0, 0.16.1, 0.16.2, 0.16.3, 0.16.4, 0.16.5, 0.16.6, 0.16.7, 0.16.8, 0.16.9, 0.16.10, 0.16.11, 0.16.12) No matching distribution found for discord.py==1.0.0a 「pip install discord.py --pre」でpre-releaseなバージョンをインストールしても0.16.12がインストールされます。 discord.pyのソースコード( https://github.com/Rapptz/discord.py )を見ても1.0.0aというバージョンは見当たりませんでした。 「pip freeze」で表示されるdiscord.pyのバージョンは本当に「1.0.0a」ですか? もし「1.0.0a」なら、インストール方法についても記載してください。
meoto2408

2019/03/15 07:33

あ、すみません。 自己解決しました。
guest

回答1

0

自己解決

python

1 @commands.command() 2 async def agree(self, ctx, mcid, ac): 3 if ctx.channel.id == 556847189662367755: 4 channel = self.bot.get_channel(557920512169476098) 5 a = "{}".format(ctx.author.id) 6 dd = "----------\n{} | ".format(ctx.author.mention) + a + " | " + mcid 7 if ac == "main": 8 role = discord.utils.get(ctx.guild.roles, name="メンバー") 9 role1 = discord.utils.get(ctx.guild.roles, name="通知") 10 role2 = discord.utils.get(ctx.guild.roles, name="非認証者") 11 await ctx.author.add_roles(role, role1) 12 await ctx.author.remove_roles(role2) 13 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 14 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#557173935590998016>にて`!?help`と入力するとコマンド一覧が、\n<#556848319360532480>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 15 await ctx.author.send(embed=embed) 16 await ctx.author.edit(nick=mcid + "#非スポンサー") 17 await channel.send(dd) 18 if ac == "sub": 19 role = discord.utils.get(ctx.guild.roles, name="メンバー") 20 role1 = discord.utils.get(ctx.guild.roles, name="通知") 21 role2 = discord.utils.get(ctx.guild.roles, name="非認証者") 22 await ctx.author.add_roles(role, role1) 23 await ctx.author.remove_roles(role2) 24 embed = discord.Embed(title="{}さんの認証が完了しましたなの".format(ctx.author.name), description='', color=0xff0000) 25 embed.add_field(name="MLSBOTに関して・・・", value="当サーバーではあらゆる機能を持ったBOTがいます。\nその機能は<#557173935590998016>にて`!?help`と入力するとコマンド一覧が、\n<#556848319360532480>にて`何ができるの?`と聞いたり`何が話せるの?`と聞いたりすると機能がわかります。", inline=False) 26 await ctx.author.send(embed=embed) 27 await ctx.author.edit(nick=mcid + "(さぶ)#非スポンサー") 28 await channel.send(dd)

Discord.pyにはrewriteバージョンというのがあり、そこに1.0.0aは該当します。

ご回答いただいた方々ありがとうございました。

投稿2019/03/24 11:14

meoto2408

総合スコア52

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問