2017-05-14 8 views
2
discord.py

にユーザーのIDを使用してユーザーを言及しないどのように私は楽しいのようなコマンドでちょうどAPIのこつを得るために始めたは私がdiscord.py使用して単純なボットをコーディングしようとしている

import discord 
import asyncio 
client = discord.Client() 


@client.event 
async def on_message(message): 
    # we do not want the bot to reply to itself 
    if message.author == client.user: 
     return 

    if message.content.startswith('!hug'): 
     await client.send_message(message.channel, "hugs {0.author.mention}".format(message)) 

    # Greetings 
    if message.content.startswith('hello'): 
     msg = 'Hello {0.author.mention}'.format(message) 
     await client.send_message(message.channel, msg)   

    # say (id) is the best 
    # This is where I am lost. how to mention someone's name or id ? 
    if message.content.startswith('!best'): 
     mid = User.id('ZERO#6885').format(message) 
     await client.send_message(message.channel, '{mid} mentioned') 
+1

「discord.py」はどこから来たのですか? – boardrider

答えて

5

だから私は最終的に他の人がこの恩恵を受けると私が実際に持っていたより少ない痛みを持って望ん試行錯誤の数日後にこれを行う方法を考え出した。.. ソリューションは、最終的には簡単でした。..

if message.content.startswith('!best'): 
     myid = '<@201909896357216256>' 
     await client.send_message(message.channel, ' : %s is the best ' % myid) 
1

Userオブジェクトの場合は、User.mentionという属性を使用して、ユーザーの言葉を表す文字列を取得します。自分のIDからユーザーオブジェクトを取得するには、Client.get_user_info(id)が必要です。ユーザー名( 'ZERO')とディスクリミネータ( '#6885')からユーザーを取得するには、ユーティリティー機能discord.utils.get(iterable, **attrs)を使用します。文脈で:

if message.content.startswith('!best'): 
    user = discord.utils.get(message.server.members, name = 'ZERO', discriminator = 6885) 
    # user = client.get_user_info(id) is used to get User from ID, but OP doesn't need that 
    await client.send_message(message.channel, user.mention + ' mentioned') 
+0

ファイル "\ client.py"、307行目、_run_eventのgetattr(self、event)(* args、** kwargs)からの出力 ファイル "bot.py"、39行目on_message id = User.idあなたがIDを取得するために何をしているのかを確認してください。 NameError:name 'User'が定義されていません。 –

+0

あなたのIDの取得方法はうまくいきません - 申し訳ありませんが、私はしませんでした。それをもっと明確にする。 –

+0

@ItachiSamaさらに読むと、実際にはユーザーのIDは持っていません。つまり、ユーザー名とディスクリミネータがあります。私はそれを反映するようにコードを変更し、あなたがコメントにIDを持っている場合に動作するメソッドを残しました。 –

1

あなたはコマンドで作業している場合は、あなたがdiscord.pyは、コマンドの機能に組み込まれて、あなたの抱擁コマンドがなります使用するのが最善だ:

import discord 
from discord.ext import commands 

@commands.command(pass_context=True) 
async def hug(self, ctx): 
    await self.bot.say("hugs {}".format(ctx.message.author.mention())) 

これはassuです

def __init__(self): 
    self.bot = discord.Client(#blah) 
関連する問題